jobs4timesLogo jobs4timesLogo

Exception Handling

Q: 01 Given:

11. public static void parse(String str) { 
12. try { 
13. float f = Float.parseFloat(str); 
14. } catch (NumberFormatException nfe) { 
15. f = 0; 
16. } finally { 
17. System.out.println(f); 
18. } 
19. } 
20. public static void main(String[] args) { 
21. parse("invalid"); 
22. } 
What is the result?
  1. 0.0
  2. Compilation fails.
  3. A ParseException is thrown by the parse method at runtime.
  4. A NumberFormatException is thrown by the parse method at runtime.
Answer: B

Q: 02 Given:

 84. try { 
 85. ResourceConnection con = resourceFactory.getConnection(); 
 86. Results r = con.query("GET INFO FROM CUSTOMER"); 
 87. info = r.getData(); 
 88. con.close(); 
 89. } catch (ResourceException re) { 
 90. errorLog.write(re.getMessage()); 
 91. } 
 92. return info; 
 
Which statement is true if a ResourceException is thrown on line 86 ?
  1. Line 92 will not execute.
  2. The connection will not be retrieved in line 85.
  3. The resource connection will not be closed on line 88.
  4. The enclosing method will throw an exception to its caller.
Answer: C

Q: 03 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.)
  1. The instance gets garbage collected.
  2. The code on line 33 throws an exception.
  3. The code on line 35 throws an exception.
  4. The code on line 31 throws an exception.
  5. The code on line 33 executes successfully.
Answer: B, C, E

Q: 04 Given:

