ProgrammingHardProduct-company placement · Java
Which lines fail to compile?
Which lines fail to compile?
// Java 10 or later
var list = new ArrayList<>(); // L1
list.add(1); list.add("a"); // L2
var x; // L3
var y = null; // L4
var z = new int[]{1, 2}; // L5- A. L2, L3 and L4
- B. L3 only
- C. L3 and L4
- D. L1, L3 and L4
Correct answer: C. L3 and L4
var needs an initializer from which a type can be inferred: L3 has none and null (L4) has no usable type. L1 infers ArrayList<Object>, so adding both an Integer and a String is fine.
More OOP with C++ and Java questions
- What happens when this is compiled? java // Java 17 sealed class Shape permits Circle, Square { } final class Circle extends Shape { }…
- What is printed? java class AppEx extends Exception { } class DbEx extends AppEx { } // static method of the main class: static void run()…
- What is printed? java // in main, which is declared throws Exception: try (AutoCloseable r = null) { System.out.print("body"); }…
- 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")…