ProgrammingHardProduct-company placement · Java
What is printed?
What is printed?
int count = 0; Runnable r = () -> System.out.print(count); count++; r.run();
- A. 0
- B. 1
- C. A runtime exception
- 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
- What is printed? java enum Level { LOW(1), HIGH(3); Level(int v) { System.out.print(name() + v + " "); } static { System.out.print("S ")…
- What is printed? java enum Dir { N, E, S, W } // in main: System.out.print(Dir.values()[2] + " " + Dir.valueOf("W").ordinal() + " ")…
- What is printed? java class Box implements Cloneable { int[] data = {1, 2}; int n = 5; public Box clone() throws…
- What is printed? java // static method of the main class: static void swap(int[] a, int[] b) { int[] t = a; a = b; b = t; a[0] = 100; } //…
- What is printed? java Object o = "hi"; String s = (String) o; Integer n = null; System.out.print((n instanceof Integer) + " " + (o…
- What is printed? java class A { static int v = 1; static int get() { return v; } } class B extends A { static int v = 2; } // in main…