nerdexam
Oracle

1Z0-851 · Question #177

Given: 01. class TestException extends Exception { } 02. class A { 03. public String sayHello(String name) throws TestException { 04. if(name == null) throw new TestException(); 05 return "Hello " +…

The correct answer is D. TestA compiles if line 10 is enclosed in a try/catch block that catches TestException. See the full explanation below for the reasoning.

Question

Given: 01. class TestException extends Exception { } 02. class A { 03. public String sayHello(String name) throws TestException { 04. if(name == null) throw new TestException(); 05 return "Hello " + name; 06. } 07. } 08. public class TestA { 09. public static void main(String[] args) { 10. new A().sayHello("Aiko"); 11. } 12. } Which statement is true?

Options

  • ACompilation succeeds.
  • BClass A does not compile.
  • CThe method declared on line 9 cannot be modified to throw TestException.
  • DTestA compiles if line 10 is enclosed in a try/catch block that catches TestException.

How the community answered

(42 responses)
  • A
    5% (2)
  • B
    14% (6)
  • C
    2% (1)
  • D
    79% (33)

Community Discussion

No community discussion yet for this question.

Full 1Z0-851 Practice