ProgrammingHardProduct-company MCQ pattern

Assume 32-bit int. What is printed?

Assume 32-bit int. What is printed?
int a = 5;
int b = a > 0 ? 1 : a < 0 ? -1 : 0;
int c = -5;
int d = c > 0 ? 1 : c < 0 ? -1 : 0;
printf("%d %d", b, d);
  1. A. 1 -1
  2. B. -1 1
  3. C. 0 0
  4. D. 1 1

Correct answer: A. 1 -1

This is the classic sign() function via nested, right-associative ternaries. For a=5 (positive): a>0 is true, b=1. For c=-5 (negative): c>0 is false, so evaluate c<0?-1:0; c<0 is true, so d=-1.

More C Programming questions

Exams that ask this topic