Home > Java Programming > Exceptions > Questions and Answers
01. |
1. What will be the output of the program? public class MyProgram{ public static void main(String args[]){ try{ System.out.print("Hello world "); } finally{ System.out.println("Finally executing "); } } } | |||||||||||
|
02. |
1. What will be the output of the program? 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() {} } | |||||||||||
|
03. |
1. What will be the output of the program? 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 "); } } | |||||||||||
|
04. | Which statement is true? | |||||||||||
|
05. |
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"); } and given that EOFException and FileNotFoundException are both subclasses ofIOException, and further assuming this block of code is placed into a class, which statement is most true concerning this code? | |||||||||||
|
06. |
class A{ static void test() throws RuntimeException { try { System.out.print("test "); throw new RuntimeException(); }catch (Exception ex) { System.out.print("exception "); } } public static void main(String[] args) { try { test(); } catch (RuntimeException ex) { System.out.print("runtime "); } System.out.print("end "); } What is the result?
| |||||||||||||||
|
07. |
class A{ public static void main(String[] args) { try { args = null; args[0] = "test"; System.out.println(args[0]); } catch (Exception ex) { System.out.println("Exception"); } catch (NullPointerException npe) { System.out.println("NullPointerException"); } } What is the result? | |||||||||||
|
08. |
1. try{ 2. // some code here 3.} catch (NullPointerException e1) { 4. System.out.print("a"); 5.} catch (Exception e2) { 6. System.out.print("b"); 7.} finally { 8. System.out.print("c"); 9.} If some sort of exception is thrown at line 2, which output is possible? | |||||||||||||||
|
09. |
Given: 1. // some code here 2. try{ 3. // some code here 4.} catch (SomeException se) { 5. // some code here 6.} finally { 7. // some code here 8.} Under which three circumstances will the code on line 7 be executed? (Choose three.) A. The instance gets garbage collected. B. The code on line 3 throws an exception. C. The code on line 5 throws an exception. D. The code on line 1 throws an exception. E. The code on line 3 executes successfully. | |||||||||||
|
10. |
class A{ public static void main(String args[]){ try{ System.out.println(“oneâ€); } System.out.println(“fourâ€); catch(Exception e){ System.out.println(“twoâ€); }finally{
System.out.println(“threeâ€); } } } | |||||||||||
|