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

Expressions Questions and Answers

41.

What is the output of this C code  

void main()
    {
        unsigned int x = -5;
        printf("%d", x);
    }

 

Answer: C

Correct answer: -5

The bits assigned to x are the two’s-complement pattern of \(-5\); storing them in an unsigned int changes the interpretation, not the bits. %d then asks printf to read those same bits back as a signed int, so -5 is what appears. Using %u would print 4294967291 on a 32-bit int.

Please log in or register to report this question.

42.

What is the output of this C code?    

void main()
    {
        int x = 0, y = 2, z = 3;
        int a = x & y | z;
        printf("%d", a);
    }

 

Answer: A

Correct answer: 3

The bitwise operators have different precedences: & binds tighter than |, so the expression is (x & y) | z:

\[ (0 \;\&\; 2) = 0, \quad 0 \;|\; 3 = 3 \]

So a is 3. Note this differs from the logical operators only in that no short-circuiting occurs.

Please log in or register to report this question.

43.

What is the output of this C code?    

int main()
    {
        int x = 1, y = 0;
        x &&= y;
        printf("%d\n", x);
    }

 

Answer: A

Correct answer: Compile time error

There is no &&= operator in C. The compound assignment operators are += -= *= /= %= &= |= ^= <<= >>= - the bitwise &= exists, but the logical && has no assignment form. The program fails to compile.

Please log in or register to report this question.

44.

Operation “a = a * b + a” can also be written as:

Answer: D

Correct answer: All of the mentioned

All three compute \(a \times b + a\).

  • a *= b + 1; is a = a * (b + 1) = a b + a.
  • a = (b + 1) * a; is the same product written the other way round.
  • (c = a * b) != (a = c + a); stores \(ab\) in c and then \(c + a\) in a.

The third form is worth a warning: != does not sequence its operands, so relying on c being set before it is read is not something real code should do.

Please log in or register to report this question.

45.

What is the output of this C code?   

 void main()
    {
        int x = 4;
        int *p = &x;
        int *k = p++;
        int r = p - k;
        printf("%d", r);
    }

 

Answer: C

Correct answer: 1

Pointer subtraction returns the number of elements between the two pointers, not the number of bytes. k holds the value p had before the increment, and p++ moved p on by one int, so p - k is 1. The compiler divides the 4-byte gap by sizeof(int) for you.

Please log in or register to report this question.

46.

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.

47.

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.

48.

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.

49.

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.

50.

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.

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