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

Expressions Questions and Answers

51.

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.

52.

What is the output of this C code?  

void main()
    {
        int x = 4, y, z;
        y = --x;
        z = x--;
        printf("%d%d%d", x, y, z);
    }

 

Answer: D

Correct answer: 2 3 3

Follow the two statements:

  • y = --x; - pre-decrement, so x becomes 3 and y takes that new value, 3.
  • z = x--; - post-decrement, so z takes the old value 3 and x then becomes 2.

Printing x, y, z gives 2 3 3.

Please log in or register to report this question.

53.

What is the output of this C code?    

void main()
    {
        char a = 'a';
        int x = (a++)++;
        printf("%d\n", x);
    }

 

Answer: C

Correct answer: Compile time error

The result of a++ is a value, not an object, so it is not an lvalue - and ++ requires a modifiable lvalue to operate on. (a++)++ therefore fails to compile. The same reasoning rules out (a + 1)++ or 5++. A single (a)++ would have been fine, leaving x equal to 97 and a equal to 'b'.

Please log in or register to report this question.

54.

What is the output of the following program?

#include <stdio.h>
int main()
{
int max =123, min = 10, *maxptr = &max, *minptr = &min;
int **nptr = &minptr, **mptr = &maxptr;
*maxptr = ++**mptr % **nptr;
max -= ( *minptr - **nptr && *maxptr || *minptr);
printf( " %d %d", ++**mptr, *minptr);
return 0;
}

 

Answer: A

Correct answer: 4 10

Work through the pointers. maxptr points at max, minptr at min, and mptr and nptr point at those two pointers, so **mptr is max and **nptr is min.

  1. *maxptr = ++**mptr % **nptr; - ++**mptr takes max to 124, and \(124 \bmod 10 = 4\), so max becomes 4.
  2. max -= (*minptr - **nptr && *maxptr || *minptr); - 10 - 10 is 0, so the && is 0; 0 || 10 is 1. So max becomes 3.
  3. printf("%d %d", ++**mptr, *minptr); - the increment takes max back to 4, and min was never touched, so it is 10.

Output: 4 10.

Please log in or register to report this question.

55.

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.

56.

What is the output of this C code?    

int main()
    {
        int y = 0;
        if (1 |(y = 1))
            printf("y is %d\n", y);
        else
            printf("%d\n", y);
 
    }

 

Answer: A

Correct answer: 1

| is the bitwise OR, and unlike || it does not short-circuit - both operands are always evaluated. So y = 1 runs, and the condition 1 | 1 is 1, which is true. The if branch prints y is 1.

Please log in or register to report this question.

57.

What is the output of this C code?
 

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

 

Answer: B

Correct answer: 1

Both operands are int, so this is integer division: the fractional part is discarded rather than rounded. \(7 / 4 = 1.75\) truncates to 1. Writing 7 / 4.0 would force floating-point division and give 1.75.

Please log in or register to report this question.

58.

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.

59.

What value will be stored in z if the following code is executed?

main()
{
    int x = 5, y = -10, z;
    int a = 4, b = 2;

    z = x+++++y * b/a;
}

Answer: C

Correct answer: Compilation error

The expression z = x+++++y * b/a; does not compile, and the reason is the rule the C standard uses to split a line into tokens - the maximal munch rule. The scanner takes the longest sequence of characters that could form a valid token at each step, and it does so without looking ahead to see whether the result will parse. Faced with +++++, it therefore produces ++, then ++, then +, giving ((x++) ++) + y. That is where it fails: x++ is an rvalue - a value, not an object - and the increment operator requires a modifiable lvalue as its operand. GCC reports lvalue required as increment operand. The fix a programmer would apply is whitespace, since the scanner respects it: written as x++ + ++y * b/a the expression is legal, and it evaluates to \[ 5 + \frac{(-9) \times 2}{4} = 5 + \frac{-18}{4} = 5 + (-4) = 1, \] because integer division in C truncates towards zero. So even under the grouping the question intends, the value is 1 rather than the 2 this record originally offered; as written, the code never gets that far. The declaration line also carried a stray semicolon, leaving y undeclared, and has been corrected.

Please log in or register to report this question.

60.

What is the output of this C code?

void main()
    {
        int x = 1, y = 0, z = 5;
        int a = x && y && z++;
        printf("%d", z);
    }

 

Answer: B

Correct answer: 5

x && y is false, and once the left operand of && is false the result is settled, so the right operand is never evaluated. z++ does not run and z stays 5. This is short-circuiting, and it is guaranteed by the standard rather than being a compiler optimisation.

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