ProgrammingVery hardProduct-company placement · Java
Which lines fail to compile?
Which lines fail to compile?
class G<T> {
T[] arr = new T[10]; // L1
T make() { return new T(); } // L2
boolean is(Object o) { return o instanceof G<?>; } // L3
static T sv; // L4
}- A. L1 and L2 only
- B. L1, L2 and L4
- C. All four
- D. L2 and L3
Correct answer: B. L1, L2 and L4
With erasure the type T is unknown at run time, so new T[] and new T() are illegal (L1, L2), and a static field cannot use the class's type parameter (L4). instanceof with the unbounded wildcard G<?> is allowed (L3).
More OOP with C++ and Java questions
- What is printed? java List<Integer> l = new ArrayList<>(List.of(1, 2, 3)); Iterator<Integer> it = l.iterator(); it.next(); it.remove()…
- What is printed? java Map<String, Integer> m = new LinkedHashMap<>(16, 0.75f, true); m.put("a", 1); m.put("b", 2); m.put("c", 3)…
- What is printed? java Map<String, Integer> m = new LinkedHashMap<>(); m.put("x", 1); m.put("y", 2); m.put("x", 3); System.out.print(m);
- What is printed? java TreeSet<Integer> s = new TreeSet<>(List.of(5, 1, 9, 3, 7)); System.out.print(s.headSet(5) + " " + s.tailSet(5) + " "…
- What is printed? java Set<List<Integer>> a = new HashSet<>(); a.add(List.of(1, 2)); a.add(new ArrayList<>(List.of(1, 2))); Set<int[]> b =…
- What is printed? java char[] c = {'h', 'i'}; System.out.print(c); System.out.print(" " + String.valueOf(c) + " " + c.length);