Machine-translated by Google. Questions are authored in English.
Top

Expressions Questions and Answers

21.

What is the value of x in this C code?    

void main()
    {
        int x = 4 *5 / 2 + 9;
    }

 

Answer: C

Correct answer: 19

* and / bind tighter than + and, having equal precedence, associate left to right:

\[ 4 \times 5 = 20, \quad 20 / 2 = 10, \quad 10 + 9 = 19 \]

Every step is integer arithmetic, so the answer is exactly 19.

Please log in or register to report this question.

22.

What is the output of this C code?

void main()
    {
        int x = 4.3 % 2;
        printf("Value of x is %d", x);
    }

 

Answer: D

Correct answer: Compile time error

The % operator is defined only for integer operands. 4.3 is a double, so 4.3 % 2 is rejected at compile time - there is no implicit conversion to rescue it. For a floating-point remainder you would call fmod(4.3, 2) from <math.h>.

Please log in or register to report this question.

23.

What is the output of this C code?

void main()
    {
        int y = 3;
        int x = 7 % 4 * 3 / 2;
        printf("Value of x is %d", x);
    }

 

Answer: A

Correct answer: Value of x is 4

%, * and / all have the same precedence and associate left to right, so the expression is grouped as ((7 % 4) * 3) / 2:

\[ 7 \bmod 4 = 3, \quad 3 \times 3 = 9, \quad 9 / 2 = 4 \]

The final division is integer division, so the 0.5 is discarded and x is 4. The declaration of y is never used.

Please log in or register to report this question.

24.

What is the output of this C code?    

void main()
    {
        int a = 5;
        int b = ++a + a++ + --a;
        printf("Value of b is %d", b);
    }

 

Answer: D

Correct answer: Undefined behaviour

b = ++a + a++ + --a; modifies a three times within one expression, with no sequence point between the changes. The standard leaves the result undefined - not merely unspecified - so any answer a particular compiler gives is accidental. This is why such expressions must be split into separate statements.

Please log in or register to report this question.

25.

The precedence of arithmetic operators is (from highest to lowest)?

Answer: A

Correct answer: %, *, /, +, -

%, * and / sit at the same, higher level and associate left to right; + and - sit at the same, lower level. The listed order reflects that grouping. So in a + b * c the multiplication happens first, while in a % b * c the two are simply taken left to right.

Please log in or register to report this question.

26.

Which of the following is not an arithmetic operation?

Answer: D

Correct answer: a != 50;

!= is a relational operator: it compares and yields 1 or 0, and does not modify a at all. The other three are compound assignment operators - a *= 20 means a = a * 20, and likewise for /= and %=.

Please log in or register to report this question.

27.

Which of the following data type will throw an error on modulus operation(%)?

Answer: C

Correct answer: float

% requires integer operands. char, short and int are all integer types and are promoted to int before the operation, so they are fine; float is not, and using it gives a compile-time error. Use fmod() for a floating-point remainder.

Please log in or register to report this question.

28.

What is the output of this C code?    

int main()
    {
        int a = 20;
        double b = 15.6;
        int c;
        c = a + b;
        printf("%d", c);
    }

 

Answer: A

Correct answer: 35

a + b promotes the int to double, giving \(20 + 15.6 = 35.6\). Assigning that to an int truncates toward zero rather than rounding, so c becomes 35. Rounding would have given 36 - the distractor.

Please log in or register to report this question.

29.

What is the output of this C code?    

int main()
    {
        int a = 20, b = 15, c = 5;
        int d;
        d = a == (b + c);
        printf("%d", d);
    }

 

Answer: A

Correct answer: 1

= has lower precedence than ==, so the statement is d = (a == (b + c)). Since \(15 + 5 = 20\) and a is 20, the comparison is true, and a true relational expression in C has the value 1.

Please log in or register to report this question.

30.

What is the output of this C code?    

void main()
    {
        int x = 0;
        if (x = 0)
            printf("Its zero\n");
        else
            printf("Its not zero\n");
    }

 

Answer: A

Correct answer: Its not zero

if (x = 0) uses the assignment operator, not ==. It assigns 0 to x, and the value of the assignment expression is that same 0, which is false. So the else branch runs and it prints Its not zero - the classic typo that compilers now warn about.

Please log in or register to report this question.

Loading…

Other Subjects

Tags: Expressions Questions and Answers || Expressions MCQ Questions and Answers || Expressions GK Questions and Answers || Expressions GK MCQ Questions || Expressions Multiple Choice Questions and Answers || Expressions GK || GK on Expressions || C Programming Questions and Answers || C Programming MCQ Questions and Answers || C Programming GK Questions and Answers || GK on C Programming