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

Expressions Questions and Answers

51.

What is the value of x in this C code?    

void main()
    {
        int x = 4 *5 / 2 + 9;
    }

 

Answer: C

Correct answer: 19

* and / bind tighter than + and, having equal precedence, associate left to right:

\[ 4 \times 5 = 20, \quad 20 / 2 = 10, \quad 10 + 9 = 19 \]

Every step is integer arithmetic, so the answer is exactly 19.

Please log in or register to report this question.

52.

What will be the output of the following code?

#include <stdio.h>

int main()
{
extern int a;
static char j = 'E';
printf("%c %d", ++j, ++a);
return 0;
}

 

Answer: B

Correct answer: F 1

++j takes the static char from 'E' to 'F', which %c prints as the letter. extern int a; declares a variable defined at file scope elsewhere; such an object has static storage duration and is therefore zero-initialised, so ++a gives 1. Output: F 1.

Please log in or register to report this question.

53.

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.

54.

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.

55.

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.

56.

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.

57.

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.

58.

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.

59.

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.

60.

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.

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