THE OCA EXAM TOPICS COVERED IN THIS PRACTICE TEST INCLUDE THE FOLLOWING:
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();
}
}
Choose the answer that lists the keywords in the order that they would be used together.
Which of the following diagrams of java.lang classes shows the inheritance model properly?
Which of the following Throwable types is it recommended not to catch in a Java application?
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++);
}
}
Which of the following is a checked exception?
Fill in the blanks: The ____________keyword is used in method declarations, while the ____________keyword is used to throw an exception to the surrounding process.
If a try statement has catch blocks for both Exception and IOException, then which of the following statements is correct?
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 aioobe;
} finally {
System.out.print('C');
}
}
}
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
}
}
Which of the following exception types must be handled or declared by the method in which they are thrown?
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);
}
}
Which of the following statements about a finally block is true?
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();
}
}
Which keywords are required with a try statement?
Which statement about the role of exceptions in Java is incorrect?
What is the output of the following application?
package harbor;
class CapsizedException extends Exception {}
class Transport {
public int travel() throws CapsizedException { return 2; };
}
public 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);
)
}
}
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;
}
Which import statement is required to be declared in order to use the Exception, RuntimeException, and Throwable classes in an application?
Which statement about the following classes is correct?
class GasException extends Exception {}
class Element {
public int getSymbol() throws GasException { return -1; } // g1
}
public 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");
}
}
}
Fill in the blanks: A program must handle or declare ____________but should never handle ____________.
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
}
}
If an exception matches two or more catch blocks, which catch block is executed?
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;
}
}
}
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; i<10; i++) {
if(list[i]) ++count;
}
return count;
}
public static void main(String[] roster) {
new Attendance().printTodaysCount();
}
}
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.
Which of the following is not a reason to add checked exceptions to a method signature?
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"));
}
}
Fill in the blanks: A try statement has ____________finally block(s) and ____________catch blocks.
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());
}
}
Given a try statement, if both the catch block and the finally block each throw an exception, what does the caller see?
What is the output of the following application?
package zoo;
class BigCat {
void roar(int level) throw RuntimeException { // m1
if(level<3) throw new IllegalArgumentException("Incomplete");
System.out.print("Roar!");
}
}
public 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);
}
}
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);
Which of the following classes will handle all types in a catch block?
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?
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));
}
}
If a try statement has catch blocks for both ClassCastException and RuntimeException, then which of the following statements is correct?
Which of the following is the best scenario to use an exception?
What is the output of the following application?
package body;
class Organ {
public void operate() throws RuntimeException {
throw new RuntimeException("Not supported");
}
}
public 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 {
}
}
}
Which statement about the following exception statement is correct?
throw new NullPointerException();
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) {}
}
}
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();
}
}
}
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;
}
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();
}
}
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
}
}
If a try statement has catch blocks for both IllegalArgumentException and ClassCastException, then which of the following statements is correct?
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!");
}
}
}
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();
}
}
Given an application that hosts a website, which of the following would most likely result in a java.lang.Error being thrown?
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
}
}
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
}
}