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

Expressions Questions and Answers

101.

What is the output of this C code?

 void main()
    {
        int a = 5, b = -7, c = 0, d;
        d = ++a && ++b || ++c;
        printf("\n%d%d%d%d", a, b, c, d);
    }

 

Answer: D

Correct answer: 6 -6 0 1

Evaluate left to right, remembering that any non-zero value is true.

  • ++a makes a 6, which is true.
  • ++b makes b -6 - non-zero, so also true.
  • The && is therefore true, so || short-circuits and ++c never runs: c stays 0.
  • d gets the value of the whole expression, 1.

Output: 6 -6 0 1.

Please log in or register to report this question.

102.

What is the correct and fully portable way to obtain the most significant byte of an unsigned integer x?

Answer: D

Correct answer: x > > ( CHAR_BIT * (sizeof(int) - 1))

The most significant byte must be shifted down by the width of all the bytes below it. The portable way to say that is CHAR_BIT * (sizeof(int) - 1) - the number of bits in a byte times the number of bytes below the top one.

x >> 24 assumes a 4-byte int and an 8-bit char. x & 0xFF00 masks the second byte, not the top one, and does not shift it down. The variant with sizeof(int) - 3 is simply the wrong count.

Please log in or register to report this question.

Answer: B

Correct answer: The local variable is given a priority.

The inner declaration shadows the outer one: within the block where the local is declared, the name refers to the local, and the global is simply not visible. It is not an error, and it does not depend on declaration order. The global can still be reached in C++ with ::name, but plain C offers no such escape.

Please log in or register to report this question.

104.

What is the output of this C code?   

 int main()
    {
        int x = 2, y = 2;
        x /= x / y;
        printf("%d\n", x);
        return 0;
    }

 

Answer: A

Correct answer: 2

x /= x / y; is x = x / (x / y). The right-hand side is computed first, with integer division throughout:

\[ x / y = 2 / 2 = 1, \quad x = 2 / 1 = 2 \]

So x is unchanged at 2.

Please log in or register to report this question.

105.

What is the output of this C code?    

void main()
    {
        int x = 1, y = 0, z = 5;
        int a = x && y || z++;
        printf("%d", z);
    }

 

Answer: A

Correct answer: 6

&& binds tighter than ||, so the expression is (x && y) || z++. With y zero the left side is false, so || must evaluate its right operand to decide the result - z++ runs and z becomes 6.

Please log in or register to report this question.

106.

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.

107.

What is the output of this C code?    

void main()
    {
        int x = 97;
        int y = sizeof(x++);
        printf("X is %d", x);
    }

 

Answer: A

Correct answer: x is 97

sizeof is evaluated at compile time from the type of its operand, and its operand is not executed. So x++ never runs, y becomes sizeof(int), and x is still 97. (Variable-length arrays are the one exception where the operand is evaluated.)

Please log in or register to report this question.

108.

What is the output of this C code? 

   int main()
    {
        if (~0 == 1)
            printf("yes\n");
        else
            printf("no\n");
    }

 

Answer: B

Correct answer: no

~0 flips every bit of zero, giving all ones - which in two’s complement is -1, not 1. So the comparison ~0 == 1 is false and the else branch prints no. Confusing ~ (bitwise NOT) with ! (logical NOT) is what the question is testing.

Please log in or register to report this question.

109.

Which of the following statement is correct about the code snippet given below?

#include <stdio.h>
int main()
{
float z = 12.35, c = 10;
if( ++z -z)
c += z;
else
c -= z;
printf( "%f %f", z, c);
return 0;
}

 

Answer: A

Correct answer: The program will result in compile time error

The statement is written c - = z; with a space between the two characters. C reads - and = as two separate tokens there, so the line parses as c - (= z), which is a syntax error. A compound assignment operator has to be written as one token: c -= z;.

Please log in or register to report this question.

110.

What is the output of this C code?    

int main()
    {
        int i = 0;
        int j = i++ + i;
        printf("%d\n", j);
    }

 

Answer: A

Correct answer: 1

With i starting at 0, i++ contributes 0 and leaves i as 1, and the second operand then reads 1, giving \(0 + 1 = 1\). Strictly this expression reads i in one operand while modifying it in the other with no sequence point between, which the standard leaves undefined; 1 is what mainstream compilers produce, but the expression should be split into two statements.

Please log in or register to report this question.

Loading…
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