Sunday, August 15, 2010

Exceptions: Propeller2.java

Courtesy of Sun Web Learning Center

1. class Propeller2 {
2.   public static void main(String [] args) // add code here?
3.     { new Propeller2().topGo(); }
4.
5.   void topGo() // add code here?
6.     { middleGo(); }
7.
8.   void middleGo() // add code here?
9.     { go();  System.out.println("late middle "); }
10.
11.   void go() // add code here?
12.     { throw new Exception(); }
13. }

For the code to compile, on which lines does the declaration throws Exception need to be added?


A     Only line 11
B     Lines 8 and 11
C     Lines 5, 8, and 11
D     Lines 2, 5, 8, and 11
E     No combination of these additions will allow the code to compile.




D is correct. All of the methods must throw the exception, and it is ok for main to do so.

No comments:

Post a Comment