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

Expressions Questions and Answers

111.

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.

112.

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.

113.

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.

114.

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.

115.

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.

Answer: B

Correct answer: Single machine instruction required for p++

p++ maps to a single increment instruction that updates p in place, whereas p + 1 computes a new value that then has to be stored somewhere. The saving is historical, though: any modern optimising compiler generates identical code for p = p + 1 and p++.

Please log in or register to report this question.

118.

Comment on the output of this C code?
   

int main()
    {
        int i, n, a = 4;
        scanf("%d", &n);
        for (i = 0; i < n; i++)
            a = a * 2;
    }

 

Answer: B

Correct answer: No output

The loop multiplies a by 2 on each of the n passes, so a ends as \(4 \times 2^n\). But the program never prints anything - there is no printf after the loop - so the computed value is simply discarded when main returns. No output.

Please log in or register to report this question.

119.

Where will the space be allocated for an automatic storage class variable?

Answer: C

Correct answer: In memory

An automatic variable lives on the stack, which is memory. The register class is the one that asks for a CPU register, and even that is only a request. A compiler may of course keep an automatic variable in a register as an optimisation, but the storage class itself specifies memory.

Please log in or register to report this question.

120.

Which is executed quickly?
 

Answer: C

Correct answer: Both

On any modern compiler ++p and p++ compile to the same machine code when the result is discarded, as in a for loop’s third clause - the optimiser sees that the old value is unused. The historical preference for ++p matters for C++ iterators, where p++ must copy the object before incrementing it.

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