ProgrammingHardProduct-company placement · Java

What is printed?

What is printed?
int count = 0;
Runnable r = () -> System.out.print(count);
count++;
r.run();
  1. A. 0
  2. B. 1
  3. C. A runtime exception
  4. D. Compile-time error

Correct answer: D. Compile-time error

A lambda may only capture local variables that are final or effectively final. Because count is changed later, it is not effectively final and the lambda body does not compile.

More OOP with C++ and Java questions

Exams that ask this topic