ProgrammingHardProduct-company placement · Java

What is printed?

What is printed?
int[] c = {0};
Runnable r = () -> c[0]++;
r.run(); r.run();
System.out.print(c[0]);
  1. A. 0
  2. B. 2
  3. C. 1
  4. D. Compile-time error: c is modified in a lambda

Correct answer: B. 2

The captured variable c (the array reference) is never reassigned, so it is effectively final; changing an element of the array it points to is allowed.

More OOP with C++ and Java questions

Exams that ask this topic