ProgrammingVery hardProduct company interview · GATE level

What is printed on a typical platform (16-bit short, 32-bit int)?

What is printed on a typical platform (16-bit short, 32-bit int)?
int i = -1;
unsigned int u = 1;
short s = -1;
unsigned short us = 1;
cout << (i < u) << (s < us) << (i < (int)u)
     << (sizeof(int) * -1 > 0);
  1. A. 0101
  2. B. 1111
  3. C. 0011
  4. D. 0111

Correct answer: D. 0111

i < u: in a comparison of int with unsigned int, the int is converted to unsigned, so -1 becomes 4294967295 and the result is false (0). s < us: both short types are first promoted to int (int can hold every unsigned short value), so -1 < 1 is true (1). i < (int)u compares two ints: true. sizeof yields an unsigned size_t, so sizeof(int) * -1 converts -1 to a huge unsigned value and the product is positive: true.

More OOP with C++ and Java questions

Exams that ask this topic