Exceptions Questions

Practice Exceptions MCQs with answers and explanations. Page 1 of 1.

Category
Java Programming
Topic
Exceptions
Page
1 / 1
Mode
Practice

Questions

Open any question to view the answer and explanation.

In Java, what will the following program print when no exception is thrown in the try block? public class X { public static void main(String [] args) { try { badMethod(); System.out.print("A"); } catch (Exception ex) { System.out.print("B"); } finally { System.out.print("C"); } System.out.print("D"); } public static void badMethod() {} }
Open
View answer
In Java, what is the output of this program when a RuntimeException is thrown and handled, with a finally block executing? public class RTExcept { public static void throwit() { System.out.print("throwit "); throw new RuntimeException(); } public static void main(String [] args) { try { System.out.print("hello "); throwit(); } catch (Exception re) { System.out.print("caught "); } finally { System.out.print("finally "); } System.out.println("after "); } }
Open
View answer
In Java, what happens when a method throws an Error that is not caught by catch(Exception), with a finally block present? public class X { public static void main(String [] args) { try { badMethod(); System.out.print("A"); } catch (Exception ex) { System.out.print("B"); } finally { System.out.print("C"); } System.out.print("D"); } public static void badMethod() { throw new Error(); // Line 22 } }
Open
View answer
In Java, what will the following program print when a return occurs inside try and a finally block is present? public class Foo { public static void main(String[] args) { try { return; } finally { System.out.println("Finally"); } } }
Open
View answer
In Java, what will be printed when a try block runs normally and a finally block executes afterward? public class MyProgram { public static void main(String args[]) { try { System.out.print("Hello world "); } finally { System.out.println("Finally executing "); } } }
Open
View answer
Java — Predict the console output for try/finally with a thrown exception: public class Test { public static void aMethod() throws Exception { try /* Line 5 */ { throw new Exception(); /* Line 7 */ } finally /* Line 9 */ { System.out.print("finally "); /* Line 11 */ } } public static void main(String args[]) { try { aMethod(); } catch (Exception e) /* Line 20 */ { System.out.print("exception "); } System.out.print("finished"); /* Line 24 */ } }
Open
View answer
Java — Determine output with RuntimeException, multiple catch blocks, and finally: public class X { public static void main(String [] args) { try { badMethod(); System.out.print("A"); } catch (RuntimeException ex) /* Line 10 */ { System.out.print("B"); } catch (Exception ex1) { System.out.print("C"); } finally { System.out.print("D"); } System.out.print("E"); } public static void badMethod() { throw new RuntimeException(); } }
Open
View answer
Java — Identify the issue with catch ordering (general Exception before specific ArithmeticException): try { int x = 0; int y = 5 / x; } catch (Exception e) { System.out.println("Exception"); } catch (ArithmeticException ae) { System.out.println(" Arithmetic Exception"); } System.out.println("finished");
Open
View answer
Java — Determine output when throwing a subclass exception and catching a superclass: class Exc0 extends Exception { } class Exc1 extends Exc0 { } /* Line 2 */ public class Test { public static void main(String args[]) { try { throw new Exc1(); /* Line 9 */ } catch (Exc0 e0) /* Line 11 */ { System.out.println("Ex0 caught"); } catch (Exception e) { System.out.println("exception caught"); } } }
Open
View answer
Java — Output with unchecked exception, single broad catch, finally, and trailing print: public class X { public static void main(String [] args) { try { badMethod(); /* Line 7 */ System.out.print("A"); } catch (Exception ex) /* Line 10 */ { System.out.print("B"); /* Line 12 */ } finally /* Line 14 */ { System.out.print("C"); /* Line 16 */ } System.out.print("D"); /* Line 18 */ } public static void badMethod() { throw new RuntimeException(); } }
Open
View answer
Java exceptions (try/catch structure) — what is the most accurate statement about the following code snippet? System.out.print("Start "); try { System.out.print("Hello world"); throw new FileNotFoundException(); } System.out.print(" Catch Here "); // Line 7 catch(EOFException e) { System.out.print("End of file exception"); } catch(FileNotFoundException e) { System.out.print("File not found"); } Assume EOFException and FileNotFoundException extend IOException.
Open
View answer
Java I/O (checked exceptions) — will this program compile given that FileOutputStream.close() throws IOException? import java.io.; public class MyProgram { public static void main(String args[]) { FileOutputStream out = null; try { out = new FileOutputStream("test.txt"); out.write(122); } catch(IOException io) { System.out.println("IO Error."); } finally { out.close(); } } } Choose the best answer.
Open
View answer
Java exceptions — select the true statement about try/catch/finally behavior and declaration requirements.
Open
View answer
In Java exception handling, which statement is actually true about catch blocks and throwable categories?
Open
View answer
In Java, what is the precise output order and behavior for this program using a try/finally with a thrown RuntimeException? public class MyProgram { public static void throwit() { throw new RuntimeException(); } public static void main(String args[]) { try { System.out.println("Hello world "); throwit(); System.out.println("Done with try block "); } finally { System.out.println("Finally executing "); } } }
Open
View answer
Java checked exception propagation: at Point X on line 5, what must be added so test() compiles when runTest() declares throws TestException? public class ExceptionTest { class TestException extends Exception {} public void runTest() throws TestException {} public void test() /* Point X */ { runTest(); } }
Open
View answer
Java "throw" targets: which of the following types can legally be thrown with the throw statement? Consider: 1) Error, 2) Event, 3) Object, 4) Throwable, 5) Exception, 6) RuntimeException.
Open
View answer

Practice smarter

Solve a few questions daily and revisit weak topics regularly to improve accuracy.