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

Expressions Questions and Answers

41.

for c = 2, value of c after c <<= 1;

Answer: D

Correct answer: c = 4;

<<= shifts left and assigns. Shifting left by one position doubles the value, because each bit moves to the next higher power of two:

\[ 2 = 10_2 \;\rightarrow\; 100_2 = 4 \]

So c becomes 4.

Please log in or register to report this question.

42.

What is the output of this C code?    

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

 

Answer: C

Correct answer: 2 1

Assignment is right-associative, so a += b -= a; groups as a += (b -= a). The inner assignment runs first: b = 2 - 1 = 1, and its value is 1. Then a += 1 gives a = 2. The output is 2 1.

Please log in or register to report this question.

43.

What is the output of this C code?   

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

 

Answer: C

Correct answer: Multiplication of a and n.

The loop runs n times and each pass adds a to result, so result ends up as \(a + a + \cdots + a = a \times n\) - repeated addition standing in for multiplication. (The scanf call is written scanf("%d", n) without the &, which is a bug in its own right; the question is about what the loop computes.)

Please log in or register to report this question.

44.

Which of the following is an invalid assignment operator?

Answer: D

Correct answer: None of the mentioned

All three are valid compound assignment operators: %= for remainder, /= for division and |= for bitwise OR. So none of them is invalid, and the answer is none of the mentioned.

Please log in or register to report this question.

45.

What is the output of this C code?    

int main()
    {
        int c = 2 ^ 3;
        printf("%d\n", c);
    }

 

Answer: A

Correct answer: 1

^ is bitwise exclusive OR, not exponentiation - a common trap for anyone coming from a language where ^ means “to the power of”:

\[ 2 = 010_2, \quad 3 = 011_2, \quad 010 \oplus 011 = 001_2 = 1 \]

So c is 1, not 8.

Please log in or register to report this question.

46.

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.

47.

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.

48.

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.

49.

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.

50.

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.

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