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

Expressions Questions and Answers

11.

What is the output of this C code?  

int main()
    {
        int a = 10;
        if (a == a--)
            printf("TRUE 1\t");
        a = 10;
        if (a == --a)
            printf("TRUE 2\t");
    }

 

Answer: C

Correct answer: TRUE 1 TRUE 2

Both comparisons read a in one operand while modifying it in the other, with no sequence point between, so both are formally undefined behaviour. 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. What the question is really illustrating is that a == a-- and a == --a cannot be reasoned about at all - not that one is true and the other false.

Please log in or register to report this question.

12.

What is the output of this C code?    

int main()
    {
        int i = 1;
        if (i++ && (i == 1))
            printf("Yes\n");
        else
            printf("No\n");
    }

 

Answer: B

Correct answer: No

i++ yields the old value 1, which is true, so the right operand is evaluated - but by then the side effect has happened and i is 2. i == 1 is therefore false, the whole condition is false, and the program prints No. The && is a sequence point, so the increment is guaranteed complete before the comparison.

Please log in or register to report this question.

13.

A function cannot be defined inside another function

Answer: A

Correct answer: True

C has no nested functions: every function is defined at file scope, and a function definition inside another is a syntax error. (GCC offers nested functions as a non-standard extension, but that is not C.) What can be nested is a block, and a block may declare its own variables.

Please log in or register to report this question.

14.

What is the output of this C code?    

int main()
    {
        int x = 1, y = 0, z = 3;
        x > y ? printf("%d", z) : return z;
    }

 

Answer: C

Correct answer: Compile time error

The conditional operator needs expressions in both branches, and return z is a statement. x > y ? printf("%d", z) : return z; is therefore a syntax error. Written properly it would be if (x > y) printf("%d", z); else return z;.

Please log in or register to report this question.

15.

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.

16.

What is the output of this C code?    

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

 

Answer: D

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.

17.

What will be the output of the following program?

#include <stdio.h>

void fun();
int main()
{
fun();
return 0;
}
void fun()
{
auto int i = 1;
register char a = 'D';
static int p = 0;
printf("%d %d %ld", i, a, p);
}

 

Answer: D

Correct answer: 1 68 0

Each declaration in fun behaves as its storage class says:

  • auto int i = 1; prints as 1.
  • register char a = 'D'; is printed with %d, not %c, so it comes out as the character’s code - 68 in ASCII.
  • static int p = 0; prints as 0.

So the output is 1 68 0. The trap is the format specifier: a char is an integer type, and %d shows its numeric value.

Please log in or register to report this question.

18.

What is the output of this C code?

int main()
    {
        int i = 5;
        int l = i / -4;
        int k = i % -4;
        printf("%d %d\n", l, k);
        return 0;
    }

 

Answer: B

Correct answer: -1 1

Integer division truncates toward zero, so \(5 / (-4) = -1\) rather than \(-2\). The remainder then follows from \((a/b) \times b + a\%b = a\):

\[ (-1) \times (-4) + r = 5 \;\Rightarrow\; r = 1 \]

The remainder takes the sign of the dividend, which is positive here, so the output is -1 1.

Please log in or register to report this question.

19.

What is the output of this C code?

int main()
    {
        int i = 2;
        int j = ++i + i;
        printf("%d\n", j);
    }

 

Answer: A

Correct answer: 6

++i takes i to 3 and contributes 3, and the second operand then reads 3, giving \(3 + 3 = 6\). As with any expression that both modifies and reads the same variable without an intervening sequence point, this is formally undefined behaviour; 6 is simply what the common compilers give.

Please log in or register to report this question.

20.

What is the output of this C code?

void main()
    {
        int y = 3;
        int x = 7 % 4 * 3 / 2;
        printf("Value of x is %d", x);
    }

 

Answer: A

Correct answer: Value of x is 4

%, * and / all have the same precedence and associate left to right, so the expression is grouped as ((7 % 4) * 3) / 2:

\[ 7 \bmod 4 = 3, \quad 3 \times 3 = 9, \quad 9 / 2 = 4 \]

The final division is integer division, so the 0.5 is discarded and x is 4. The declaration of y is never used.

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