ProgrammingVery hardProduct-company placement · Java
Which line fails to compile?
Which line fails to compile?
class P { void run() throws java.io.IOException { } }
class C extends P { @Override void run() { } }
// in main, which declares no throws clause:
new C().run(); // L1
P p = new C(); p.run(); // L2- A. L1 only
- B. Both
- C. L2 only
- D. Neither
Correct answer: C. L2 only
An override may drop checked exceptions. The compiler checks exceptions against the STATIC type: through C, run() throws nothing; through a P reference it may throw IOException, which must be caught or declared.
More OOP with C++ and Java questions
- What is printed? java Map<String, Integer> m = new HashMap<>(); m.put("a", 1); m.keySet().remove("a"); System.out.print(m.size() + " ")…
- What is printed? java TreeSet<Integer> s = new TreeSet<>(List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); SortedSet<Integer> sub = s.subSet(3, 6)…
- What is printed? java List<String> l = Arrays.asList("b", null, "a"); l.sort(Comparator.nullsFirst(Comparator.naturalOrder()))…
- What is printed? java // static method of the main class: static String make() { System.out.print("M"); return "y"; } // in main…
- What is printed? java Stream<Integer> s = Stream.of(1, 2, 3); System.out.print(s.count()); System.out.print(s.count());
- What happens when this method is compiled (Java 7 or later)? java static void f() throws java.io.IOException { try { if (Math.random() <…