Basic data types, operations, and flow control (decision-making statements) Module 2 Test Answers Full 100%
-
What happens if you try to compile and run this program?
#include <stdio.h> int main(void) { int i, j, k; i = 2; j = -2; if(i) i--; if(j) j++; k = i * j; printf("%d",k); return 0; }
-1
-2
1
2
Explanation & Hints: With the provided C program, here’s what will happen when it’s compiled and run:
- Variable Initialization:
i
is initialized to 2.j
is initialized to -2.
- Conditional Operations:
if(i)
checks ifi
is non-zero. Sincei
is 2, the condition is true, andi--
is executed, decrementingi
to 1.if(j)
checks ifj
is non-zero. Sincej
is -2, the condition is also true, andj++
is executed, incrementingj
to -1.
- Multiplication and Output:
k
is calculated as the product ofi
andj
, which are now 1 and -1 respectively, sok = 1 * -1 = -1
.- The program then prints
-1
.
- Return: The program returns 0, indicating successful execution.
Thus, when this program is compiled and executed, it outputs
-1
.- Variable Initialization:
-
What is the value of the
x
variable at the end of the following snippet?float x; x = 1. / 2 * 3; /***
1.5
1
2
- 0
-
Explanation & Hints: To determine the value of the
x
variable in the given snippet, we need to evaluate the expressionx = 1. / 2 * 3;
.Here, the expression involves:
1.
which is a floating-point number equivalent to1.0
.- Division by
2
, resulting in1.0 / 2 = 0.5
. - Multiplication of the result by
3
, leading to0.5 * 3 = 1.5
.
Thus, the value of
x
at the end of this snippet will be1.5
.
-
What is the decimal value of the following integer literal?
0x8
8
100
10
- the literal is invalid
-
Explanation & Hints: The integer literal you provided,
0x8
, represents a hexadecimal (base 16) value. In hexadecimal, digits range from0
to15
, with the lettersA
throughF
representing values10
through15
. Here’s how you convert0x8
to decimal (base 10):8
in hexadecimal is simply8
in decimal.
So, the decimal value of the hexadecimal integer literal
0x8
is8
. This means the correct answer from your list is8
.
-
What is the value of the
c
variable at the end of the following snippet?char c; c = '\';
- the assignment is invalid and causes a compilation error
\
'
\0
-
Explanation & Hints: Cosd is indeed invalid due to a syntax error. In C programming, the backslash (
\
) is used as an escape character to denote special characters like newline (\n
), tab (\t
), null (\0
), etc. When you writec = '\';
, the compiler expects something to follow the backslash to complete the escape sequence.Since the backslash is used improperly here without a following character that forms a valid escape sequence, this line of code will cause a compilation error. To correctly assign a backslash to the character variable
c
, you should use two backslashes:c = '\\';
This correctly represents a single backslash character. Thus, in the context of the options you provided, the most accurate response is:
- the assignment is invalid and causes a compilation error
-
Which line of code must you insert in order for the program to produce the expected output?
Expected output:
1
Code:
#include <stdio.h> int main() { var = 1; printf("%d \n", var); return 0; }
int var;
placed above
var = 1;
int var;
placed below
var = 1;
int var;
placed below
printf("%d \n", var);
- No change are required – the program will compile and produce the expceted output
-
Explanation & Hints: The code has an issue because the variable
var
is used in themain
function before it’s declared and defined. Here’s the relevant part of the code:#include <stdio.h> int main() { var = 1; printf("%d \n", var); return 0; } int var;
In this layout,
var
is declared after its usage inmain
, which is not valid in C. C requires that variables be declared before they are used. This means you need to declarevar
at a scope accessible tomain
before it is used.To fix the code, you should place the declaration of
var
at a global level before themain
function begins. This would makevar
a global variable accessible withinmain
and correctly initialized before it’s used. Here’s how you should modify the code:#include <stdio.h> int var; int main() { var = 1; printf("%d \n", var); return 0; }
The correct choice is:
- placed above
var = 1;
This option places the declaration of
var
where it needs to be for the code to compile successfully and produce the expected output of1
. - placed above
-
What is the value of the
var
variable at the end of the following snippet?int var; var = 2; var = var * var; var = var + var; /* var = var / var; var = var % var; */
8
1
0
16
-
Explanation & Hints: To find the value of the variable
var
at the end of the given C code snippet, let’s walk through the code step by step:Initialization:
int var; var = 2;
Here,
var
is initialized to2
.First Update:
var = var * var;
The value of
var
is squared, so2 * 2 = 4
. Nowvar
equals4
.Second Update:
var = var + var;
The value of
var
is added to itself, so4 + 4 = 8
. Nowvar
equals8
.The lines within the comment block are not executed, as they are commented out:
/* var = var / var; var = var % var; */
Thus, these lines do not affect the value of
var
.Therefore, the value of
var
at the end of this snippet is 8.
-
What is the value of the
k
variable at the end of the following snippet?int i, j, k; i = 3; j = -3; k = (i >= i) + (j <= j) + (i == j) + (i > j);
3
2
1
0
-
Explanation & Hints: To determine the value of the variable
k
at the end of the provided C code snippet, we will evaluate each of the expressions in the assignment tok
based on the values ofi
andj
. Here’s the detailed evaluation:Variable Initialization:
int i, j, k; i = 3; j = -3;
Assignment to k:
k = (i >= i) + (j <= j) + (i == j) + (i > j);
Breaking down each part:
(i >= i)
: This checks ifi
is greater than or equal toi
. Sincei
is equal to itself, this is true, and thus, evaluates to1
.(j <= j)
: This checks ifj
is less than or equal toj
. Sincej
is equal to itself, this is true, and thus, evaluates to1
.(i == j)
: This checks ifi
is equal toj
. Since3
is not equal to-3
, this is false, and thus, evaluates to0
.(i > j)
: This checks ifi
is greater thanj
. Since3
is greater than-3
, this is true, and thus, evaluates to1
.
Summing these results gives:
k = 1 + 1 + 0 + 1; k = 3;
Thus, the value of
k
at the end of this code snippet is 3.
-
What is the value of the
k
variable at the end of the following snippet?int i, j, k; i = 3; j = -3; k = i * j; k += j; k /= i;
-4
-8
4
8
-
Explanation & Hints: Let’s evaluate the code snippet step by step to find the value of the variable
k
:Initialization of Variables:
int i, j, k; i = 3; j = -3;
Calculation of k:
- First,
k
is calculated by multiplyingi
andj
:
k = i * j;
Substituting the values, =3×−3=−9.- Next,
j
is added tok
:
k += j;
This translates to =−9+(−3)=−12.- Finally,
k
is divided byi
:
k /= i;
This results in =−12÷3=−4.Therefore, the value ofk
at the end of this snippet is -4. - First,
-
What is the value of the
k
variable at the end of the following snippet?int i, j, k; i = 4; j = 5; k = i-- * ++j;
24
28
21
18
-
Explanation & Hints: Let’s break down the operations step-by-step:
int i, j, k; i = 4; j = 5; k = i-- * ++j;
- Initial values:
i = 4
j = 5
- Pre-increment
j
(++j
):++j
incrementsj
by 1 before using its value.- So,
j
becomes6
.
- Post-decrement
i
(i--
):i--
uses the current value ofi
(which is4
) and then decrementsi
by 1.- So, the value of
i
used in the multiplication is4
, and after the operation,i
becomes3
.
- Multiplication:
k = i-- * ++j
k = 4 * 6
k = 24
So, the value of
k
at the end of the snippet is 24. - Initial values:
-
Which of the following identifiers is a valid variable name?
Monte_Carlo
Monte-Carlo
Monte Carlo
Monte@Carlo
-
Explanation & Hints: Generally, variable names must:
- Begin with a letter (uppercase or lowercase) or an underscore (_).
- Be followed by letters, digits, or underscores.
Based on these rules, let’s evaluate the given identifiers:
- Monte_Carlo: This is a valid variable name. It starts with a letter and contains only letters and underscores.
- Monte-Carlo: This is not a valid variable name because it contains a hyphen (-), which is not allowed in variable names.
- Monte Carlo: This is not a valid variable name because it contains a space, which is not allowed in variable names.
- Monte@Carlo: This is not a valid variable name because it contains an at symbol (@), which is not allowed in variable names.
So, the valid variable name is Monte_Carlo.
-
What is the value of the
var
variable at the end of the following snippet?int var; var = 2; var = var * var; var = var + var; var = var / var; var = var % var;
0
1
8
16
-
Explanation & Hints: Let’s break down the operations step-by-step:
int var; var = 2; var = var * var; var = var + var; var = var / var; var = var % var;
- Initial value:
var = 2
- Multiplication:
var = var * var
var = 2 * 2
var = 4
- Addition:
var = var + var
var = 4 + 4
var = 8
- Division:
var = var / var
var = 8 / 8
var = 1
- Modulo:
var = var % var
var = 1 % 1
var = 0
(since any number modulo itself is 0)
So, the value of
var
at the end of the snippet is 0. - Initial value:
-
What is the value of the
x
variable at the end of the following snippet?int x; x = 1 / 2;
0
1
2
0.5
-
Explanation & Hints: Let’s break down the assignment:
int x; x = 1 / 2;
1 / 2
is an integer division. Since both1
and2
are integers, the result is also an integer.1 / 2
equals0
in integer division because the fractional part (0.5) is discarded.
So, the value of
x
at the end of the snippet is 0.
-
What is the value of the following floating-point literal?
8765E-2
87.65
8.765
876.5
0.8765
-
Explanation & Hints: The floating-point literal
8765E-2
is expressed in scientific notation, whereE-2
denotes multiplication by 10−2.To calculate the value:
8765×10−2=8765×0.01=87.65
So, the value of the floating-point literal
8765E-2
is 87.65.
-
What is the value of the
x
variable at the end of the following snippet?int x; x = 1 / 2 * 3; /* */
0
1
2
1.5
-
Explanation & Hints: Let’s break down the operations step-by-step:
int x;
x = 1 / 2 * 3;In C, the operations are performed from left to right due to the same precedence level (both
/
and*
have the same precedence and associate left to right):- Integer division:
1 / 2
is an integer division. Since both1
and2
are integers, the result is also an integer.1 / 2
equals0
in integer division because the fractional part (0.5) is discarded.
- Multiplication:
- The result of the division (
0
) is then multiplied by3
. 0 * 3
equals0
.
- The result of the division (
So, the value of
x
at the end of the snippet is 0. - Integer division:
-
Which of the following identifiers is an invalid variable name?
0_
_0_
_0
___
-
Explanation & Hints: In most programming languages, variable names must adhere to specific rules. Typically, these rules include:
- The name must start with a letter (uppercase or lowercase) or an underscore (
_
). - The name can only contain letters, digits, and underscores.
Given these rules, let’s evaluate the provided identifiers:
- 0_: This is an invalid variable name because it starts with a digit.
- 0: This is a valid variable name. It starts with an underscore and contains only letters and underscores.
- _0: This is a valid variable name. It starts with an underscore and contains only letters and underscores.
- ___: This is a valid variable name. It starts with an underscore and contains only underscores.
So, the invalid variable name is 0_.
- The name must start with a letter (uppercase or lowercase) or an underscore (
-
What is the decimal value of the following integer literal?
08
the literal is invalid
8
1000
10
-
Explanation & Hints: In most programming languages, including C and C++, an integer literal that starts with a
0
is interpreted as an octal (base-8) number. However, the digits in an octal number can only range from 0 to 7. The presence of the digit8
makes08
an invalid octal number.Therefore, the correct response is:
the literal is invalid.
-
What is the value of the
k
variable at the end of the following snippet?int i, j, k; i = 4; j = 5; k = --i * j++;
15
18
16
12
-
Explanation & Hints: Let’s break down the operations step-by-step:
int i, j, k; i = 4; j = 5; k = --i * j++;
- Initial values:
i = 4
j = 5
- Pre-decrement
i
(--i
):--i
decrementsi
by 1 before using its value.- So,
i
becomes3
.
- Post-increment
j
(j++
):j++
uses the current value ofj
(which is5
) and then incrementsj
by 1.- So, the value of
j
used in the multiplication is5
, and after the operation,j
becomes6
.
- Multiplication:
k = --i * j++
k = 3 * 5
k = 15
So, the value of
k
at the end of the snippet is 15. - Initial values:
-
What is the value of the
c
variable at the end of the following snippet?char c; c = 'a'; c -= ' ';
A
a
\0
- the assignment is invalid and causes a compilation error
-
Explanation & Hints: In C, characters are represented as integers according to their ASCII values. The ASCII value of
'a'
is 97 and the ASCII value of the space character' '
is 32.Here’s the snippet you provided:
char c; c = 'a'; c -= ' ';
Let’s break down the operations step-by-step:
- Initial assignment:
c = 'a'
assigns the character'a'
toc
. The ASCII value of'a'
is 97.
- Subtraction operation:
c -= ' '
subtracts the ASCII value of' '
(which is 32) from the ASCII value of'a'
.- So, the calculation is:
97 - 32 = 65
.
- Resulting character:
- The resulting ASCII value, 65, corresponds to the character
'A'
.
- The resulting ASCII value, 65, corresponds to the character
Therefore, the value of the
c
variable at the end of the snippet is ‘A’. - Initial assignment:
-
Is the following declaration valid?
int var, var;
- No
- Yes
-
Explanation & Hints: No, the declaration
int var, var;
is not valid in C (and most other programming languages) because it attempts to declare the same variablevar
twice in the same scope. This results in a compilation error due to the redefinition of the variable name. Each variable in a given scope must have a unique identifier.
-
Which of the following is a proper integer number (in the “C” language sense)?
123456
123,456
123.456
123_456
-
Explanation & Hints: In C, a proper integer number is a sequence of digits without any separators such as commas, decimal points, or underscores. Based on this, let’s evaluate the provided options:
- 123456: This is a proper integer number.
- 123,456: This is not a proper integer number because it contains a comma.
- 123.456: This is not a proper integer number because it contains a decimal point.
- 123_456: This is not a proper integer number because it contains an underscore.
Therefore, the proper integer number in the “C” language sense is 123456.