Home > Java Programming > Exceptions > Questions and Answers
01. |
try{ File f = new File("a.txt"); }catch(Exception e){ }catch(IOException io){ } Does this code create new file name a.txt ?
| |||||||||||
|
02. |
1. public class A { 2. public void method1() { 3. try { 4. B b = new B(); 5. b.method2(); 6. // more code here 7. } catch (TestException te) { 8. throw new RuntimeException(te); 9. } 10. } 11. } 1. public class B { 2. public void method2() throws TestException { 3. // more code here 4. } 5. } 6. public class TestException extends Exception { 7. } Given: 1. public void method() { 2. A a = new A(); 3. a.method1(); 4. } Which statement is true if a TestException is thrown on line 3 of class B? | |||||||||||
|
03. |
What is the output for the below code ? public class SuperClass { public int doIt(String str, Integer... data)throws ArrayIndexOutOfBoundsException{ String signature = "(String, Integer[])"; System.out.println(str + " " + signature); return 1; } } public class SubClass extends SuperClass{ public int doIt(String str, Integer... data) throws Exception{ String signature = "(String, Integer[])"; System.out.println("Overridden: " + str + " " + signature); return 0; } public static void main(String... args){ SuperClass sb = new SubClass(); try{ sb.doIt("hello", 3); }catch(Exception e){ } } } | |||||||||||
|
04. |
What is the result of executing the following code, using the parameters 0 and 3 ? public void divide(int a, int b) { try { int c = a / b; } catch (Exception e) { System.out.print("Exception "); } finally { System.out.println("Finally"); } | |||||||||||
|
05. |
Which are most typically thrown by an API developer or an application developer as opposed to being thrown by the JVM? (Choose all that apply.) A. ClassCastException B. IllegalStateException C. NumberFormatException D. IllegalArgumentException E. ExceptionInInitializerError | |||||||||||
|
06. |
What is the output for the below code ? public class SuperClass { public int doIt(String str, Integer... data)throws ArrayIndexOutOfBoundsException{ String signature = "(String, Integer[])"; System.out.println(str + " " + signature); return 1; } } public class SubClass extends SuperClass{ public int doIt(String str, Integer... data) throws Exception{ String signature = "(String, Integer[])"; System.out.println("Overridden: " + str + " " + signature); return 0; } public static void main(String... args){ SuperClass sb = new SubClass(); try{ sb.doIt("hello", 3); }catch(Exception e){ } } } | |||||||||||
|
07. |
Which of the below statement is true about Error? A)An Error is a subclass of Throwable
B)An Error is a subclass of Exception C)Error indicates serious problems that a reasonable application should not try to catch. D)An Error is a subclass of IOException | |||||||||||
|
08. | Which of the following is type of RuntimeException? | |||||||||||
|
09. |
The program given below will test your understanding of Float primitive data types and functions in Java program. Given below the sample code : public class SuperClass { public static void main(String[] args) { System.out.println(stringConvert("0.3")); System.out.println(stringConvert("0.3A")); System.out.println(stringConvert(null)); } public static boolean stringConvert(String s) { float factor = 0; try { factor = Float.valueOf(s).floatValue(); return true; } catch (NumberFormatException e) { System.out.println("Exception in number formatting " + s); factor = Float.NaN; } finally { System.out.println("Finally"); } return false; } } Find the output of the following code ? | |||||||||||
|
10. |
Given: 31. // some code here 32. try { 33. // some code here 34. } catch (SomeException se) { 35. // some code here 36. } finally { 37. // some code here 38. } Under which three circumstances will the code on line 37 be executed? (Choose three.) A. The instance gets garbage collected. B. The code on line 33 throws an exception. C. The code on line 35 throws an exception. D. The code on line 31 throws an exception. E. The code on line 33 executes successfully. | |||||||||||
|