ProgrammingVery hardProduct-company placement · Java
What is printed?
What is printed?
// in main, which is declared throws Exception:
try (AutoCloseable r = null) {
System.out.print("body");
}
System.out.print(" done");- A. A NullPointerException is thrown when the resource is closed
- B. Compile-time error: a resource cannot be null
- C. body done
- D. " done" only; the body is skipped
Correct answer: C. body done
The try-with-resources translation only calls close() if the resource is non-null, so a null resource is simply ignored.
More OOP with C++ and Java questions
- How many of these statements are correct in Java? (i) A subclass object contains storage for its superclass's private fields. (ii) A…
- What is printed? java int[][] a = {{1}, {2}}; int[][] b = a.clone(); b[0][0] = 9; b[1] = new int[]{5}; System.out.print(a[0][0] + " " +…
- What is printed? java StringBuilder[] sbs = new StringBuilder[3]; Arrays.fill(sbs, new StringBuilder()); sbs[0].append("x")…
- What is printed? java List<Integer> l = new ArrayList<>(10); System.out.print(l.size() + " "); l.set(0, 1); System.out.print(l);
- What is printed? java List<Integer> l = new ArrayList<>(List.of(2, 2, 3, 2, 4)); for (int i = 0; i < l.size(); i++) if (l.get(i) == 2)…
- What is printed? java Set<String> s = new HashSet<>(); System.out.print(s.add("a") + " " + s.add("a") + " " + s.remove("b") + " " +…