V. (10 points) What is printed when the following program is run? import java.io
ID: 3919451 • Letter: V
Question
V. (10 points) What is printed when the following program is run? import java.io.*; public class Question6 f public static void main(String[] args) try methodA methodB methodB ) catch (Exception e) System.out.println("beginning.."); try l met.hodB methodA ) catch (Exception e) I System . out.println ( e.getMessage () }; public static void methodA) throws Exception ( methodB throw new Exception "exception from method A" public static void methodB ) throws Exceptionf System.out.println("print in methodB"; try methodC system.out.println("found a fof exception" System.out.println("catch and throw in methodB"i catch (FileNotFoundException e 0 ) catch (Exception e) [ throw e methodC public static void methodc ) throws Exception( throw new FileNotFoundException "almost done!") CS 3443- SAMPLE FINAL EXAM 6 of 11Explanation / Answer
Please find the sequential execution numbered from 1 to 10
import java.io.*;
public class Question6 {
public static void main(String[] args) {
try {
methodA(); /* (1) Call transfers from Here to method A */
methodB();
methodB();
} catch(Exception e) { /* (9) - Control comes here from methodB last instruction */
System.out.println(e.getMessage()); /* (10) - Prints a message "almost done!" */
}
}
public static void methodA() throws Exception {
methodB(); /* (2) Call transfers from Here to method B */
throw new Exception("exception from method A");
}
public static void methodB() throws Exception {
System.out.println("print in methodB"); /* (3) Call transfers from method A to method B and prints a message "print in methodB" */
try {
methodC(); /* (4) Controls comes here and calls methodC */
} catch(FileNotFoundException e) { /* (6) Control comes here from methodC */
System.out.println("found a fof exception"); /* (7) Prints message "found a fof exception" */
} catch(Exception e) {
System.out.println("catch and throw in methodB");
throw e;
}
methodC(); /* (8) - Control comes to this instruction */
}
public static void methodC() throws Exception {
throw new FileNotFoundException("almost done!"); /* (5) Controls comes here and throws FileNotFoundException back to methodB */
}
}
Output of code will be:
print in methodB
found a fof exception
almost done!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.