Page: 2
5. Exception Handling
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? A. test B. Exception C. Compilation fails. D. 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.)  A. The application will crash. B. The code on line 29 will be executed. C. The code on line 5 of class A will execute. D. The code on line 5 of class B will execute. E. 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? A. test end B. Compilation fails. C. test runtime end D. test exception end E. 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? A. c B. a C. ab D. ac E. bc F. 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? A. java.lang.StackOverflowError B. java.lang.IllegalStateException C. java.lang.ExceptionInInitializerError D. 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? A. null B. finally C. null finally D. Compilation fails. E. finally exception Answer: E
Page: 2
1
2
3
4
5
|