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

Expressions Questions and Answers

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.

102.

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.

103.

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.

104.

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.

105.

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.

106.

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.

107.

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.

108.

Which of the following is not a compound assignment operator?

Answer: D

Correct answer: ==

== is the equality operator: it compares and yields 1 or 0, and assigns nothing. The other three are compound assignments - a /= b means a = a / b, and likewise for += and %=.

Please log in or register to report this question.

109.

What is the output of this C code?   

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

 

Answer: D

Correct answer: 6

x *= x + y; means x = x * (x + y) - the whole right-hand side is evaluated first, because compound assignment binds more loosely than +:

\[ x = 2 \times (2 + 1) = 6 \]

There is exactly one modification of x, so nothing here is undefined.

Please log in or register to report this question.

110.

What will be the storage class of variable I in the code written below?

#include <stdio.h>

int main()
{
int i = 10;
printf("%d", i);
return 0;
}

 

Answer: A

Correct answer: Automatic storage class

A variable declared inside a function with no storage class specifier defaults to auto: it is created on the stack when the block is entered, holds junk unless initialised, and is destroyed on exit. The keyword auto itself is almost never written, precisely because it is the default.

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