THE OCP EXAM TOPICS COVERED IN THIS PRACTICE TEST INCLUDE THE FOLLOWING:
Which answer choice can replace line 6 so the code continues to produce the same output?
3: List<String> rug = new ArrayList<>();
4: rug.add("circle");
5: rug.add("square");
6: System.out.println(rug);
Which best describes this code?
class Stats {
private int data;
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
}
What design pattern or principle ensures that there will be no more than one instance of a class?
What is the output of this code?
class Laptop extends Computer {
public void startup() {
System.out.print("laptop-");
}
}
public class Computer {
public void startup() {
System.out.print("computer-");
}
public static void main(String[] args) {
Computer computer = new Laptop();
Laptop laptop = new Laptop();
computer.startup();
laptop.startup();
}
}
Which method can be correctly inserted into this class to meet the contract of the equals() method? You may assume that text is not null.
class Button {
private String text;
public int hashCode() {
return text.hashCode();
}
}
public boolean equals(Object o) {
if ( o == null ) return true;
if (! (o instanceof Button)) return false;
return text.equals(o.text);
}
public boolean equals(Object o) {
if ( o == null ) return true;
Button b = (Button) o;
return text.equals(b.text);
}
public boolean equals(Object o) {
if (! (o instanceof Button)) return false;
return text.equals(o.text);
}
public boolean equals(Object o) {
if (! (o instanceof Button)) return false;
Button b = (Button) o;
return text.equals(b.text);
}
Fill in the blanks: ____________means the state of an object cannot be changed while ____________means that it can.
Which is the first line to fail to compile?
class Tool {
void use() { } // r1
}
class Hammer extends Tool {
private void use() { } // r2
public void bang() { } // r3
}
Which of these classes properly implement(s) the singleton pattern?
class ExamAnswers {
private static ExamAnswers instance = new ExamAnswers();
private List<String> answers = new ArrayList<>();
public static List<String> getAnswers() {
return instance.answers;
}
}
class TestAnswers {
private static TestAnswers instance = new TestAnswers();
private List<String> answers = new ArrayList<>();
public static TestAnswers getTestAnswers() {
return instance;
}
public List<String> getAnswers() {
return answers;
}
}
What does the following print?
public class Transport {
static interface Vehicle {}
static class Bus implements Vehicle {}
public static void main(String[] args) {
Bus bus = new Bus();
boolean n = null instanceof Bus;
boolean v = bus instanceof Vehicle;
boolean b = bus instanceof Bus;
System.out.println(n + " " + v + " " + b);
}
}
What technique allows multiple variables from the same class to be shared across all instances of a class?
Which is not a requirement for a class to be immutable?
Which statement is true about encapsulation while providing the broadest access allowed?
What does the following print?
class Laptop extends Computer {
String type = "laptop";
}
public class Computer {
String type = "computer";
public static void main(String[] args) {
Computer computer = new Laptop();
Laptop laptop = new Laptop();
System.out.print(computer.type + "," + laptop.type);
}
}
Which of these classes is/are immutable?
public final class Flower {
private final String name;
private final List<Integer> counts;
public Flower(String name, List<Integer> counts) {
this.name = name;
this.counts = counts;
}
public String getName() {
return name;
}
public List<Integer> getCounts() {
return counts;
}
}
public final class Plant {
private final String name;
private final List<Integer> counts;
public Plant(String name, List<Integer> counts) {
this.name = name;
this.counts = new ArrayList<>(counts);
}
public String getName() {
return name;
}
public List<Integer> getCounts() {
return new ArrayList<>(counts);
}
}
Which methods compile?
private static int numShovels;
private int numRakes;
public int getNumShovels() {
return numShovels;
}
public int getNumRakes() {
return numRakes;
}
Which methods compile?
private static int numShovels;
private int numRakes;
public static int getNumShovels() {
return numShovels;
}
public static int getNumRakes() {
return numRakes;
}
How many lines of the main method fail to compile?
11: static interface Vehicle {}
12: static class Bus implements Vehicle {}
13:
14: public static void main(String[] args) {
15: Bus bus = new Bus();
16:
17: System.out.println(null instanceof Bus);
18: System.out.println(bus instanceof Vehicle);
19: System.out.println(bus instanceof Bus);
20: System.out.println(bus instanceof ArrayList);
21: System.out.println(bus instanceof Collection);
22: }
Which variable declaration is the first line not to compile?
class Building {}
class House extends Building{}
public void convert() {
Building b = new Building();
House h = new House();
Building bh = new House();
Building p = (House) b;
House q = (Building) h;
Building r = (Building) bh;
House s = (House) bh;
}
Which statement is true about the code that can fill in the blank?
class Sticker {
public int hashCode() {
return 1;
}
public boolean equals(Object o) {
return____________ ;
}
}
What change is needed to make Secret well encapsulated?
import java.util.*;
public class Secret {
private int number = new Random().nextInt(10);
public boolean guess(int candidate) {
return number == candidate;
}
}
Which of these classes best implement(s) the singleton pattern?
class ExamAnswers {
private static ExamAnswers instance = new ExamAnswers();
private List<String> answers = new ArrayList<>();
private ExamAnswers() {}
public ExamAnswers getExamAnswers() {
return instance;
}
public List<String> getAnswers() {
return answers;
}
}
class TestAnswers {
private static TestAnswers instance = new TestAnswers();
private List<String> answers = new ArrayList<>();
private TestAnswers() {}
public static TestAnswers getTestAnswers() {
return instance;
}
public List<String> getAnswers() {
return answers;
}
}
How many lines does the following code output?
public class Cars {
static {
System.out.println("static");
}
private static void drive() {
System.out.println("fast");
}
public static void main(String[] args) {
drive();
drive();
}
}
Which is not a true statement given this diagram?
Given the diagram in the previous question, how many of the classes can call the display() method?
What does the following print?
1: class SmartWatch extends Watch {
2: private String getType() { return "smart watch"; }
3: public String getName(String suffix) {
4: return getType() + suffix;
5: }
6: }
7: public class Watch {
8: private String getType() { return "watch"; }
9: public String getName(String suffix) {
10: return getType() + suffix;
11: }
12: public static void main(String[] args) {
13: Watch watch = new Watch();
14: SmartWatch smartWatch = new SmartWatch();
15: System.out.print(watch.getName(","));
16: System.out.print(smartWatch.getName(""));
17: }
18: }
What does the following print?
public class Transport {
static interface Vehicle {}
static class Bus implements Vehicle {}
static class Van extends Bus {}
public static void main(String[] args) {
Bus bus = new Van();
Van van = new Van();
Van[] vans = new Van[0];
boolean b = bus instanceof Vehicle;
boolean v = van instanceof Vehicle;
boolean a = vans instanceof Vehicle[];
System.out.println(b + " " + v + " " + a);
}
}
Which of the following correctly fills in the blank so this code compiles and prints true?
public class Button {
private String text;
public int hashCode() {
return text.hashCode();
}
public boolean equals(Object o) {
if (____________) return false;
Button b = (Button) o;
return text.equals(b.text);
}
public static void main(String[] args) {
Button b1 = new Button();
Button b2 = new Button();
b1.text = "mickey";
b2.text = "mickey";
System.out.println(b1.equals(b2));
}
}
Which is the first line to fail to compile?
class Tool {
void use() { } // r1
}
class Hammer extends Tool {
private void use(String s) { } // r2
public void bang() { } // r3
}
What is lazy instantiation?
Which variable declaration is the first line not to compile?
30: class Building {}
31: class House extends Building{}
32:
33: public void convert() {
34: Building b = new Building();
35: House h = new House();
36: Building bh = new House();
37: House p = (House) b;
38: House q = (House) h;
39: House r = (House) bh;
40: }
Which statement about encapsulation is not true?
Which of these classes is/are immutable?
public class Flower {
private final String name;
private final List<Integer> counts;
public Flower(String name, List<Integer> counts) {
this.name = name;
this.counts = new ArrayList<>(counts);
}
public final String getName() {
return name;
}
public final List<Integer> getCounts() {
return new ArrayList<>(counts);
}
}
public class Plant {
private final String name;
private final List<Integer> counts;
public Plant(String name, List<Integer> counts) {
this.name = name;
this.counts = new ArrayList<>(counts);
}
public String getName() {
return name;
}
public List<Integer> getCounts() {
return new ArrayList<>(counts);
}
}
How many lines does the following code output?
public class Cars {
private static void drive() {
static {
System.out.println("static");
}
System.out.println("fast");
}
public static void main(String[] args) {
drive();
drive();
}
}
How many of the following pairs of values can fill in the blanks to comply with the contract of the hashCode() and equals() methods?
class Sticker {
public int hashCode() {
return _____________;
}
public boolean equals(Object o) {
return _____________;
}
}
How do you change the value of an instance variable in an immutable class?
Which technique or pattern requires instance variables to implement?
How many lines of output does the following generate?
public class HowMany {
static {
System.out.println("any");
}
{
System.out.println("more");
}
public static void main(String[] args) {
new HowMany();
new HowMany();
}
}
Which is the first line to fail to compile?
class Tool {
default void use() { } // r1
}
class Hammer extends Tool {
public void use() { } // r2
public void bang() { } // r3
}
Which variable declaration is the first line to throw a ClassCastException at runtime?
class Building {}
class House extends Building{}
public void convert() {
Building b = new Building();
House h = new House();
Building bh = new House();
House p = (House) b;
House q = (House) h;
House r = (House) bh;
}
Which of the following values can fill in the blank for the class to be correctly implemented?
class Sticker {
public int hashCode(Object o) {
return_____________ ;
}
public boolean equals(Object o) {
return true;
}
}