This chapter contains 80 questions and is designed to simulate a real OCA exam. While previous chapters were focused on a specific set of objectives, this chapter covers all of the objectives on the exam. We recommend you take this exam only after you score well on the questions in the individual chapters.
For this chapter, you should try to simulate the real exam experience as much as possible. This means setting aside 150 minutes of uninterrupted time to complete the test, as well as not looking at any reference material while taking the exam. If you don’t know an answer to a question, complete it as best you can and move on to the next question, just as you would on a real exam.
Remember, the exam permits writing material, such as a whiteboard. If you do not have a whiteboard handy, you can just use blank sheets of paper and a pencil. If you do well on this test, then you are hopefully ready to take the real exam. With that said, good luck!
What is the output if this class is run with java Indexing cars carts?
public class Indexing {
public static void main(String... books) {
StringBuilder sb = new StringBuilder();
for (String book : books)
sb.insert(sb.indexOf("c"), book);
System.out.println(sb);
}
}
Fill in the blanks: The operators +=,__________ ,__________ ,__________ ,__________ , and ++ are listed in increasing or the same level of operator precedence. (Choose two.)
Which of the following are valid JavaBean signatures? (Choose three.)
Which of the following are true? (Choose two.)
20: int[] crossword [] = new int[10][20];
21: for (int i = 0; i < crossword.length; i++)
22: for (int j = 0; j < crossword.length; j++)
23: crossword[i][j] = 'x';
24: System.out.println(crossword.size());
Which of the following statements about java.lang.Error are most accurate? (Choose two.)
Given a class that uses the following import statements, which class would be automatically accessible without using its full package name? (Choose three.)
import forest.Bird;
import jungle.tree.*;
import savana.*;
How many of the following variables represent immutable objects?
ArrayList l = new ArrayList();
String s = new String();
StringBuilder sb = new StringBuilder();
LocalDateTime t = LocalDateTime.now();
What is the output of the following?
StringBuilder builder = new StringBuilder("Leaves growing");
do {
builder.delete(0, 5);
} while (builder.length() > 5);
System.out.println(builder);
What is the output of the following application?
package reality;
public class Equivalency {
public static void main(String[] edges) {
final String ceiling = "up";
String floor = new String("up");
final String wall = new String(floor);
System.out.print((ceiling==wall) +" "+(floor==wall) +" "+ceiling.equals(wall));
}
}
How many times does the following code print true?
1: public class Giggles {
2: public static void main(String[] args) {
3: String lol = "lol";
4: System.out.println(lol.toUpperCase() == lol);
5: System.out.println(lol.toUpperCase() == lol.toUpperCase());
6: System.out.println(lol.toUpperCase().equals(lol));
7: System.out.println(lol.toUpperCase().equals(lol.toUpperCase()));
8: System.out.println(lol.toUpperCase().equalsIgnoreCase(lol));
9: System.out.println(lol.toUpperCase()
10: .equalsIgnoreCase(lol.toUpperCase()));
11: } }
Which lines can be removed together without stopping the code from compiling and while printing the same output? (Choose three.)
14: String race = "";
15: outer:
16: do {
17: inner:
18: do {
19: race += "x";
20: } while (race.length() <= 4);
21: } while (race.length() < 4);
22: System.out.println(race);
Which of the following do not compile when filling in the blank? (Choose two.)
long bigNum = ____________;
How many lines does this program print?
import java.time.*;
public class OnePlusOne {
public static void main(String... nums) {
LocalTime time = LocalTime.of(1, 11);
while (time.getHour() < 1) {
time.plusHours(1);
System.out.println("in loop");
}
}
}
What is the result of running the following program?
1: package fun;
2: public class Sudoku {
3: static int[][] game;
4:
5: public static void main(String args[]) {
6: game[3][3] = 6;
7: Object[] obj = game;
8: obj[3] = 'X';
9: System.out.println(game[3][3]);
10: }
11: }
Which of the following use generics and compile without warnings? (Choose two.)
Which of the following are true right before the main() method ends? (Choose two.)
public static void main(String[] args) {
String shoe1 = new String("sandal");
String shoe2 = new String("flip flop");
String shoe3 = new String("croc");
shoe1 = shoe2;
shoe2 = shoe3;
shoe3 = shoe1;
}
How many lines of the following application do not compile?
package ocean;
class BubbleException extends Exception {}
class Fish {
Fish getFish() throws BubbleException {
throw new RuntimeException("fish!");
}
}
public final class Clownfish extends Fish {
public final Clownfish getFish() {
throw new RuntimeException("clown!");
}
public static void main(String[] bubbles) {
final Fish f = new Clownfish();
f.getFish();
System.out.println("swim!");
}
}
How many lines does this code output?
import java.util.*;
import java.util.function.*;
public class PrintNegative {
public static void main(String[] args) {
List<Integer> list= new ArrayList<>();
list.add(-5);
list.add(0);
list.add(5);
print(list, e -> e < 0);
}
public static void print(List<Integer> list, Predicate<Integer> p) {
for (Integer num : list)
if (p.test(num))
System.out.println(num);
}
}
Which keywords are required with a try statement?
What is the output of the following?
12: int result = 8;
13: loop: while (result > 7) {
14: result++;
15: do {
16: result--;
17: } while (result > 5);
18: break loop;
19: }
20: System.out.println(result);
What is the result of compiling and executing the following application?
package reptile;
public class Alligator {
static int teeth;
double scaleToughness;
public Alligator() {
teeth++;
}
public void snap(int teeth) {
System.out.print(teeth+" ");
teeth--;
}
public static void main(String[] unused) {
new Alligator().snap(teeth);
new Alligator().snap(teeth);
}
}
What is the output of the following?
public class Costume {
public static void main(String[] black) {
String witch = "b";
String tail = "lack";
witch.concat(tail);
System.out.println(witch);
}
}
Which modifiers can be independently applied to an interface method? (Choose three.)
What is the output of the following?
public class Shoelaces {
public static void main(String[] args) {
String tie = null;
while (tie = null)
tie = "shoelace";
System.out.print(tie);
}
}
What statements are true about compiling a Java class file? (Choose two.)
What is the output of the following application?
package woods;
interface Plant {
default String grow() { return "Grow!"; }
}
interface Living {
public default String grow() { return "Growing!"; }
}
public class Tree implements Plant, Living { // m1
public String grow(int height) { return "Super Growing!"; }
public static void main(String[] leaves) {
Plant p = new Tree(); // m2
System.out.print(((Living)p).grow()); // m3
}
}
What is the result of the following?
public static void main(String... args) {
String name = "Desiree";
int _number = 694;
boolean profit$$$;
System.out.println(name + " won. "
+ _number + " profit? " + profit$$$);
}
Fill in the blanks: Given a variable x, __________ decreases the value of x by 1 and returns the original value, while __________ increases the value of x by 1 and returns the new value.
Given the following two classes in the same package, which constructors contain compiler errors? (Choose three.)
public class Big {
public Big(boolean stillIn) {
super();
}
}
public class Trouble extends Big {
public Trouble() {}
public Trouble(int deep) {
super(false);
this();
}
public Trouble(String now, int... deep) {
this(3);
}
public Trouble(long deep) {
this("check",deep);
}
public Trouble(double test) {
super(test>5 ? true : false);
}
}
Which of the following can replace the comment so this code outputs 100? (Choose two.)
public class Stats {
// INSERT CODE
public static void main(String[] math) {
System.out.println(max - min);
}
}
Which of the following statements are true about Java operators and statements? (Choose two.)
What is the output of the following?
1: public class Legos {
2: public static void main(String[] args) {
3: StringBuilder sb = new StringBuilder();
4: sb.append("red");
5: sb.deleteCharAt(0);
6: sb.delete(1, 1);
7: System.out.println(sb);
8: }
9: }
Which of the following is a valid method name in Java? (Choose two.)
Which of the following statements about inheritance are true? (Choose two.)
Which of the following statements about Java are true?
What is the output of the following?
String[][] listing = new String[][] { { "Book", "34.99" },
{ "Game", "29.99" }, { "Pen", ".99" } };
System.out.println(listing.length + " " + listing[0].length);
Which of the following variable types is permitted in a switch statement? (Choose three.)
What does the following do?
public class Shoot {
interface Target {
boolean needToAim(double angle);
}
static void prepare(double angle, Target t) {
boolean ready = t.needToAim(angle); // k1
System.out.println(ready);
}
public static void main(String[] args) {
prepare(45, d => d > 5 || d < -5); // k2
}
}
Which of the following is a valid code comment in Java? (Choose three.)
Given the following two classes, each in a different package, which lines allow the second class to compile when inserted independently? (Choose two.)
package food;
public class Grass {
public static int seeds = 10;
public static Grass getGrass() {return new Grass();}
}
package woods;
// INSERT CODE HERE
public class Deer {
public void eat() {
getGrass();
System.out.print(seeds);
}
}
What is the result of the following?
import java.util.*;
public class Museums {
public static void main(String[] args) {
String[] array = {"Natural History", "Science", "Art"};
List<String> museums = Arrays.asList(array);
museums.remove(2);
System.out.println(museums);
}
}
Which of the following substitutions will compile? (Choose two.)
public class Underscores {
public String name = "Sherrin";
public void massage() {
int zip = 10017;
}
}
What is the result of the following when called as java counting.Binary?
package counting;
import java.util.*;
public class Binary {
public static void main(String[] args) {
args = new String[] {"0", "1", "01", "10" };
Arrays.sort(args);
System.out.println(Arrays.toString(args));
}
}
Fill in the blanks: Using the _____________ and _____________ modifiers together allows a variable to be accessed from any class, without requiring an instance variable.
How many lines does the following code output?
import java.util.*;
public class Exams {
public static void main(String[] args) {
List<String> exams = Arrays.asList("OCA", "OCP");
for (String e1 : exams)
for (String e2 : exams)
System.out.print(e1 + " " + e2);
System.out.println();
}
}
Which of the following are true statements? (Choose two.)
How many of the following lines of code compile?
char one = Integer.parseInt("1");
Character two = Integer.parseInt("2");
int three = Integer.parseInt("3");
Integer four = Integer.parseInt("4");
short five = Integer.parseInt("5");
Short six = Integer.parseInt("6");
Given the application below, what data types can be inserted into the blank that would allow the code to print 3? (Choose three.)
public class Highway {
public int drive(long car) { return 2; }
public int drive(double car) { return 3; }
public int drive(int car) { return 5; }
public int drive(short car) { return 3; }
public static void main(String[] gears) {
____________ value = 5;
System.out.print(new Highway().drive(value));
}
}
How many times does this code print true?
import java.time.*;
public class Equality {
public void main(String[] args) {
System.out.println(new StringBuilder("zelda")
== new StringBuilder("zelda"));
System.out.println(3 == 3);
System.out.println("bart" == "bart");
System.out.println(new int[0] == new int[0]);
System.out.println(LocalTime.now() == LocalTime.now());
}
}
What is the output of the following application?
package ballroom;
public class Dance {
public static void swing(int... beats) throws ClassCastException {
try {
System.out.print("1"+beats[2]); // p1
} catch (RuntimeException e) {
System.out.print("2");
} catch (Exception e) {
System.out.print("3");
} finally {
System.out.print("4");
}
}
public static void main(String... music) {
new Dance().swing(0,0); // p2
System.out.print("5");
}
}
What is the output of the following?
List<String> drinks = Arrays.asList("can", "cup");
for (int container = drinks.size(); container > 0; container++) {
System.out.print(drinks.get(container-1) + ",");
}
Which of the following method signatures are valid declarations of an entry point in a Java application? (Choose three.)
Given the application below and the choices available, which lines must all be removed to allow the code to compile? (Choose three.)
1: package year;
2: public class Seasons {
3: public static void main(String[] time) {
4: final long winter = 10;
5: final byte season = 2;
6: int fall = 4;
7: final short summer = 3;
8: switch(season) {
9: case 1:
10: case winter: System.out.print("winter");
11: default:
12: case fall: System.out.print("fall");
13: case summer: System.out.print("summer");
14: default:
15: }
16: }
17: }
Given the application below, which lines do not compile? (Choose three.)
package furryfriends;
interface Friend {
protected String getName(); // h1
}
class Cat implements Friend {
String getName() { // h2
return "Kitty";
}
}
public class Dog implements Friend {
String getName() throws RuntimeException { // h3
return "Doggy";
}
public static void main(String[] adoption) {
Friend friend = new Dog(); // h4
System.out.print(((Cat)friend).getName()); // h5
System.out.print(((Dog)null).getName()); // h6
}
}
Which of the following are unchecked exceptions? (Choose three.)
What is the result of compiling and executing the following application?
package ranch;
public class Cowboy {
private int space = 5;
private double ship = space < 2 ? 1 : 10; // g1
public void printMessage() {
if(ship>1) {
System.out.println("Goodbye");
} if(ship<10 && space>=2) System.out.println("Hello"); // g2
else System.out.println("See you again");
}
public static final void main(String... stars) {
new Cowboy().printMessage();
}
}
Given the following three class declarations, which sets of access modifiers can be inserted, in order, into the blank lines below that would allow all of the classes to compile? (Choose three.)
package wake;
public class Alarm {
____________static int clock;
____________long getTime() {return clock;}
}
package wake;
public class Coffee {
private boolean bringCoffee() { return new Alarm().clock<10;}
}
package sleep;
public class Snooze extends wake.Alarm {
private boolean checkTime() { return getTime()>10;}
}
Given that FileNotFoundException is a subclass of IOException and Long is a subclass of Number, what is the output of the following application?
package materials;
import java.io.*;
class CarbonStructure {
protected long count;
public abstract Number getCount() throws IOException; // q1
public CarbonStructure(int count) { this.count = count; }
}
public class Diamond extends CarbonStructure {
public Diamond() { super(15); }
public Long getCount() throws FileNotFoundException { // q2
return count;
}
public static void main(String[] cost) {
try {
final CarbonStructure ring = new Diamond(); // q3
System.out.print(ring.getCount()); // q4
} catch (IOException e) {
e.printStackTrace();
}
}
}
How many lines contain a compile error?
1: import java.time.*;
2: import java.time.format.*;
3:
4: public class HowLong {
5: public void main(String h) {
6: LocalDate newYears = new LocalDate(2017, 1, 1);
7: Period period = Period.ofYears(1).ofDays(1);
8: DateTimeFormat format = DateTimeFormat.ofPattern("MM-dd-yyyy");
9: System.out.print(format.format(newYears.minus(period)));
10: }
11: }
Which of the following statements about try-catch blocks are correct? (Choose two.)
What is printed by the following code snippet?
int fish = 1 + 2 * 5>=2 ? 4 : 2;
int mammals = 3 < 3 ? 1 : 5>=5 ? 9 : 7;
System.out.print(fish+mammals+"");
Which of the following statements about objects, reference types, and casting are correct? (Choose three.)
What is the output of the following when run as java EchoFirst seed flower plant?
package unix;
import java.util.*;
public class EchoFirst {
public static void main(String[] args) {
int result = Arrays.binarySearch(args, args[0]);
System.out.println(result);
}
}
How many objects are eligible for garbage collection at the end of the main() method?
package store;
public class Shoes {
static String shoe1 = new String("sandal");
static String shoe2 = new String("flip flop");
public static void shopping() {
String shoe3 = new String("croc");
shoe2 = shoe1;
shoe1 = shoe3;
}
public static void main(String... args) {
shopping();
}
}
Fill in the blanks: The ____________keyword is used in method declarations, the ____________keyword is used to guarantee a statement will execute even if an exception is thrown, and the ____________keyword is used to throw an exception to the surrounding process.
Which statements best describe the result of this code? (Choose two.)
package nyc;
public class TouristBus {
public static void main(String... args) {
String[] nycTourLoops = new String[] { "Downtown", "Uptown", "Brooklyn" };
String[] times = new String[] { "Day", "Night" };
for (int i = 0, j = 0; i < nycTourLoops.length; i++, j++)
System.out.println(nycTourLoops[i] + " " + times[j]);
}
}
Fill in the blanks: Because of____________ , it is possible to ____________a method, which allows Java to support____________ .
What is the result of the following?
package calendar;
public class Seasons {
public static void seasons(String... names) {
int l = names[1].length(); // s1
System.out.println(names[l]); // s2
}
public static void main(String[] args) {
seasons("Summer", "Fall", "Winter", "Spring");
}
}
How many lines of the following application contain compilation errors?
1: package percussion;
2:
3: interface MakesNoise {}
4: abstract class Instrument implements MakesNoise {
5: public Instrument(int beats) {}
6: public void play() {}
7: }
8: public class Drum extends Instrument {
9: public void play(int count) {}
10: public void concert() {
11: super.play(5);
12: }
13: public static void main(String[] beats) {
14: MakesNoise mn = new Drum();
15: mn.concert();
16: }
17: }
What is the output of the following application?
package fly;
public class Helicopter {
public int adjustPropellers(int length, String[] type) {
length++;
type[0] = "LONG";
return length;
}
public static void main(String[] climb) {
final Helicopter h = new Helicopter();
int length = 5;
String[] type = new String[1];
length = h.adjustPropellers(length, type);
System.out.print(length+","+type[0]);
}
}
How many lines of the following application do not compile?
package castles;
class OpenDoorException extends Exception {}
class CableSnapException extends OpenDoorException {}
public class Palace {
public void openDrawbridge() throws Exception {
try {
throw new Exception("Problem");
} catch (OpenDoorException e) {
throw new OpenDoorException();
} catch (CableSnapException ex) {
try {
throw new OpenDoorException();
} catch (Exception ex) {
} finally {
System.out.println("Almost done");
}
} finally {
throw new RuntimeException("Unending problem");
}
}
public static void main(String[] moat) throws IllegalArgumentException {
new Palace().openDrawbridge();
}
}
Choose the best answer: ____________and ____________are two properties that go hand in hand to improve class design by structuring a class with related attributes and actions while protecting the underlying data from access by other classes.
What is the output of the following?
string bike1 = "speedy";
string bike2 = new String("speedy");
boolean test1 = bike1 == bike2;
boolean test2 = bike1.equals(bike2);
System.out.println(test1 + " " + test2);
What is the output of the following when run as java EchoFirst seed flower plant?
package unix;
import java.util.*;
public class EchoFirst {
public static void main(String[] args) {
Arrays.sort(args);
int result = Arrays.binarySearch(args, args[0]);
System.out.println(result);
}
}
Which are true statements? (Choose three.)
How many lines does this program print?
import java.time.*;
public class OnePlusOne {
public static void main(String... nums) {
LocalDate time = LocalDate.of(1, 11);
while (time.getHour() < 1) {
time.plusHours(1);
System.out.println("in loop");
}
}
}
How many objects are eligible for garbage collection immediately before the end of the main() method?
public class Tennis {
public static void main(String[] game) {
String[] balls = new String[1];
int[] scores = new int[1];
balls = null;
scores = null;
}
}
What is the output of the following?
14: int count = 0;
15: LocalDate date = LocalDate.of(2017, Month.JANUARY, 1);
16: while (date.getMonth() != Month.APRIL)
17: date = date.minusMonths(1);
18: count++;
19: System.out.println(count);
How many lines of the following class do not compile?
1: package arctic;
2: abstract class Bear {
3: protected int sing;
4: protected abstract int grunt();
5: int sing() {
6: return sing;
7: }
8: }
9: public class PolarBear extends Bear {
10: int grunt() {
11: sing() += 10;
12: return super.grunt()+1;
13: return 10;
14: }
15: }
In which places is the default keyword permitted to be used? (Choose two.)