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

Expressions Questions and Answers

111.

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.

112.

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.

115.

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.

116.

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.

117.

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.

118.

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.

119.

What is the output of this C code?   

 int main()
    {
        int a = 2;
        if (a >> 1)
           printf("%d\n", a);
    }

 

Answer: C

Correct answer: 2

a >> 1 shifts 2 (binary 10) right by one place, giving 1. That is non-zero, so the condition is true and the body runs, printing the value of a - which the shift never changed, since >> yields a new value rather than modifying its operand. The output is 2.

Please log in or register to report this question.

120.

What is the output of this C code?   

 int main()
    {
        if (7 & 8)
        printf("Honesty");
            if ((~7 & 0x000f) == 8)
                printf("is the best policy\n");
    }

 

Answer: C

Correct answer: is the best policy

Take the two conditions separately.

  • 7 & 8: \(0111 \;\&\; 1000 = 0000\). They share no set bit, so the value is 0 and Honesty is not printed.
  • ~7 & 0x000f: \(\sim 0111 = \ldots 11111000\); masking with \(1111\) leaves \(1000 = 8\), which does equal 8.

So only the second printf runs, and the output is is the best policy.

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