Multiple choice technology programming languages

Class TestException 1. public class TestException extends Exception { 2. } Class A: 1. public class A { 2. 3. public String sayHello(String name) throws TestException { 4. 5. if(name == null) { 6. throw new TestException(); 7. } 8. 9. return “Hello “+ name; 10. } 11. 12. } A programmer wants to use this code in an application: 45. A a=new A(); 46. System.out.println(a.sayHello(”John”)); Which are true?

  1. Line 46 will compile if the enclosing method throws a TestException.

  2. Class A will not compile.

  3. Line 46 will compile if enclosed in a try block, where TestException is caught

  4. Line 45 can throw the unchecked exception TestException.

Reveal answer Fill a bubble to check yourself
A,C Correct answer
Explanation

TestException is a checked exception (extends Exception). Line 46 calls a method that throws TestException, so it must either: (A) be in a method that declares throws TestException, or (C) be wrapped in a try-catch block. Option A is true - if the enclosing method throws TestException, line 46 compiles. Option C is true - try-catch handles it. Option D is false because TestException is checked, not unchecked.