Test Your Knowledge (Ch 8)

The following questions are based on what we have learned so far in Chapter 8 – Operators in C. To test your understanding, try answering the following questions. The explanations are also provided to each question so you can always check the correctness of your answers. All the best 👍


Question 1

State whether the following statement is True or False.
“Unary operators have the highest precedence among all the operators.”

True
False
explanation

It is not the case that unary operators have the highest precedence among all the operators. Parentheses and increment/decrement operators have greater precedence than unary operators. Refer to the table given in this tutorial for more information.

Question 2

Can we use floating-point operands with the modulus (%) operator?

Yes
No
explanation

Modulus operator (%) requires integer operands, hence we cannot use floating-point operands with it.

Question 3

Consider the following code snippet:

int i;
float f;

i = 2.1456f;
f = 10

What are the final values of i and f?

i = 2.1456 and f = 10
i = 2.1456 and f = 10.0
i = 2 and f = 10
i = 2 and f = 10.0
explanation

Consider the assignment expression x = y. If x and y are of different types, then the value of y will be converted to the type of the x. This is the reason why 2.1456 is converted to 2 as it is assigned to an integer variable, and 10 is converted to 10.0 as it is assigned to a float variable.

Question 4

Consider the following code snippet:

int i;
float f;

f = i = 3.33;

What is the final value of f?

f = 3.33
f = 3
f = 3.0
f = 3.30
explanation

The assignment operator is right-associative; therefore, the order of evaluation is right to left. First, 3.33 is assigned to ‘i’. Since ‘i’ is an integer variable, the type of 3.33 is converted to an integer, which means 3 is stored in the variable ‘i’. Similarly, ‘f’ is a float variable; hence, 3.0 is stored in the variable ‘f’. Therefore, the final value of ‘f’ is 3.0.

Question 5

Consider the following expression:

a * b - c * d / e

Which of the following expression is equivalent to the above expression?

[(a * b) – [(c * d) / e]]
[a * [(b – c) * (d / e)]]
[(a * b) (- (c * d) / e)]
a * (b – c) * (d / e)
explanation

According to the instructions, multiplication and division have a higher precedence than subtraction. The order of evaluation is from left to right. The first option is the only option that satisfies these criteria, therefore it is the correct answer.

Question 6

Determine the output of the following program:

#include <stdio.h>

int main() {
   int i = 1;
   int j = ++i + 50 * 2;
 
   printf("%d %d", i, j);
   return 0;
}
1 101
1 102
2 101
2 102
explanation
j = ++i + 50 * 2
j = 2 + 50 * 2    [pre-increment]
j = 2 + 100       [multiplication has greater precedence than addition]
j = 102

At the end, i = 2 (as i is incremented in the expression evaluation) and j = 102.

Question 7

Determine the output of the following program:

#include <stdio.h>

int main() {
   int i = 1;
   int j = i++ + 50 * 2;
 
   printf("%d %d", i, j);
   return 0;
}
1 101
1 102
2 101
2 102
explanation
j = i++ + 50 * 2
j = 1 + 50 * 2    [increment will happen after expression evaluation]
j = 1 + 100       [multiplication has greater precedence than addition]
j = 101

At the end, i = 2 (as i is incremented in the expression evaluation) and j = 101.

Question 8

Determine the output of the following program:

#include <stdio.h>

int main() {
   printf("%d", !(10 > 5) && (2 != 2) || (3 >= 3));
   return 0;
}
1
0
explanation
!(10 > 5) && (2 != 2) || (3 >= 3)
!(1) && (0) || (1)
0 && 0 || 1
0 || 1
1
Question 9

Determine the output of the following program:

#include <stdio.h>

int main() {
    int i = 1, j = 2;
    printf("%d", ++i + (j = i > 0));
    return 0;
}
4
3
2
1
explanation
++i + (j = i > 0)
2 + (j = 1)
2 + 1
3
Question 10

Determine the output of the following program:

#include <stdio.h>

int main() {
    int i = 1, j = 2;
    printf("%d", !!i + !j);
    return 0;
}
1
0
2
None of the above
explanation

!!i + !j !0 + !2 1 + 0 1

Your score is out of



Leave a comment

Leave a comment

Your email address will not be published. Required fields are marked *

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.