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

Expressions Questions and Answers

61.

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: B

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.

62.

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.

63.

What is the value of x after executing the following statement?

int x = 011 | 0x10;

Answer: C

Correct answer: 25

The two literals are in different bases: a leading 0 marks octal and 0x marks hexadecimal.

\[ 011_8 = 9 = 01001_2, \quad 0x10 = 16 = 10000_2 \]

The bitwise OR sets every bit that is set in either, giving \(11001_2 = 25\).

Please log in or register to report this question.

64.

What is the output of this C code?    

void main()
    {
        int a = 5, b = -7, c = 0, d;
        d = ++a && ++b || ++c;
        printf("\n%d%d%d%d", a,  b, c, d);
    }

 

Answer: D

Correct answer: 6 -6 0 1

Evaluate left to right, remembering that any non-zero value is true.

  • ++a makes a 6, which is true.
  • ++b makes b -6 - non-zero, so also true.
  • The && is therefore true, so || short-circuits and ++c never runs: c stays 0.
  • d gets the value of the whole expression, 1.

Output: 6 -6 0 1.

Please log in or register to report this question.

65.

Which is not a storage class?

Answer: B

Correct answer: Struct

The storage classes in C are auto, register, static, extern and typedef - typedef being grouped with them by the grammar even though it names a type rather than allocating storage. struct is not among them: it introduces a type, and a structure variable still needs a storage class of its own.

Please log in or register to report this question.

66.

For the following statement find the values generated for p and q?

int p = 0, q = 1;
p = q++;
p = ++q;
p = q--;
p = --q;

Value of p & q are

Answer: A

Correct answer: 1 1

Follow the four statements from p = 0, q = 1:

  • p = q++; - p gets the old 1; q becomes 2.
  • p = ++q; - q becomes 3 and p gets 3.
  • p = q--; - p gets the old 3; q becomes 2.
  • p = --q; - q becomes 1 and p gets 1.

So both end at 1.

Please log in or register to report this question.

67.

What is the output of this C code?    

void main()
    {
        int a = -5;
        int k = (a++, ++a);
        printf("%d\n", k);
    }

 

Answer: A

Correct answer: -3

The comma operator evaluates its left operand, discards the result, then evaluates the right operand and yields that. So a++ takes a to -4, then ++a takes it to -3 and produces -3. k is therefore -3. There is a sequence point at the comma, so this is well defined.

Please log in or register to report this question.

68.

What is the output of this C code? 

   int main()
    {
        int x = -2;
        x = x >> 1;
        printf("%d\n", x);
    }

 

Answer: B

Correct answer: -1

-2 is all ones followed by a zero: \(\ldots 1110\). Shifting right on a signed negative value is an arithmetic shift in every mainstream implementation, so the sign bit is copied in from the left and the result is \(\ldots 1111 = -1\). Arithmetically it is a division by two, rounding toward negative infinity.

Please log in or register to report this question.

69.

What is the output of this C code?

void main()
    {
        int x = 4.3 % 2;
        printf("Value of x is %d", x);
    }

 

Answer: D

Correct answer: Compile time error

The % operator is defined only for integer operands. 4.3 is a double, so 4.3 % 2 is rejected at compile time - there is no implicit conversion to rescue it. For a floating-point remainder you would call fmod(4.3, 2) from <math.h>.

Please log in or register to report this question.

70.

What is the type of the below assignment expression if x is of type float, y is of type int?
        y = x + y;

Answer: A

Correct answer: int

The type of an assignment expression is the type of its left operand, after the right-hand side has been converted to it. Here x + y is computed as a float, then converted to int for the assignment to y - so the expression y = x + y has type int, and the fractional part is lost.

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