ProgrammingModerateProduct-company placement · Java

What is printed?

What is printed?
int i = 0;
while (true) {
    try { i++; if (i == 3) break; }
    finally { System.out.print(i); }
}
  1. A. 123
  2. B. 12
  3. C. 1233
  4. D. Infinite loop

Correct answer: A. 123

finally runs on every iteration, including the one that leaves the loop with break, so 1, 2 and 3 are all printed.

More OOP with C++ and Java questions

Exams that ask this topic