ProgrammingModerateAmazon/Microsoft SDE MCQ · Java placement

What is printed?

What is printed?
int i = 0, j = 0;
boolean p = (i++ > 0) & (i++ > 0);
boolean q = (j++ > 0) && (j++ > 0);
System.out.print(i + " " + j + " " + p + " " + q);
  1. A. 1 1 false false
  2. B. 2 2 false false
  3. C. 2 1 false false
  4. D. 2 1 true false

Correct answer: C. 2 1 false false

& on booleans evaluates both operands, so i is incremented twice; && stops after the first false operand, so j is incremented once. Both first comparisons are 0 > 0, false, so p and q are false.

More OOP with C++ and Java questions

Exams that ask this topic