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

Expressions Questions and Answers

61.

What is the difference between the following 2 codes?

//Program 1
    int main()
    {
        int d, a = 1, b = 2;
        d =  a++ + ++b;
        printf("%d %d %d", d, a, b);
    }

 //Program 2
    int main()
    {
        int d, a = 1, b = 2;
        d =  a++ + ++b;
        printf("%d %d %d", d, a, b);
    }

 

Answer: A

Correct answer: No difference as space doesn’t make any difference, values of a, b, d are same in both the case

The two programs are character for character the same, so naturally they produce the same values: a++ contributes 1 and leaves a as 2, ++b makes b 3 and contributes 3, so d is 4 and the output is 4 2 3. The point the question is driving at is that whitespace between tokens is insignificant - though it is worth knowing the limit of that rule: a+++++b would not compile, because the compiler takes the longest possible token at each step and reads it as a++ ++ +b.

Please log in or register to report this question.

62.

What is the output of this C code?   

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

 

Answer: B

Correct answer: a = 2, b = 1

c = a++ + b; uses the old value of a, so c becomes 2, and the side effect then makes a equal to 2. b is only read, so it stays 1. The printed values are those of a and b, not of c.

Please log in or register to report this question.

63.

What is the output of this C code?

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

 

Answer: A

Correct answer: 15, 4, 5

++a + ++a + a++ modifies a three times in one expression, and the three printf arguments may be evaluated in any order - the standard does not fix it. 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. The lesson to take away is not the number but the rule: one variable, one modification per expression.

Please log in or register to report this question.

64.

For which of the following, “PI++;” code will fail?

Answer: A

Correct answer: #define PI 3.14

A #define is textual substitution, so PI++; expands to 3.14++; - and a constant is not an lvalue, so it cannot be incremented. The other two are variables: float PI can be incremented arithmetically, and char *PI can be incremented as a pointer.

Please log in or register to report this question.

65.

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.

66.

What is the output of this C code?    

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

 

Answer: A

Correct answer: 1

With i starting at 0, i++ contributes 0 and leaves i as 1, and the second operand then reads 1, giving \(0 + 1 = 1\). Strictly this expression reads i in one operand while modifying it in the other with no sequence point between, which the standard leaves undefined; 1 is what mainstream compilers produce, but the expression should be split into two statements.

Please log in or register to report this question.

67.

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.

68.

Comment on the output of this C code?    

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

 

Answer: A

Correct answer: = operator is not a sequence point

The point of the example is that = does not introduce a sequence point. In i = i++ + i; the compiler is free to order the read of i, the increment, and the store in any way it likes, so the result is undefined. Sequence points in C come from &&, ||, the comma operator, the conditional ?:, and the end of a full expression - not from assignment.

Please log in or register to report this question.

69.

What is the output of this C code?   

int main()
    {
        int i = 0;
        int x = i++, y = ++i;
        printf("%d % d\n", x, y);
        return 0;
    }

 

Answer: A

Correct answer: 0, 2

The comma separating two declarators is a sequence point, so the two initialisations happen strictly in order and nothing here is undefined. x = i++ takes the old value 0 and leaves i at 1; then y = ++i makes i 2 and gives 2.

Please log in or register to report this question.

70.

What is the output of this C code?    

int main()
    {
        int i = 10;
        int *p = &i;
        printf("%d\n", *p++);
    }

 

Answer: A

Correct answer: 10

++ binds tighter than *, but it is a post-increment, so *p++ means “dereference the current p, then advance p”. The value printed is therefore i itself, 10; the pointer moves on afterwards, to an address that is no longer valid.

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