Advertisement
AkanthaAnil

OCA Exceptions Exercise 9

Jun 3rd, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1. package ocaexamcodingpractice;
  2.  
  3. public class ExceptionsTestNumber9 {
  4.  
  5.   public void start() {
  6.    
  7.     try {
  8.       System.out.print("Starting up");
  9.       throw new Exception();
  10.      
  11.     } catch (Exception e) {
  12.       System.out.println("Problem");
  13.       System.exit(0);
  14.      
  15.     } finally {
  16.       System.out.println("Shutting down");
  17.     }
  18.   }
  19.  
  20.   public static void main (String [] args) {
  21.     new ExceptionsTestNumber9().start();
  22.  
  23.   }
  24. }
  25.  
  26. /* What is the output of the following program?
  27.  *
  28.  * A. Starting up
  29.  * B. Starting up Problem
  30.  * C. Starting up Problem Shutting Down
  31.  * D. Starting up Shutting Down
  32.  * E. The code does not compile
  33.  * F. An uncaught exception is thrown
  34.  *
  35.  * What is going on here? It is actually super easy!
  36.  *
  37.  * 1. The main method calls start() on a new ExceptionsTestNumber9
  38.  * 2. Start() runs and Starting up is printed. Start() throws Exception e
  39.  * 3. Code moves to catch block where Exception e is handled. Problem is then printed.
  40.  * 4. System.exit() is called, which terminates the JVM, and nothing else is executed! DONE!
  41.  *
  42.  * So, the correct answer is B.
  43.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement