THE OCP EXAM TOPICS COVERED IN THIS PRACTICE TEST INCLUDE THE FOLLOWING:
If a try, a catch, and a finally statement are used together but no exception is generated, which blocks are executed and in which order?
Fill in the blanks: A try statement__________ a catch or a finally block, while a try-with-resources statement __________.
What is the output of the following application?
package park;
class LostBallException extends Exception {}
public class Ball {
public void toss() throw LostBallException {
throw new ArrayStoreException();
}
public static void main(String[] bouncy) {
try {
new Ball().toss();
} catch (Throwable e) {
System.out.print("Caught!");
}
}
}
Which symbol(s) can be used to separate exception types in a multi-catch statement?
What is the result of executing the following application with assertions enabled?
1: package ice;
2: public class Igloo {
3: public static void main(String[] bricks) {
4: int flakes = 10;
5: double assert = 7.0;
6: assert (true :"");
7: assert flakes++>5;
8: }
9: }
Which of the following classes is a checked exception?
How many constructors in WhaleSharkException compile in the following class?
package friendly;
public class WhaleSharkException extends Exception {
public WhaleSharkException() {
super("Friendly shark!");
}
public WhaleSharkException(String message) {
super(new Exception(new WhaleSharkException()));
}
public WhaleSharkException(Exception cause) {}
}
Given the following class diagram, which two classes are missing in the hierarchy starting with the bottom and going upward?
How many lines of text does the following program print?
package lighting;
import java.io.IOException;
public class Light {
public void turnOn() throws IOException {
new IOException("Not ready");
}
public static void main(String[] b) throws Exception {
try {
new Light().turnOn();
} catch (RuntimeException b) { // y1
System.out.println(b);
throw new IOException(); // y2
} finally {
System.out.println("complete");
}
}
}
Which statement, when inserted into the main() method of a program, guarantees an AssertionError will be thrown at runtime?
What is the output of the following application?
package paper;
import java.io.Closeable;
public class PrintCompany {
class Printer implements Closeable { // r1
public void print() {
System.out.println("This just in!");
}
public void close() {}
}
public void printHeadlines() {
try {Printer p = new Printer()} { // r2
p.print();
}
}
public static void main(String[] headlines) {
new PrintCompany().printHeadlines(); // r3
}
}
Which statement about try-with-resources is not true?
How many lines of text does the following program print?
package bee;
class SpellingException extends RuntimeException {}
public class SpellChecker {
public final static void main(String... participants) {
try {
if(!"cat".equals("kat")) {
new SpellingException();
}
} catch (SpellingException | NullPointerException e) {
System.out.println("Spelling problem!");
} catch (Exception e) {
System.out.println("Unknown Problem!");
} finally {
System.out.println("Done!");
}
}
}
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;
}
}
}
Which statement about Closeable and AutoCloseable is true?
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 (________________) {}
}
}
Which statement about a multi-catch statement is true?
Given the following class, how many lines contain compilation errors?
package move;
interface Closing {
void close() throws Exception;
}
class 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();
}
}
}
Which of the following is not true of using a try-with-resources statement?
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.score>2) : johnny5.score++;
assert johnny5.score>=5 : System.out.print("No input");
System.out.print("Made it!");
}
}
Which of the following classes is an unchecked exception?
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");
}
}
}
Which of the following is not a command that enables or disables assertions at runtime?
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";}
}
class MakeSign implements AutoCloseable {
public void close() {}
public void send(String message) {
System.out.print(message);
}
}
public class Translate {
public static void main(String... hands) {
try (ReadSign r = new ReadSign();
MakeSign w = new MakeSign();) {
w.send(r.get());
}
}
}
What is the output of the following application?
package what;
class FunEvent implements AutoCloseable {
public void close() {
System.out.print("1");
}
}
public 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");
}
}
}
Which statement best describes how a class that implements the AutoCloseable interface should be written?
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");
}
}
Which of the following is not a good use of assertions?
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;
}
}
}
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 {}
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);
}
}
public 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);
}
}
}
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 (_______________) {
}
}
}
Given the following two variables, which assertion statement compiles successfully?
int age = 22;
final String name = "Josephine";
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");
}
}
}
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();
}
}
What is the output of the following application?
package classical;
import java.io.*;
class OutOfTuneException extends Exception {
OutOfTuneException(String message) { super(message); }
}
public 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!");
}
}
}
Given the following class, which command causes the class to throw an AssertionError at runtime?
public class Falcon extends Exception {
private int parsec = 12;
public Falcon(String name) {
super(name);
}
public static void main(String[] aluminum) {
assert new Falcon(null).parsec<12;
}
}
What is the output of the following application?
package db;
import java.io.*;
import java.sql.*;
public class DatabaseHelper {
static class MyDatabase implements Closeable {
public void close() throws SQLException {
System.out.print("2");
}
public void write(String data) {}
public String read() {return null;}
}
public static void main(String... files) throws Exception {
try (MyDatabase myDb = new MyDatabase()) {
// TODO: Decide what to read/rite
} finally {
System.out.print("1");
}
}
}
How many lines of text does the following program print?
package tron;
class DiskPlayer implements AutoCloseable {
public void close() throws Exception {}
}
public class LightCycle {
public static void main(String... bits) {
try (DiskPlayer john = new DiskPlayer()) {
System.out.println("ping");
} finally {
System.out.println("pong");
}
System.out.println("return");
}
}
Given the application below, what is the name of the class printed at line e1?
package canyon;
final class FallenException extends Exception {}
final class HikingGear implements AutoCloseable {
@Override public void close() throws Exception {
throw new FallenException();
}
}
public class Cliff {
public final void climb() throws Exception {
try (HikingGear gear = new HikingGear()) {
throw new RuntimeException();
}
}
public static void main(String... rocks) {
try {
new Cliff().climb();
} catch (Throwable t) {
System.out.println(t); // e1
}
}
}