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

Expressions Questions and Answers

81.

What is the final value of j in the below code?

int main()
    {
        int i = 0, j = 0;
        if (i && (j = i + 10))
            //do something
            ;
    }

 

Answer: D

Correct answer: 0

&& short-circuits: since i is 0 the whole condition is already false, so the right operand (j = i + 10) is never evaluated and the assignment never happens. j keeps its initial value, 0. This guarantee is what makes idioms such as if (p && p->next) safe.

Please log in or register to report this question.

82.

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.

84.

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.

Answer: B

Correct answer: 0 or 1

A relational or logical expression in C yields an int that is exactly 1 for true and 0 for false. That is stricter than the rule for testing a value, where any non-zero counts as true. So (3 > 2) is 1, never 3 and never some other positive number.

Please log in or register to report this question.

86.

What will be the value of d in the following program?    

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

 

Answer: B

Correct answer: 1

== binds tighter than =, so the statement is d = ((b + c) == a). Since \(5 + 5 = 10\) equals a, the comparison is true and yields 1. Had = bound tighter the code would not even compile, since d = b + c is not something you can compare in that position.

Please log in or register to report this question.

87.

What is the output of this C code?    

int main()
    {
        int a = 10, b = 5, c = 3;
        b != !a;
        c = !!a;
        printf("%d\t%d", b, c);
    }

 

Answer: A

Correct answer: 5 1

b != !a; is an expression statement whose value is computed and thrown away - it does not assign anything, so b remains 5. c = !!a applies logical NOT twice: !10 is 0 and !0 is 1, so the double negation reduces any non-zero value to 1. That idiom is the usual way of normalising a value to 0 or 1.

Please log in or register to report this question.

88.

Which among the following is NOT a logical or relational operator?

Answer: D

Correct answer: =

= is the assignment operator: it stores a value rather than comparing or combining truth values. != and == are relational, and || is logical. Mistaking = for == in a condition is the classic C bug.

Please log in or register to report this question.

89.

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.

90.

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.

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