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

Expressions Questions and Answers

11.

What is the value of the below assignment expression
      (x = foo()) in the statement (x = foo()) != 1, considering that foo() returns 2?

Answer: A

Correct answer: 2

The value of an assignment expression is the value assigned. foo() returns 2, so x becomes 2 and the expression (x = foo()) itself has the value 2. That value is then what the surrounding != 1 compares against - and the comparison, being a separate expression, would yield 1.

Please log in or register to report this question.

12.

What will be the output of the following program?
 

#include <stdio.h>
int main()
{
int num = 0, z = 3;
if ( ! (num <= 0) || ++z )
printf( "%d %d ", ++num + z++, ++z );
else
printf( "%d %d", --num + z--, --z);
return 0;
}

 

Answer: B

Correct answer: 6 5

num is 0, so num <= 0 is true and !(num <= 0) is 0. The || must therefore evaluate its right operand: ++z takes z to 4 and is non-zero, so the if branch runs.

In printf("%d %d ", ++num + z++, ++z) the arguments are evaluated right to left on the usual compilers: ++z makes z 5 and yields 5; then ++num gives 1 and z++ yields 5, so the first argument is 6. Output: 6 5. Note that the order of argument evaluation is unspecified, so this particular pairing is compiler-dependent.

Please log in or register to report this question.

13.

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.

14.

What will be the output of the following program?

#include <stdio.h>

int main()
{
static unsigned int a = 23;
register unsigned char c = 'R';
auto long unsigned q = 345L;
static long signed p = 345L;
printf("a = %u c = %c", a ,c);
printf("\nq = %ld p = %ld", q, p);
return 0;
}

 

Answer: A

Correct answer: a=23 c=R q = 345 p = 345

Each variable prints its own value, and the storage classes affect only lifetime and placement, not the values: a is 23, and c printed with %c shows the character R. The two long variables both hold 345 and print with %ld as 345. The only real trap is the case of the letters in the options - the format string writes a lower-case a.

Please log in or register to report this question.

15.

Relational operators cannot be used on:

Answer: A

Correct answer: structure

Relational operators work on scalars - arithmetic types and pointers - because comparison needs a single value to order. A struct is an aggregate of several members, so s1 < s2 has no meaning and is rejected; even == is not defined for structures, since padding bytes would make a byte-wise comparison unreliable. Comparing them means comparing their members one by one.

Please log in or register to report this question.

16.

Are logical operators in C evaluated with short-circuit evaluation?

Answer: A

Correct answer: True

Both && and || short-circuit, and the standard requires it rather than leaving it to the compiler. If the left operand of && is false, or of || is true, the result is already determined and the right operand is not evaluated at all - side effects in it simply do not happen.

Please log in or register to report this question.

17.

What is the output of this C code?    

int main()
    {
        int a = 10, b = 10;
        if (a = 5)
        b--;
        printf("%d, %d", a, b--);
    }

 

Answer: C

Correct answer: a = 5, b = 9

if (a = 5) assigns 5 to a, and the value of that assignment, 5, is non-zero and so true. The body runs, taking b from 10 to 9. The printf then shows a as 5 and, because b-- is a post-decrement, the value passed is the current 9 - the decrement to 8 happens after the value has been handed over, and nothing prints afterwards.

Please log in or register to report this question.

18.

Which of the following data type will throw an error on modulus operation(%)?

Answer: C

Correct answer: float

% requires integer operands. char, short and int are all integer types and are promoted to int before the operation, so they are fine; float is not, and using it gives a compile-time error. Use fmod() for a floating-point remainder.

Please log in or register to report this question.

19.

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.

20.

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.

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