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]);- A. 0
- B. 2
- C. 1
- 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
- What is printed? java List<Integer> a = new ArrayList<>(List.of(1, 2)); List<Integer> b = new LinkedList<>(List.of(1, 2))…
- What is printed? java Queue<Integer> q = new ArrayDeque<>(); q.add(1); q.add(2); q.offer(3); q.poll(); System.out.print(q.peek() + " " +…
- What is printed? java System.out.print(Stream.of(1, 2, 3).reduce(10, Integer::sum));
- What is printed? java long n = Stream.of(List.of(1, 2), List.of(2, 3)) .flatMap(List::stream).distinct().count(); System.out.print(n);
- What is printed? java Map<Boolean, List<Integer>> m = Stream.of(2, 4) .collect(Collectors.partitioningBy(x -> x > 5))…
- What happens? java interface Show { String toString(); } class C implements Show { } // in main: System.out.print(new C() instanceof Show);