OCJP Practice Papers - Exceptions And Assertions
OCJP Practice Papers Java Stream API include following topics
- Use try-catch and throw statements
- Use catch, multi-catch, and finally clauses
- Use Autoclose resources with a try-with-resources statement
- Create custom exceptions and Auto-closeable resource
- Test invariants by using assertions
See the complete syllabus for OCJP here
1. If a try, a catch, and a finally statement are used together but no exception isgenerated, which blocks are executed and in which order?
A. try
B. try, catch
C. try, catch, finally
D. try, finally
2. Fill in the blanks: A try statement__________ a catch or a finally block, while atry-with-resources statement __________.
A. is not required to contain, is not required to contain either
B. is not required to contain, must contain one of them
C. must contain, is not required to contain either
D. must contain, must contain a catch block
3. What is the output of the following application?
A. Caught!
B. The code does not compile because LostBallException is not handled or declared
in the main() method.
C. The code does not compile because ArrayStoreException is not handled or
declared in the toss() method.
D. The code does not compile for a different reason.
4. Which symbol(s) can be used to separate exception types in a multi-catch statement?
I. &
II. |
III. ||
A. II only
B. III only
C. II and III
D. I, II, and III
5. What is the result of executing the following application with assertions enabled?
A. It throws an AssertionError at runtime.
B. It prints nothing at runtime.
C. Exactly one line of code does not compile.
D. Two lines of code do not compile.
6. Which of the following classes is a checked exception?
A. java.lang.Error
B. java.lang.IllegalStateException
C. java.text.ParseException
D. java.lang.RuntimeException
7. How many constructors in WhaleSharkException compile in the following class?
A. None
B. One
C. Two
D. Three
8. Given the following class diagram, which two classes are missing in the hierarchystarting with the bottom and going upward?
A. IOException, Exception
B. RuntimeException, Exception
C. IllegalArgumentException, RuntimeException
D. IllegalStateException, RuntimeException
9. How many lines of text does the following program print?
A. One
B. Two
C. The code does not compile because of line y1.
D. The code does not compile because of line y2.
10. Which statement, when inserted into the main() method of a program, guarantees anAssertionError will be thrown at runtime?
A. assert(0,"Invalid");
B. assert 0==1;
C. assert 0==0;
D. None of the above
11. What is the output of the following application?
A. This just in!
B. The code does not compile because of line r1.
C. The code does not compile because of line r2.
D. The code does not compile because of line r3.
12. Which statement about try-with-resources is not true?
A. If the try block and close() method both throw an exception, the one thrown bythe close() method is suppressed.
B. A catch block is not required.
C. If more than one resource is used, the resources are closed in the order they were created.
D. Parentheses are used for the resource declaration section, even if more than oneresource is used.
13. How many lines of text does the following program print?
A. One
B. Two
C. Three
D. The code does not compile.
14. Which exception classes, when inserted into the blank in the Problems class, allow the
code to compile?
package more;
class MissingMoneyException extends Exception {}
class MissingFoodException extends Exception {}
public class Problems {
public void doIHaveAProblem() throws MissingMoneyException,
MissingFoodException {
System.out.println("No problems");
}
public static void main(String[] lots) throws ________________{
try {
final Problems p = new Problems();
p.doIHaveAProblem();
} catch (Exception e) {
throw e;
}
}
}
I. Exception
II. MissingMoneyException
III. MissingMoneyException, MissingFoodException
A. I only
B. III only
C. I and III
D. I, II, and II
15. Which statement about Closeable and AutoCloseable is true?
A. AutoCloseable extends Closeable.
B. The close() method in a class that implements AutoCloseable cannot throw an
IOException.
C. The close() method in a class that implements Closeable cannot throw an
Exception.
D. There is no difference; one was added for backward compatibility.
16. Which expression, when inserted into the blank in the following class, allows the code
to compile?
package sun;
import java.io.*;
public class Beach {
class TideException extends Exception {}
public void surf() throws RuntimeException {
try {
throw new TideException();
} catch (________________) {}
}
}
A. Exception a | RuntimeException f
B. IllegalStateException | TideException t
C. TideException | IOException i
D. TideException | Exception x
17. Which statement about a multi-catch statement is true?
A. The exception types must be ordered from broadest to narrowest.
B. The exception types must be ordered from narrowest to broadest.
C. The variable of a multi-catch block with more than one exception cannot be
reassigned within the block.
D. The variable of a multi-catch block with one exception type cannot be reassigned
within the block.
18. Given the following class, how many lines contain compilation errors?
package move;
interface Closing {
void close() throws Exception;
}c
lass Shelf implements Closing {
public void close() throws Exception {}
}
public class Step {
static {
try (Shelf shelf = new Shelf()) {
throws new IllegalArgumentException();
} catch (Exception e) {
} catch (IllegalArgumentException e) {
} finally {
shelf.close();
}
}
}
A. None
B. Two
C. Three
D. Four
19. Which of the following is not true of using a try-with-resources statement?
A. Associated catch blocks are run before the declared resources have been closed.
B. It is compatible with all classes that implement the AutoCloseable interface.
C. It is compatible with all classes that implement the Closeable interface.
D. It shortens the amount of code a developer must write.
20. Assuming the following application is executed with assertions enabled, what is the
result?
package input;
public class DataIntegrity {
private int score;
public DataIntegrity() {
super();
DataIntegrity.this.score = 5;
}
public static void main(String[] books) {
final DataIntegrity johnny5 = new DataIntegrity();
assert(johnny5.score2) : johnny5.score++;
assert johnny5.score=5 : System.out.print("No input");
System.out.print("Made it!");
}
}
A. An AssertionError is thrown with a message of 5.
B. An AssertionError is thrown with a message of No input.
C. Made it! is printed.
D. The code does not compile.
21. Which of the following classes is an unchecked exception?
A. java.io.IOException
B. java.io.NotSerializableException
C. java.sql.SQLException
D. java.util.MissingResourceException
22. What is the result of compiling and executing the following class?
package wind;
public class Storm {
public static void main(String... rain) throws Exception {
try (final AutoCloseable weatherTracker = new AutoCloseable() {
public void close() throws RuntimeException {}
}) {
System.out.println(weatherTracker.toString());
} catch (Exception e) {
if(weatherTracker != null) {
weatherTracker.close();
}
} finally {
System.out.println("Storm gone");
}
}
}
A. It prints one line.
B. It prints two lines.
C. It does not compile due to an error in the declaration of the weatherTracker
resource.
D. It does not compile for a different reason.
23. Which of the following is not a command that enables or disables assertions at
runtime?
A. -di
B. -disableassertions
C. -ea
D. -enableassertions
24. What is the output of the following application?
package signlanguage;
import java.io.Closeable;
class ReadSign implements Closeable {
public void close() {}
public String get() {return "Hello";}
}c
lass MakeSign implements AutoCloseable {
public void close() {}
public void send(String message) {
System.out.print(message);
}
}p
ublic class Translate {
public static void main(String... hands) {
try (ReadSign r = new ReadSign();
MakeSign w = new MakeSign();) {
w.send(r.get());
}
}
}
A. Hello
B. The code does not compile because of the ReadSign class.
C. The code does not compile because of the try-with-resources statement.
D. None of the above
25. What is the output of the following application?
package what;
class FunEvent implements AutoCloseable {
public void close() {
System.out.print("1");
}
}p
ublic class Happening {
public static void main(String... lots) {
try (FunEvent f = new FunEvent()) {
System.out.print("2");
throw new ArithmeticException();
} catch (Exception e) {
System.out.print("3");
} finally {
System.out.print("4");
}
}
}
A. 214
B. 2134
C. 2314
D. The code does not compile.
26. Which statement best describes how a class that implements the AutoCloseable
interface should be written?
A. The close() method is optional since the AutoCloseable interface defines a
default implementation.
B. The close() method should avoid modifying data after it has been run once.
C. The close() method should not throw any exceptions.
D. The close() method should return a status code.
27. Which statement about the following program is correct?
package dogpark;
public class Fetch {
public int play(String dogName) throws Exception {
try {
throw new RuntimeException(dogName);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static final void main(String[] ball) throws RuntimeException {
new Fetch().play("Webby");
new Fetch().play("Georgette");
}
}
A. The program prints one exception at runtime.
B. The program prints two exceptions at runtime.
C. The class does not compile because of the play() method.
D. The class does not compile because of the main() method.
28. Which of the following is not a good use of assertions?
A. Check method post conditions.
B. Modify local variables.
C. Test control flow invariants.
D. Validate class invariants.
29. Which statement about the following application is correct?
package highway;
import java.io.*;
class CarCrash extends RuntimeException {}
public class Car {
public static void main(String[] seatbelts) throws Exception { // w1
try {
throw new IOException("Auto-pilot error");
} catch (Exception | CarCrash e) { // w2
throw e;
} catch (Exception a) { // w3
throw a;
}
}
}
A. The code does not compile because of line w1.
B. The code does not compile because of line w2.
C. The code does not compile because of line w3.
D. The code compiles and runs without issue.
30. Which statements about the following classes are true?
public class Dopey extends Grumpy {}
public class Grumpy extends Exception {}
public class Happy extends IOException {}
public class Sleepy extends IllegalStateException {}
public class Sneezy extends Throwable {}
I. Four of the classes are checked exceptions.
II. Two of the classes are unchecked exceptions.
III. None of the class declarations contain any compilation errors.
A. I only
B. I and III
C. II and III
D. I, II, and III
31. What is the output of the following application?
package vortex;
class TimeException extends Exception {}
class TimeMachine implements AutoCloseable {
int v;
public TimeMachine(int v) {this.v = v;}
public void close() throws Exception {
System.out.print(v);
}
}p
ublic class TimeTraveler {
public static void main(String[] twelve) {
try (TimeMachine timeSled = new TimeMachine(1);
TimeMachine delorean = new TimeMachine(2);
TimeMachine tardis = new TimeMachine(3)) {
} catch (TimeException e) {
System.out.print(4);
} finally {
System.out.print(5);
}
}
}
A. 1235
B. 3215
C. 41235
D. The code does not compile.
32. Which expression, when inserted into the blank in the following class, allows the code
to compile?
package music;
public class Bells {
class Player implements AutoCloseable {
@Override public void close() throws RingException {}
}
class RingException extends Exception {
public RingException(String message) {}
}
public static void main(String[] notes) throws Throwable {
try (Player p = null) {
throw new Exception();
} catch (Exception e) {
} catch (_______________) {
}
}
}
A. Error r
B. IllegalStateException b
C. RingException p
D. The code does not compile regardless of the expression used.
33. Given the following two variables, which assertion statement compiles successfully?
int age = 22;
final String name = "Josephine";
A. assert (age=2);
B. assert age!=age : (1age ? "Error" : 10);
C. assert name.equals("") : () - "Oops";
D. assert name.length()(long)age : return "Mistake";
34. Which statement about the following program is true?
package tag;
class MissedCallException extends Exception {}
public class Phone {
static void makeCall() throws RuntimeException {
throw new ArrayIndexOutOfBoundsException("Call");
}
public static void main(String[] messages) {
try {
makeCall();
} catch (MissedCallException e) {
throw new RuntimeException("Voicemail");
} finally {
throw new RuntimeException("Text");
}
}
}
A. An exception is printed at runtime with Call in the message.
B. An exception is printed at runtime with Voicemail in the message.
C. An exception is printed at runtime with Text in the message.
D. The code does not compile.
35. Which statement about the following program is correct?
package fairy;
public class Tale {
class BearException extends RuntimeException {}
class WolfException extends RuntimeException {}
class DragonException extends RuntimeException {}
public int tellStory() {
try {} catch (BearException d) {
d = new RuntimeException();
throw d;
} catch (WolfException | DragonException e) {
e = new RuntimeException();
throw e;
}
return 3;
}
public static void main(String... wand) throws RuntimeException{
new Tale().tellStory();
}
}
A. The class compiles and does not print anything at runtime.
B. The code does not compile solely due to the first catch block in tellStory().
C. The code does not compile solely due to the second catch block in tellStory().
D. The code does not compile due to errors in both catch blocks in tellStory().
36. What is the output of the following application?
package classical;
import java.io.*;
class OutOfTuneException extends Exception {
OutOfTuneException(String message) { super(message); }
}p
ublic class Piano {
public void play() throws OutOfTuneException, FileNotFoundException {
throw new OutOfTuneException("Sour note!");
}
public static void main(String... keys) throws OutOfTuneException {
final Piano piano = new Piano();
try {
piano.play();
} catch (Exception e) {
throw e;
} finally {
System.out.println("Song finished!");
}
}
}
A. Song finished!
B. An exception is printed with Sour note! in the stack trace.
C. Both of the above
D. None of the above
37. Given the following class, which command causes the class to throw anAssertionError at runtime?
A. java Falcon
B. java -ea -da:Falcon Falcon
C. java -da -ea:Falcon Falcon
D. The code does not compile.
38. What is the output of the following application?
A. 12
B. 21
C. The code does not compile because of the MyDatabase class.
D. The code does not compile because of the try-with-resources statement.
39. How many lines of text does the following program print?
A. One
B. Two
C. Three
D. The code does not compile.
40. Given the application below, what is the name of the class printed at line e1?
A. canyon.FallenException
B. java.lang.RuntimeException
C. The code does not compile.
D. The code compiles, but the answer cannot be determined until runtime.
OCJP Practice Test, Certification Path, Tips/Tricks
- Oracle (OCJP) Java Certification Exam
- Java Certification Tips And Tricks
- OCJP Practice Papers - Java Basics
- OCJP Practice Papers - Operators And Decision Constructs
- OCJP Practice Papers-A Advanced Java Class Design
- OCJP Practice Papers Creating And Using Arrays
- OCJP Practice Papers Using Loop Constructs
- OCJP Practice Papers - Generics And Collections
- OCJP Practice Papers - Lambda Built-In Functional Interfaces
- OCJP Practice Papers Java Class Design
- OCJP Practice Papers - Java Stream API
- OCJP Practice Papers - Exceptions And Assertions
- OCJP Practice Papers - Date/Time API
- OCJP Practice Papers - Java I/O Fundamentals
- OCJP Practice Papers - Working With Methods And Encapsulation
- OCJP Practice Papers - Working With Inheritance
- OCJP Practice Papers - Handling Exceptions
- OCJP Practice Papers - Selected Classes From Java API
- OCJP Practice Papers - Java File I/O Nio.2
- OCJP Practice Papers - Java Concurrency