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

Expressions Questions and Answers

91.

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.

92.

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.

93.

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.

94.

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.

95.

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.

96.

What is the output of this C code?

int main()
    {
        int a = 1, b = 1, d = 1;
        printf("%d, %d, %d", ++a + ++a+a++, a++ + ++b, ++d + d++ + a++);
    }

 

Answer: A

Correct answer: 15, 4, 5

++a + ++a + a++ modifies a three times in one expression, and the three printf arguments may be evaluated in any order - the standard does not fix it. The expression modifies a variable more than once, and reads it as well, with no sequence point in between. The C standard makes that undefined behaviour, so the answer listed is what one common compiler happens to produce - another compiler, or another optimisation level, may give something else, and no program should depend on it. The lesson to take away is not the number but the rule: one variable, one modification per expression.

Please log in or register to report this question.

97.

What will be the output of the following code snippet?
 

Y = 5;
if (! Y > 10)
X = Y + 3;
else
X = Y + 10;

printf(" X = %d Y = %d", X, Y);

 

Answer: A

Correct answer: The program will print X = 15 Y = 5

! binds far tighter than >, so the condition is (!Y) > 10, not !(Y > 10). With Y equal to 5, !Y is 0, and \(0 > 10\) is false. The else branch runs, giving \(X = 5 + 10 = 15\), and Y is never modified, so the output is X = 15 Y = 5.

Please log in or register to report this question.

98.

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

num = 5;
printf( "%d", ++num++ );

 

Answer: C

Correct answer: The code will result in L – value required

++num++ groups as ++(num++), because postfix binds tighter than prefix. The result of num++ is a value, not an object, so the prefix ++ has nothing it can modify - the compiler reports that an lvalue is required. The same applies to ++(a + b) or 5++.

Please log in or register to report this question.

99.

What is the output of this C code?    

int main()
    {
        unsigned int a = 10;
        a = ~a;
        printf("%d\n", a);
    }

 

Answer: C

Correct answer: -11

~ flips every bit, and for a two’s-complement integer that is exactly \(\sim a = -a - 1\):

\[ \sim 10 = -10 - 1 = -11 \]

The variable is unsigned, so it actually holds 4294967285, but %d reads those bits back as a signed int and prints -11.

Please log in or register to report this question.

Answer: B

Correct answer: For loop counter

register is worth asking for where a variable is accessed very often, and a loop counter is the classic case - it is read and written on every iteration. It cannot be used where the variable’s address is needed, since a register has no address. In practice modern compilers allocate registers better than the hint does, so the keyword is now of historical interest.

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