11. class A { 
12. public void process() { System.out.print("A,"); } 
13. class B extends A { 
14. public void process() throws IOException { 
15. super.process(); 
16. System.out.print("B,"); 
17. throw new IOException(); 
18. } 
19. public static void main(String[] args) { 
20. try { new B().process(); } 
21. catch (IOException e) { System.out.println("Exception"); }} 
What is the result ?
  1. Exception
  2. A,B,Exception
  3. Compilation fails because of an error in line 20.
  4. Compilation fails because of an error in line 14.
  5. A NullPointerException is thrown at runtime.
Answer: D

Q: 05 Given:

 11. static void test() throws Error { 
 12. if (true) throw new AssertionError(); 
 13. System.out.print("test "); 
 14. } 
 15. public static void main(String[] args) { 
 16. try { test(); } 
 17. catch (Exception ex) { System.out.print("exception "); } 
 18. System.out.print("end "); 
 19. } 
 
What is the result ?
  1. end
  2. Compilation fails.
  3. exception end
  4. exception test end
  5. A Throwable is thrown by main.
  6. An Exception is thrown by main.
Answer: E

Q: 06 Given:

  
11. Float pi = new Float(3.14f); 
12. if (pi > 3) { 
13. System.out.print("pi is bigger than 3. "); 
14. }
15. else { 
16. System.out.print("pi is not bigger than 3. "); 
17. } 
18. finally { 
19. System.out.println("Have a nice day."); 
20. } 
What is the result ?
  1. Compilation fails.
  2. pi is bigger than 3.
  3. An exception occurs at runtime.
  4. pi is bigger than 3. Have a nice day.
  5. pi is not bigger than 3. Have a nice day.
Answer: A

Q : 07 Given:

11. public static void main(String[] args) { 
12. try { 
13. args = null; 
14. args[0] = "test"; 
15. System.out.println(args[0]); 
16. } catch (Exception ex) { 
17. System.out.println("Exception"); 
18. } catch (NullPointerException npe) { 
19. System.out.println("NullPointerException"); 
20. } 
21. } 
What is the result ?
  1. test
  2. Exception
  3. Compilation fails.
  4. NullPointerException
Answer: C

Q:08 Click the Exhibit button. Given:

25. try { 
26. A a = new A(); 
27. a.method1(); 
28. } catch (Exception e) { 
29. System.out.print("an error occurred"); 
30. }

Which two statements are true if a NullPointerException is thrown on line 3 of class C ? (Choose two.)

  1. The application will crash.
  2. The code on line 29 will be executed.
  3. The code on line 5 of class A will execute.
  4. The code on line 5 of class B will execute.
  5. The exception will be propagated back to line 27.
Answer: B, E

Q:09 Given:

11. static void test() throws RuntimeException {
12. try {
13. System.out.print("test ");
14. throw new RuntimeException();
15. }
16. catch (Exception ex) { System.out.print("exception "); }
17. }
18. public static void main(String[] args) {
19. try { test(); }
20. catch (RuntimeException ex) { System.out.print("runtime "); }
21. System.out.print("end ");
22. }
What is the result ?
  1. test end
  2. Compilation fails.
  3. test runtime end
  4. test exception end
  5. A Throwable is thrown by main at runtime.
Answer: D

Q:10 Given:

33. try { 
34. // some code here 
35. } catch (NullPointerException e1) { 
36. System.out.print("a"); 
37. } catch (RuntimeException e2) { 
38. System.out.print("b"); 
39. } finally { 
40. System.out.print("c"); 
41. } 
What is the result if a NullPointerException occurs on line 34 ?
  1. c
  2. a
  3. ab
  4. ac
  5. bc
  6. abc
Answer: D

Q:11 Given:

10. public class Foo { 
11. static int[] a; 
12. static { a[0]=2; } 
13. public static void main( String[] args ) {} 
14. } 
Which exception or error will be thrown when a programmer attempts to run this code ?
  1. java.lang.StackOverflowError
  2. java.lang.IllegalStateException
  3. java.lang.ExceptionInInitializerError
  4. java.lang.ArrayIndexOutOfBoundsException
Answer: C

Q: 12 Given:

11. static void test() { 
12. try { 
13. String x = null; 
14. System.out.print(x.toString() + " "); 
15. } 
16. finally { System.out.print("finally "); } 
17. } 
18. public static void main(String[] args) { 
19. try { test(); } 
20. catch (Exception ex) { System.out.print("exception "); } 
21. }
What is the result ?
  1. null
  2. finally
  3. null finally
  4. Compilation fails.
  5. finally exception
Answer: E

Q: 13 Click the Exhibit button.

Given:
31. public void method() {
32. A a = new A();
33. a.method1();
34. }
Which statement is true if a TestException is thrown on line 3 of class B ?
  1. Line 33 must be called within a try block.
  2. The exception thrown by method1 in class A is not required to be caught.
  3. The method declared on line 31 must be declared to throw a RuntimeException.
  4. On line 5 of class A, the call to method2 of class B does not need to be placed in a try/catch block.
Answer: B

Q: 14 Click the Exhibit button.

Which statement is true about the two classes ?
  1. Compilation of both classes will fail.
  2. Compilation of both classes will succeed.
  3. Compilation of class A will fail. Compilation of class B will succeed.
  4. Compilation of class B will fail. Compilation of class A will succeed.
Answer: D

Q : 15

Click the Exhibit button.
Class TestException
1. public class TestException extends Exception {
2. }
Class A:
1. public class A {
2.
3. public String sayHello(String name) throws TestException {
4.
5. if(name == null) {
6. throw new TestException();
7. }
8.
9. return “Hello “+ name;
10. }
11.
12. }
A programmer wants to use this code in an application:
45. A a=new A();
46. System.out.println(a.sayHello(”John”));
Which two are true? (Choose two.)
  1. Class A will not compile.
  2. Line 46 can throw the unchecked exception TestException.
  3. Line 45 can throw the unchecked exception TestException.
  4. Line 46 will compile if the enclosing method throws a TestException.
  5. Line 46 will compile if enclosed in a try block, where TestExceptionis caught.
Answer: D , E

Q : 16 Given:

11.classA { 
12. public void process() { System.out.print(”A “); } } 
13. class B extends A { 
14. public void process() throws RuntimeException { 
15. super.process(); 
16. if (true) throw new RuntimeException(); 
17. System.out.print(“B”); }} 
18. public static void main(String[] args) { 
19. try { ((A)new B()).process(); } 
20. catch (Exception e) { System.out.print(”Exception “); } 
21. }
What is the result ?
  1. Exception
  2. A Exception
  3. A Exception B
  4. A B Exception
  5. Compilation fails because of an error in line 14.
  6. Compilation fails because of an error in line 19.
Answer: B

Q : 17

which two code fragments are most likely to cause a StackOverflowError? (Choose two.)
  1. int []x = {1,2,3,4,5}; for(int y = 0; y < 6; y++) System.out.println(x[y]);
  2. static int[] x = {7,6,5,4}; static { x[1] = 8; x[4] = 3; }
  3. for(int y = 10; y < 10; y++) doStuff(y);
  4. void doOne(int x) { doTwo(x); } void doTwo(int y) { doThree(y); } void doThree(int z) { doTwo(z); }
  5. for(int x = 0; x < 1000000000; x++) doStuff(x);
  6. void counter(int i) { counter(++i); }
Answer: D,F

Q : 18

which can appropriately be thrown by a programmer using Java SE technology to create a desktop application ?
  1. ClassCastException
  2. NullPointerException
  3. NoClassDefFoundError
  4. NumberFormatException
  5. ArrayIndexOutOfBoundsException
Answer: D

19. Given:

import java.io.*; 

class Master { 
String doFileStuff() throws FileNotFoundException { 
 return "a"; 
} 
}
 
class Slave extends Master { 
public static void main(String[] args) { 
 String s = null; 
 try {
  s = new Slave().doFileStuff(); 
 } 
 catch ( Exception x) { 
   s = "b"; 
 } 
System.out.println(s); } // insert code here } 
Which, inserted independently at // insert code here, will compile, and produce the output b? (Choose all that apply.)
  1. String doFileStuff() { return "b"; }
  2. String doFileStuff() throws IOException { return "b"; }
  3. String doFileStuff(int x) throws IOException { return "b"; }
  4. String doFileStuff() throws FileNotFoundException { return "b"; }
  5. String doFileStuff() throws NumberFormatException { return "b"; }
  6. String doFileStuff() throws NumberFormatException, FileNotFoundException { return "b"; }
Answer:
  • A , D, E, and F are correct. It's okay for an overriding method to throw the same exceptions, narrower exceptions, or no exceptions.
    And it's okay for the overriding method to throw any runtime exceptions.
  • B is incorrect, because the overriding method is trying to throw a broader exception. C is incorrect. This method doesn't override, so the output is a.

20. Given:

class Input { 
 public static void main(String[] args) { 
  String s = "-"; 
  try { 
   doMath(args[0]); s += "t "; // line 6 
  } 
  finally { 
   System.out.println(s += "f "); 
   } 
  } 
public static void doMath(String a) { 
 int y = 7 / Integer.parseInt(a); 
} 
}

And the command-line invocations: java Input java Input 0

Which are true? (Choose all that apply.)
  1. Line 6 is executed exactly 0 times.
  2. Line 6 is executed exactly 1 time.
  3. Line 6 is executed exactly 2 times.
  4. The finally block is executed exactly 0 times.
  5. The finally block is executed exactly 1 time.
  6. The finally block is executed exactly 2 times.
  7. Both invocations produce the same exceptions.
  8. Each invocation produces a different exception.
Answer:
  • A , F, and H are correct. Since both invocations throw exceptions, line 6 is never reached.
    Since both exceptions occurred within a try block, the finally block will always execute.
    The first invocation throws an ArrayIndexOutOfBoundsException,
    and the second invocation throws an ArithmeticException for the attempt to divide by zero.
  • B, C, D, E, and G are incorrect based on the above.

21. Given:

class Plane { 
 static String s = "-"; 
public static void main(String[] args) { 
 new Plane().s1(); 
 System.out.println(s); 
} 
void s1() { 
 try {  s2();  } 
 catch (Exception e) { s += "c"; } 
 } 
 
 void s2() throws Exception { s3(); s += "2"; s3(); s += "2b"; } 
 void s3() throws Exception { throw new Exception(); } 
} 
What is the result ?
  1. -
  2. -c
  3. -c2
  4. -2c
  5. -c22b
  6. -2c2b
  7. -2c2bc
  8. Compilation fails.
Answer:
  • B is correct. Once s3() throws the exception to s2(), s2() throws it to s1(), and no more of s2()'s code will be executed.
  • A, C, D, E, F, G, and H are incorrect based on the above.

22. Given:

try { 
 int x = Integer.parseInt("two"); 
}
Which could be used to create an appropriate catch block? (Choose all that apply.)
  1. ClassCastException
  2. IllegalStateException
  3. NumberFormatException
  4. IllegalArgumentException
  5. ExceptionInInitializerError
  6. ArrayIndexOutOfBoundsException
Answer:
  • C and D are correct. Integer.parseInt can throw a NumberFormatException, and IllegalArgumentException is its superclass (i.e., a broader exception).
  • A, B, E, and F are not in NumberFormatException‘s class hierarchy.

23. Given:

1. class Ping extends Utils { 
2. public static void main(String [] args) { 
3. Utils u = new Ping(); 
4. System.out.print(u.getInt(args[0])); 
5. } 
6. int getInt(String arg) { 
7. return Integer.parseInt(arg); 
8. } 
9. } 
10. class Utils { 
11. int getInt(String x) throws Exception { return 7; } 
12. }
And the following three possible changes:
C1. Declare that main() throws an Exception. 
C2. Declare that Ping.getInt() throws an Exception. 
C3. Wrap the invocation of getInt() in a try / catch block. 
Which change(s) allow the code to compile? (Choose all that apply.)
  1. Just C1 is sufficient.
  2. Just C2 is sufficient.
  3. Just C3 is sufficient.
  4. Both C1 and C2 are required.
  5. Both C1 and C3 are required.
  6. Both C2 and C3 are required.
  7. All three changes are required.
Answer:
  • A and C are correct. Remember that line 4 is making a polymorphic call so the compiler knows that an exception might be thrown.
    If C1 is implemented the exception has been sufficiently declared, and if C3 is implemented the exception has been sufficiently handled.
    C2 is not necessary in either case.
  • B, D, E, F, and G are incorrect based on the above.

24. Given:

class Emu { 
 static String s = "-"; 
 public static void main(String[] args) { 
  try { 
   throw new Exception(); 
  } 
  catch (Exception e) { 
  try { 
   try { 
     throw new Exception(); 
   } catch (Exception ex) {
      s += "ic "; 
   } throw new Exception(); 
   } catch (Exception x) { s += "mc "; } 
 finally { s += "mf "; } 
 } finally { s += "of "; } 
 System.out.println(s); 
} 
} 
What is the result ?
  1. -ic of
  2. -mf of
  3. -mc mf
  4. -ic mf of
  5. -ic mc mf of
  6. -ic mc of mf
  7. Compilation fails.
Answer:
  • E is correct. There is no problem nesting try / catch blocks.
    As is normal, when an exception is thrown, the code in the catch block runs, then the code in the finally block runs.
  • A, B, C, D, and F are incorrect based on the above.

25. Given:

class Mineral { } 
class Gem extends Mineral { } 
class Miner { 
 static int x = 7; 
 static String s = null; 
 public static void getWeight(Mineral m) { 
  int y = 0 / x; 
  System.out.print(s + " "); 
 } 
public static void main(String[] args) { 
 Mineral[] ma = {new Mineral(), new Gem()}; 
 for(Object o : ma) 
 getWeight((Mineral) o); 
} 
}
And the command-line invocation: java Miner.java What is the result ?
  1. null
  2. null null
  3. A ClassCastException is thrown.
  4. A NullPointerException is thrown.
  5. A NoClassDefFoundError is thrown.
  6. An ArithmeticException is thrown.
  7. An IllegalArgumentException is thrown.
  8. An ArrayIndexOutOfBoundsException is thrown.
Answer:
  • E is correct. The invocation should be java Miner, in which case null null would be produced.
  • A, B, C, D, F, G, and H are incorrect based on the above.

26. 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.)

  1. ClassCastException
  2. IllegalStateException
  3. NumberFormatException
  4. IllegalArgumentException
  5. ExceptionInInitializerError
Answer:
  • B , C, and D are correct. B is typically used to report an environment problem such as trying to access a resource that's closed. C is often thrown in API methods that attempt to convert poorly formed String arguments to numeric values. D is often thrown in API methods that receive poorly formed arguments.
  • A and E are thrown by the JVM.


BACK