OCJP Practice Papers - Handling Exceptions

OCJP Practice Papers Java Handling Exceptions include following topics

  • Differentiate among checked exceptions, unchecked exceptions, and Errors
  • Create a try-catch block and determine how exceptions alter normal program flow
  • Describe the advantages of Exception handling
  • Create and invoke a method that throws an exception
  • Recognize common exception classes (such as NullPointerException, ArithmeticException, ArrayIndexOutOfBoundsException, ClassCastException)

See the complete syllabus for OCJP here

  1. What is the result of compiling and executing the following application? package mind; public class Remember { public static void think() throws Exception { // k1 try { throw new Exception(); } } public static void main(String… ideas) throws Exception { think(); } }
    A. The code compiles and runs without printing anything.
    B. The code compiles but a stack trace is printed at runtime.
    C. The code does not compile because of line k1.
    D. The code does not compile for another reason.

2. Choose the answer that lists the keywords in the order that they would be used together.
A. catch, try, finally
B. try, catch, finally
C. finally, catch, try
D. try, finally, catch

3. Which of the following diagrams of java.lang classes shows the inheritance model properly? A. B. C. D. 4. Which of the following Throwable types is it recommended not to catch in a Java application?
A. Error
B. CheckedException
C. Exception
D. RuntimeException

5. What is the output of the following application?

package game;
public class Baseball {
public static void main(String... teams) {
try {
int score = 1;
System.out.print(score++);
} catch (Throwable t) {
System.out.print(score++);
} finally {
System.out.print(score++);
}
System.out.print(score++);
}
}

A. 123
B. 124
C. 12
D. None of the above

6. Which of the following is a checked exception?
A. ClassCastException
B. IOException
C. ArrayIndexOutOfBoundsException
D. IllegalArgumentException

7. Fill in the blanks: The ____________keyword is used in method declarations, while the ____________keyword is used to throw an exception to the surrounding process.
A. throws, throw
B. catch, throw
C. throw, throws
D. throws, catch

8. If a try statement has catch blocks for both Exception and IOException, then which of the following statements is correct?
A. The catch block for Exception must appear before the catch block for IOException.
B. The catch block for IOException must appear before the catch block for Exception.
C. The catch blocks for these two exception types can be declared in any order.
D. A try statement cannot be declared with these two catch block types because they

are incompatible. 9. What is the output of the following application?

package game;
public class Football {
public static void main(String officials[]) {
try {
System.out.print('A');
throw new RuntimeException("Out of bounds!");
} catch (ArrayIndexOutOfBoundsException aioobe) {
System.out.print('B');
throw t;
} finally {
System.out.print('C');
}
}
}

A. ABC
B. ABC, followed by a stack trace for a RuntimeException
C. AC, followed by a stack trace for a RuntimeException
D. None of the above

10. What is the result of compiling and running the following application?

package castles;
public class Fortress {
public void openDrawbridge() throws Exception { // p1
try {
throw new Exception("Circle");
} catch (Exception e) {
System.out.print("Opening!");
} finally {
System.out.print("Walls"); // p2
}
}
public static void main(String[] moat) {
new Fortress().openDrawbridge(); // p3
}
}

A. The code does not compile because of line p1.
B. The code does not compile because of line p2.
C. The code does not compile because of line p3.
D. The code compiles, but a stack trace is printed at runtime.

11. Which of the following exception types must be handled or declared by the method in which they are thrown?
A. NullPointerException
B. Exception
C. RuntimeException
D. ArithmeticException

12. What is the output of the following application? package game; public class BasketBall { public static void main(String[] dribble) { try { System.out.print(1); throw new ClassCastException(); } catch (ArrayIndexOutOfBoundsException ex) { System.out.print(2); } catch (Throwable ex) { System.out.print(3); } finally { System.out.print(4); } System.out.print(5); } }
A. 1345
B. 1235
C. The code does not compile.
D. The code compiles but throws an exception at runtime.

13. Which of the following statements about a finally block is true?
A. Every line of the finally block is guaranteed to be executed.
B. The finally block is executed only if the related catch block is also executed.
C. The finally statement requires brackets {}.
D. The finally block cannot throw an exception.

14. Given that FileNotFoundException is a subclass of IOException, what is the output of the following application? package office; import java.io.*; public class Printer { public void print() { try { throw new FileNotFoundException(); } catch (IOException exception) { System.out.print("Z"); } catch (FileNotFoundException enfe) { System.out.print("X"); } finally { System.out.print("Y"); } } public static void main(String... ink) { new Printer().print(); } }
A. XY
B. ZY
C. The code does not compile.
D. The code compiles but a stack trace is printed at runtime.

15. Which keywords are required with a try statement? I. catch II. finalize III. finally
A. I only
B. II only
C. I or III, or both
D. None of these statements are required with a try statement.

16. Which statement about the role of exceptions in Java is incorrect?
A. Exceptions are often used when things go wrong or deviate from the expected path.
B. An application that throws an exception will terminate.
C. Some exceptions can be avoided programmatically.
D. An application that can properly handle its exception may recover from unexpected

problems. 17. What is the output of the following application? package harbor; class CapsizedException extends Exception {} class Transport { public int travel() throws CapsizedException { return 2; }; }p ublic class Boat { public int travel() throws Exception { return 4; }; // j1 public static void main(String... distance) throws Exception{ try { System.out.print(new Boat().travel()); } catch (Exception e) ( System.out.print(8); ) } }
A. 4
B. 8
C. The code does not compile due to line j1.
D. The code does not compile for another reason.

18. Which of following method signatures would not be allowed in a class implementing the Printer interface? class PrintException extends Exception {} class PaperPrintException extends PrintException {} public interface Printer { abstract int printData() throws PrintException; }
A. public int printData() throws PaperPrintException
B. public int printData() throws Exception
C. public int printData()
D. None of the above

19. Which import statement is required to be declared in order to use the Exception, RuntimeException, and Throwable classes in an application?
A. import java.exception.*;
B. import java.util.exception.*;
C. import java.lang.*;
D. None of the above

20. Which statement about the following classes is correct? class GasException extends Exception {} class Element { public int getSymbol() throws GasException { return -1; } // g1 }p ublic class Oxygen extends Element { public int getSymbol() { return 8; } // g2 public void printData() { try { System.out.print(getSymbol()); } catch { // g3 System.out.print("Unable to read data"); } } }
A. The code does not compile because of line g1.
B. The code does not compile because of line g2.
C. The code does not compile because of line g3.
D. None of the above

21. Fill in the blanks: A program must handle or declare ____________but should never handle ____________.
A. java.lang.Error, unchecked exceptions
B. checked exceptions, java.lang.Error
C. java.lang.Throwable, java.lang.Error
D. unchecked exceptions, java.lang.Exception

22. What is the result of compiling and running the following application? package castles; class CastleUnderSiegeException extends Exception {} class KnightAttackingException extends CastleUnderSiegeException {} public class Citadel { public void openDrawbridge() throws RuntimeException { // q1 try { throw new KnightAttackingException(); } catch (Exception e) { throw new ClassCastException(); } finally { throw new CastleUnderSiegeException(); // q2 } } public static void main(String[] moat) { new Citadel().openDrawbridge(); // q3 } }
A. The code does not compile because of line q1.
B. The code does not compile because of line q2.
C. The code does not compile because of line q3.
D. The code compiles, but a stack trace is printed at runtime.

23. If an exception matches two or more catch blocks, which catch block is executed?
A. The first one that matches is executed.
B. The last one that matches is executed.
C. All matched blocks are executed.
D. It is not possible to write code like this.

24. What is the output of the following application? package system; public class Computer { public void compute() throws Exception { throw new RuntimeException("Error processing request"); } public static void main(String[] bits) { try { new Computer().compute(); System.out.print("Ping"); } catch (NullPointerException e) { System.out.print("Pong"); throw e; } } }
A. Ping
B. Pong
C. The code does not compile.
D. The code compiles but throws an exception at runtime.

25. In the following application, the value of list has been omitted. Assuming the code compiles without issue, which one of the following is not a possible output of executing this class? package checkboard; public class Attendance { private Boolean[] list = // value omitted public int printTodaysCount() { int count=0; for(int i=0; i10; i++) { if(list[i]) ++count; } return count; } public static void main(String[] roster) { new Attendance().printTodaysCount(); } }
A. A stack trace for NullPointerException is printed.
B. A stack trace for ArrayIndexOutOfBoundsException is printed.
C. A stack trace for ClassCastException is printed.
D. None of the above

26. Fill in the blanks: A ____________occurs when a program recurses too deeply into an infinite loop, while a(n) ____________occurs when a reference to a nonexistent object is acted upon.
A. NoClassDefFoundError, StackOverflowError
B. StackOverflowError, NullPointerException
C. ClassCastException, IllegalArgumentException
D. StackOverflowError, IllegalArgumentException

27. Which of the following is not a reason to add checked exceptions to a method signature?
A. To force a caller to handle or declare its exceptions
B. To notify the caller of potential types of problems
C. To ensure that exceptions never cause the application to terminate
D. To give the caller a chance to recover from a problem

28. What is the output of the following application? package peculiar; public class Stranger { public static String getFullName(String firstName, String lastName) { try { return firstName.toString() + " " + lastName.toString(); } finally { System.out.print("Finished!"); } catch (NullPointerException npe) { System.out.print("Problem?"); } return null; } public static void main(String[] things) { System.out.print(getFullName("Joyce","Hopper")); } }
A. Joyce Hopper
B. Finished!Joyce Hopper
C. Problem?Finished!null
D. None of the above

29. Fill in the blanks: A try statement has ____________finally block(s) and ____________catch blocks.
A. zero or one, zero or more
B. one, one or more
C. zero or one, zero or one
D. one or more, zero or one

30. What is the output of the following application? package pond; abstract class Duck { protected int count; public abstract int getDuckies(); } public class Ducklings extends Duck { private int age; public Ducklings(int age) { this.age = age; } public int getDuckies() { return this.age/count; } public static void main(String[] pondInfo) { Duck itQuacks = new Ducklings(5); System.out.print(itQuacks.getDuckies()); } }
A. 0
B. 5
C. The code does not compile.
D. The code compiles but throws an exception at runtime.

31. Given a try statement, if both the catch block and the finally block each throw an exception, what does the caller see?
A. The exception from the catch block
B. The exception from the finally block
C. Both the exception from the catch block and the exception from the finally block
D. None of the above

32. What is the output of the following application? package zoo; class BigCat { void roar(int level) throw RuntimeException { // m1 if(level3) throw new IllegalArgumentException("Incomplete"); System.out.print("Roar!"); } }p ublic class Lion extends BigCat { public void roar() { // m2 System.out.print("Roar!!!"); } public static void main(String[] cubs) { final BigCat kitty = new Lion(); // m3 kitty.roar(2); } }
A. The code does not compile because of line m1.
B. The code does not compile because of line m2.
C. The code does not compile because of line m3.
D. The code compiles but a stack trace is printed at runtime.

33. Given the following code snippet, which specific exception will be thrown? final Object exception = new Exception(); final Exception data = (RuntimeException)exception; System.out.print(data);
A. ClassCastException
B. RuntimeException
C. NullPointerException
D. None of the above

34. Which of the following classes will handle all types in a catch block?
A. Exception
B. Error
C. Throwable
D. RuntimeException

35. In the following application, the values of street and city have been omitted. Which one of the following is a possible output of executing this class? I. 350 5th Ave - New York II. Posted:350 5th Ave - New York package registration; public class Address { public String getAddress(String street, String city) { try { return street.toString() + " : " + city.toString(); } finally { System.out.print("Posted:"); } } public static void main(String[] form) { String street = // value omitted String city = // value omitted System.out.print(new Address().getAddress(street,city)); } }
A. I only
B. II only
C. I and II
D. None of the above

36. If a try statement has catch blocks for both ClassCastException and RuntimeException, then which of the following statements is correct?
A. The catch block for ClassCastException must appear before the catch block for RuntimeException.
B. The catch block for RuntimeException must appear before the catch block for ClassCastException.
C. The catch blocks for these two exception types can be declared in any order.
D. A try statement cannot be declared with these two catch block types because they

are incompatible. 37. Which of the following is the best scenario to use an exception?
A. The computer caught fire.
B. The code does not compile.
C. A caller passes invalid data to a method.
D. A method finishes sooner than expected.

38. What is the output of the following application? package body; class Organ { public void operate() throws RuntimeException { throw new RuntimeException("Not supported"); } }p ublic class Heart extends Organ { public void operate() throws Exception { System.out.print("beat"); } public static void main(String... cholesterol) throws Exception { try { new Heart().operate(); } finally { } } }
A. beat
B. Not supported
C. The code does not compile.
D. The code compiles but a stack trace is printed at runtime.

39. Which statement about the following exception statement is correct? throw new NullPointerException();
A. The code where this is called must include a try-catch block that handles this exception.
B. The method where this is called must declare a compatible exception.
C. This exception cannot be handled.
D. This exception can be handled with a try-catch block or ignored altogether by the

surrounding method. 40. What is the output of the following application? package clothing; public class Coat { public Long zipper() throws Exception { try { String checkZipper = (String)new Object(); } catch (Exception e) { throw RuntimeException("Broken!"); } return null; } public static void main(String... warmth) { try { new Coat().zipper(); System.out.print("Finished!"); } catch (Throwable t) {} } }
A. Finished!
B. Finished!, followed by a stack trace
C. The application does not produce any output at runtime.
D. The code does not compile.

41. Given the following application, which type of exception will be printed in the stack trace at runtime? package carnival; public class WhackAnException { public static void main(String... hammer) { try { throw new ClassCastException(); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(); } catch (RuntimeException e) { throw new NullPointerException(); } finally { throw new RuntimeException(); } } }
A. IllegalArgumentException
B. NullPointerException
C. RuntimeException
D. The code does not compile.

42. Which of these method signatures is allowed in a class implementing the Outfielder interface? class OutOfBoundsException extends BadCatchException {} class BadCatchException extends Exception {} public interface Outfielder { public void catchBall() throws OutOfBoundsException; }
A. public int catchBall() throws OutOfBoundsException
B. public int catchBall() throws BadCatchException
C. public int catchBall() throws Exception
D. None of the above

43. What is the output of the following application? package city; public class Street { public static void dancing() throws RuntimeException { try { throw new IllegalArgumentException(); } catch (Error) { System.out.print("Unable!"); } } public static void main(String... count) throws RuntimeException { dancing(); } }
A. Unable!
B. The application does not produce any output.
C. The application compiles but produces a stack trace at runtime.
D. The code does not compile.

44. What is the result of compiling and running the following application? package castles; class DragonException extends Exception {} public class Lair { public void openDrawbridge() throws Exception { // r1 try { throw new Exception("This Exception"); } catch (RuntimeException e) { throw new DragonException(); // r2 } finally { throw new RuntimeException("Or maybe this one"); } } public static void main(String[] moat) throws Exception { new Lair().openDrawbridge(); // r3 } }
A. The code does not compile because of line r1.
B. The code does not compile because of line r2.
C. The code does not compile because of line r3.
D. The code compiles, but a stack trace is printed at runtime.

45. If a try statement has catch blocks for both IllegalArgumentException and ClassCastException, then which of the following statements is correct?
A. The catch block for IllegalArgumentException must appear before the catch block for ClassCastException.
B. The catch block for ClassCastException must appear before the catch block for IllegalArgumentException.
C. The catch blocks for these two exception types can be declared in any order.
D. A try statement cannot be declared with these two catch block types because they

are incompatible. 46. What is the output of the following application? package broken; class Problem implements RuntimeException {} public class BiggerProblem extends Problem { public static void main(String uhOh[]) { try { throw new BiggerProblem(); } catch (BiggerProblem re) { System.out.print("Problem?"); } catch (Problem e) { System.out.print("Handled"); } finally { System.out.print("Fixed!"); } } }
A. Problem?Fixed!
B. Handled.Fixed!
C. Problem?Handled.Fixed!
D. The code does not compile.

47. What is the output of the following application? package lighting; interface Source { void flipSwitch() throws Exception; }public class LightBulb implements Source { public void flipSwitch() { try { throws new RuntimeException("Circuit Break!"); } finally { System.out.print("Flipped!"); } } public static void main(String... electricity) throws Throwable { final Source bulb = new LightBulb(); bulb.flipSwitch(); } }
A. A stack trace for a RuntimeException
B. Flipped!, followed by a stack trace for a RuntimeException
C. The code does not compile because flipSwitch() is an invalid method override.
D. The code does not compile for another reason.

48. Given an application that hosts a website, which of the following would most likely result in a java.lang.Error being thrown?
A. Two users try to register an account at the same time.
B. The application temporarily loses connection to the network.
C. A user enters their password incorrectly.
D. The application runs out of memory.

49. Given that FileNotFoundException is a subclass of IOException, what is the output of the following application? package storage; import java.io.*; public class Backup { public void performBackup() { try { throw new IOException("Disk not found"); } catch (Exception e) { try { throw new FileNotFoundException("File not found"); } catch (FileNotFoundException e) { // z1 System.out.print("Failed"); } } } public static void main(String... files) { new Backup().performBackup(); // z2 } }
A. Failed
B. The application compiles but a stack trace is printed at runtime.
C. The code does not compile because of line z1.
D. The code does not compile because of line z2.

50. What is the output of the following application? package bed; public class Sleep { public static void snore() { try { String sheep[] = new String[3]; System.out.print(sheep[3]); } catch (RuntimeException e) { System.out.print("Awake!"); } finally { throw new Exception(); // x1 } } public static void main(String... sheep) { // x2 new Sleep().snore(); // x3 } }
A. Awake!, followed by a stack trace
B. The code does not compile because of line x1.
C. The code does not compile because of line x2.
D. The code does not compile because of line x3

OCJP Practice Test, Certification Path, Tips/Tricks

  1. Oracle (OCJP) Java Certification Exam
  2. Java Certification Tips And Tricks
  3. OCJP Practice Papers - Java Basics
  4. OCJP Practice Papers - Operators And Decision Constructs
  5. OCJP Practice Papers-A Advanced Java Class Design
  6. OCJP Practice Papers Creating And Using Arrays
  7. OCJP Practice Papers Using Loop Constructs
  8. OCJP Practice Papers - Generics And Collections
  9. OCJP Practice Papers - Lambda Built-In Functional Interfaces
  10. OCJP Practice Papers Java Class Design
  11. OCJP Practice Papers - Java Stream API
  12. OCJP Practice Papers - Exceptions And Assertions
  13. OCJP Practice Papers - Date/Time API
  14. OCJP Practice Papers - Java I/O Fundamentals
  15. OCJP Practice Papers - Working With Methods And Encapsulation
  16. OCJP Practice Papers - Working With Inheritance
  17. OCJP Practice Papers - Handling Exceptions
  18. OCJP Practice Papers - Selected Classes From Java API
  19. OCJP Practice Papers - Java File I/O Nio.2
  20. OCJP Practice Papers - Java Concurrency