ProgrammingHardInfosys · Cognizant GenC · Capgemini

Consider this Java-style code. int a = 5, b = 0, c = 0; if (a > 3 || ++b > 0) c += 1; if (a < 3 && ++b > 0) c += 10; if ((a > 3) | (++b > 0)) c += 100; System.out.println(b + " " + c); What is printed?

  1. A. 1 101
  2. B. 0 101
  3. C. 2 101
  4. D. 3 111

Correct answer: A. 1 101

|| short-circuits (a > 3 is true) so b stays 0 and c becomes 1. && short-circuits (a < 3 is false), no change. The non-short-circuit | evaluates ++b, making b = 1, and the condition is true, so c = 101.

More Pseudocode and Code Tracing questions

Exams that ask this topic