Multiple choice technology programming languages

Click the Exhibit button. 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 two are true? (Choose two.)

  1. Class A will not compile.

  2. Line 46 can throw the unchecked exception TestException.

  3. Line 45 can throw the unchecked exception TestException.

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

  5. Line 46 will compile if enclosed in a try block, where TestException

Reveal answer Fill a bubble to check yourself
D,E Correct answer
Explanation

TestException extends Exception, making it a checked exception. Line 46 calls a.sayHello() which throws TestException, so this line must either be in a try-catch block or the enclosing method must declare 'throws TestException'. Option D correctly states that the enclosing method must throw the exception. Option E correctly states that a try block with TestException handling would allow compilation. Options B and C incorrectly call it unchecked - it's checked.