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

Expressions Questions and Answers

51.

What is the output of this C code?  

void main()
    {
        int x = 4, y, z;
        y = --x;
        z = x--;
        printf("%d%d%d", x, y, z);
    }

 

Answer: D

Correct answer: 2 3 3

Follow the two statements:

  • y = --x; - pre-decrement, so x becomes 3 and y takes that new value, 3.
  • z = x--; - post-decrement, so z takes the old value 3 and x then becomes 2.

Printing x, y, z gives 2 3 3.

Please log in or register to report this question.

52.

What is the output of this C code?   

 void main()
    {
        int x = 4;
        int *p = &x;
        int *k = p++;
        int r = p - k;
        printf("%d", r);
    }

 

Answer: C

Correct answer: 1

Pointer subtraction returns the number of elements between the two pointers, not the number of bytes. k holds the value p had before the increment, and p++ moved p on by one int, so p - k is 1. The compiler divides the 4-byte gap by sizeof(int) for you.

Please log in or register to report this question.

53.

What is the output of this C code?

 void main()
    {
        int a = 5, b = -7, c = 0, d;
        d = ++a && ++b || ++c;
        printf("\n%d%d%d%d", a, b, c, d);
    }

 

Answer: D

Correct answer: 6 -6 0 1

Evaluate left to right, remembering that any non-zero value is true.

  • ++a makes a 6, which is true.
  • ++b makes b -6 - non-zero, so also true.
  • The && is therefore true, so || short-circuits and ++c never runs: c stays 0.
  • d gets the value of the whole expression, 1.

Output: 6 -6 0 1.

Please log in or register to report this question.

54.

What is the output of this C code?    

void main()
    {
        int a = -5;
        int k = (a++, ++a);
        printf("%d\n", k);
    }

 

Answer: A

Correct answer: -3

The comma operator evaluates its left operand, discards the result, then evaluates the right operand and yields that. So a++ takes a to -4, then ++a takes it to -3 and produces -3. k is therefore -3. There is a sequence point at the comma, so this is well defined.

Please log in or register to report this question.

55.

What is the output of this C code?   

 int main()
    {
        int x = 2;
        x = x << 1;
        printf("%d\n", x);
    }

 

Answer: A

Correct answer: 4

<< shifts the bits left, and each place shifted left doubles the value:

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

So x becomes 4. Shifting a small positive int left is well defined, so the result does not depend on the compiler or the machine.

Please log in or register to report this question.

56.

What is the output of this C code? 

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

 

Answer: B

Correct answer: -1

-2 is all ones followed by a zero: \(\ldots 1110\). Shifting right on a signed negative value is an arithmetic shift in every mainstream implementation, so the sign bit is copied in from the left and the result is \(\ldots 1111 = -1\). Arithmetically it is a division by two, rounding toward negative infinity.

Please log in or register to report this question.

57.

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.

58.

What is the output of this C code?  

int main()
    {
        int x = -2;
        if (!0 == 1)
            printf("yes\n");
        else
            printf("no\n");
    }

 

Answer: A

Correct answer: yes

! is logical negation: it maps zero to 1 and any non-zero value to 0. So !0 is exactly 1, the comparison !0 == 1 holds, and the program prints yes. Contrast ~0, which flips bits and gives -1.

Please log in or register to report this question.

59.

What is the output of this C code?    

int main()
    {
        int y = 0;
        if (1 |(y = 1))
            printf("y is %d\n", y);
        else
            printf("%d\n", y);
 
    }

 

Answer: A

Correct answer: 1

| is the bitwise OR, and unlike || it does not short-circuit - both operands are always evaluated. So y = 1 runs, and the condition 1 | 1 is 1, which is true. The if branch prints y is 1.

Please log in or register to report this question.

60.

What is the output of this C code?    

int main()
    {
        int y = 1;
        if (y & (y = 2))
            printf("true %d\n");
        else
            printf("false %d\n");
 
    }

 

Answer: C

Correct answer: Either option a or option b

& does not short-circuit and does not order its operands, so y is read on the left and modified on the right with no sequence point between. The standard leaves that undefined, and a compiler may legitimately read y before or after the assignment - giving 1 & 2 = 0, or 2 & 2 = 2. Either branch may therefore be taken. (The printf calls also have a %d with no argument, which is a second fault.)

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