Given the code fragment:
What is the result?
A. Execution terminates in the first catch statement, and caught a RuntimeException is printed to the console.
B. Execution terminates In the second catch statement, and caught an Exception is printed to the console.
C. A runtime error is thrown in the thread "main".
D. Execution completes normally, and Ready to use is printed to the console.
E. The code fails to compile because a throws keyword is required.
C
public static void main(String[] args) {
// ArrayList myList = new ArrayList();
ArrayList myList = new ArrayList();
String myarray[];
try{
while(true){ //infinite loop so Exception in thread “main” java.lang.OutOfMemoryError: Java heap space
myList.add(“My string”);}
}catch (RuntimeException re){
System.out.println(“caught RuntimeException”);
}catch (Exception e){
System.out.println(“caught Exception”);
}
System.out.println(“Ready to use”);
}
C is correct.
Exception in thread “main” java.lang.OutOfMemoryError
I guess none of the available options are correct! The while loop is infinite so the program will keep running forever.