Curioustab
Aptitude
General Knowledge
Verbal Reasoning
Computer Science
Interview
Take Free Test
Aptitude
General Knowledge
Verbal Reasoning
Computer Science
Interview
Take Free Test
Exceptions Questions
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() {} }
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 "); } }
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 } }
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"); } } }
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 "); } } }
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 */ } }
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(); } }
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");
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"); } } }
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(); } }
Java exceptions (try/catch structure) — what is the most accurate statement about the following code snippet?\n\nSystem.out.print("Start ");\ntry \n{\n System.out.print("Hello world");\n throw new FileNotFoundException();\n}\nSystem.out.print(" Catch Here "); // Line 7\ncatch(EOFException e) \n{\n System.out.print("End of file exception");\n}\ncatch(FileNotFoundException e) \n{\n System.out.print("File not found");\n}\n\nAssume EOFException and FileNotFoundException extend IOException.
Java I/O (checked exceptions) — will this program compile given that FileOutputStream.close() throws IOException?\n\nimport java.io.;\npublic class MyProgram \n{\n public static void main(String args[])\n {\n FileOutputStream out = null;\n try \n {\n out = new FileOutputStream("test.txt");\n out.write(122);\n }\n catch(IOException io) \n {\n System.out.println("IO Error.");\n }\n finally \n {\n out.close();\n }\n }\n}\n\nChoose the best answer.
Java exceptions — select the true statement about try/catch/finally behavior and declaration requirements.
In Java exception handling, which statement is actually true about catch blocks and throwable categories?
In Java, what is the precise output order and behavior for this program using a try/finally with a thrown RuntimeException? public class MyProgram {\n public static void throwit() { throw new RuntimeException(); }\n public static void main(String args[]) {\n try {\n System.out.println("Hello world ");\n throwit();\n System.out.println("Done with try block ");\n } finally {\n System.out.println("Finally executing ");\n }\n }\n}
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 {\n class TestException extends Exception {}\n public void runTest() throws TestException {}\n public void test() /* Point X */ {\n runTest();\n }\n}
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.