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

Expressions Questions and Answers

21.

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.

22.

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.

23.

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.

24.

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.

25.

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.

26.

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.

27.

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.

28.

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.

29.

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.

30.

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.

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