ProgrammingHardAmazon/Microsoft SDE MCQ · Java placement
Which line fails to compile?
Which line fails to compile?
void run() {
try { int x = 1; }
catch (RuntimeException e) { } // line 1
try { int y = 2; }
catch (Exception e) { } // line 2
try { int z = 3; }
catch (java.io.IOException e) { } // line 3
}- A. Line 1
- B. Line 2
- C. Line 3
- D. None of them
Correct answer: C. Line 3
It is a compile-time error to catch a checked exception that the try block cannot throw, so catching IOException here is rejected. Unchecked exceptions and Exception itself (which covers unchecked ones) may always be caught.
More OOP with C++ and Java questions
- What is printed? java class P { static { System.out.print("1"); } { System.out.print("2"); } P() { System.out.print("3"); } } class C…
- Which line fails to compile? java int base = 10; int step = 1; step = 2; Function<Integer, Integer> f = x -> x + base; // line 1…
- What is printed? java // static methods of the main class: static void f(Object o) { System.out.print("O"); } static void f(int... a) {…
- What is printed? java char c = 'a'; c += 1; System.out.print(('a' + 1) + "" + (char) ('a' + 1) + c);
- What is printed? java Object o = null; String s = (String) o; System.out.print(s + (o instanceof Object) + (s == null));
- What is printed? java List<String> l = new ArrayList<>(List.of("pear", "fig", "apple", "kiwi"))…