Java OCA OCP Practice Question 1355

Question

What is the result of compiling and running the following application?

package mypkg; /*from w w  w  . ja va  2  s .  co  m*/
public class Main { 
   public void m() throws Exception {  // p1 
      try { 
         throw new Exception("Circle"); 
      } catch (Exception e) { 
         System.out.print("Opening!"); 
      } finally { 
         System.out.print("Walls");  // p2 
      } 
   } 


  public static void main(String[] moat) { 
     new Main().m();  // p3 
  } 
} 
  • A. The code does not compile because of line p1.
  • B. The code does not compile because of line p2.
  • C. The code does not compile because of line p3.
  • D. The code compiles, but a stack trace is printed at runtime.


C.

Note

The application does not compile, so Option D is incorrect.

The m() method compiles without issue, so Options A and B are incorrect.

The issue here is how the m() method is called from within the main() method on line p3.

The m() method declares the checked exception, Exception, but the main() method from which it is called does not handle or declare the exception.

In order for this code to compile, the main() method would have to have a try-catch statement around line p3 that properly handles the checked exception, or the main() would have to be updated to declare a compatible checked exception.

Line p3 does not compile, and Option C is the correct answer.




PreviousNext

Related