Java OCA OCP Practice Question 2877

Question

Given:

2. public class Main {  
3.   public static void main(String[] args) {  // change code here ?  
4.     // insert code here ?  
5.       new Main().doShape();    
6.     // insert code here ?  
...  //from   w  ww.jav  a 2 s .  co m
10.   }  
11.   static void doShape() throws Exception {  
12.     throw new Exception();  
13. } } 

Which are true? (Choose all that apply.)

  • A. Compilation succeeds with no code changes.
  • B. Compilation succeeds if main() is changed to throw an Exception.
  • C. Compilation succeeds if a try-catch is added, surrounding line 5.
  • D. Compilation succeeds if a try-finally is added, surrounding line 5.
  • E. Compilation succeeds only if BOTH main() declares an Exception AND a try-catch is added, surrounding line 5.


B and C are correct.

Note

Invoking doShape() will cause an Exception that must either be handled (option C) or declared (option B).

(Remember, you can compile a program in which main() throws an Exception.).

A is incorrect based on the above.

D is incorrect because a try-finally will not handle the Exception.

E is incorrect because EITHER handling or declaring the exception is sufficient.




PreviousNext

Related