THE OCA EXAM TOPICS COVERED IN THIS PRACTICE TEST INCLUDE THE FOLLOWING:
Fill in the blanks: The____________ access modifier allows access to everything the ____________access modifier does and more.
What is the command to call one constructor from another constructor in the same class?
What is the output of the following application?
package stocks;
public class Bond {
private static int price = 5;
public boolean sell() {
if(price<10) {
price++;
return true;
} else if(price>=10) {
return false;
}
}
public static void main(String[] cash) {
new Bond().sell();
new Bond().sell();
new Bond().sell();
System.out.print(price);
}
}
What is true about the following program?
package figures;
public class Dolls {
public void nested() { nested(2,true); } // g1
public int nested(int level, boolean height) { return nested(level); }
public int nested(int level) { return level+1; }; // g2
public static void main(String[] outOfTheBox) {
System.out.print(new Dolls().nested());
}
}
Fill in the blank: Java uses ___________ to send data into a method.
Which of the following is a valid JavaBean method signature?
Which of the following statements about calling this() in a constructor is not true?
Which of the following can fill in the blank to make the class compile?
package ai;
public class Robot {
____________ compute() { return 10; }
}
Fill in the blank: A ____________variable is always available to all instances of the class.
Which line of code, inserted at line p1, causes the application to print 5?
package games;
public class Jump {
private int rope = 1;
protected boolean outside;
public Jump() {
// p1
outside = true;
}
public Jump(int rope) {
this.rope = outside ? rope : rope+1;
}
public static void main(String[] bounce) {
System.out.print(new Jump().rope);
}
}
Which of the following statements is not true?
Given the following class, what should be inserted into the two blanks to ensure the class data is properly encapsulated?
package storage;
public class Box {
public String stuff;
____________String____________ () {
return stuff;
}
public void setStuff(String stuff) {
this.stuff = stuff;
}
}
Which statement about a no-argument constructor is true?
Which of the following method signatures does not contain a compiler error?
Given the following application, which diagram best represents the state of the mySkier, mySpeed, and myName variables in the main() method after the call to the slalom() method?
package slopes;
public class Ski {
private int age = 18;
private static void slalom(Ski racer, int[] speed, String name) {
racer.age = 18;
name = "Wendy";
speed = new int[1];
speed[0] = 11;
racer = null;
}
public static void main(String... mountain) {
final Ski mySkier = new Ski();
mySkier.age = 16;
final int[] mySpeed = new int[1];
final String myName = "Rosie";
slalom(mySkier,mySpeed,myName);
}
}
Given the class below, which method signature could be successfully added to the class as an overloaded version of the findAverage() method?
public class Calculations {
public Integer findAverage(int sum) { return sum; }
}
Which of the following is not a reason to use encapsulation when designing a class?
Which of the following data types can be modified after they are passed to a method as an argument?
What is the best way to call the following method from another class in the same package, assuming the class using the method does not have any static imports?
package useful;
public class MathHelper {
public static int roundValue(double d) {
// Implementation omitted
}
}
Given a method with one of the following return types, which data type prevents the return statement from being used within the method?
How many final modifiers would need to be removed for this application to compile?
package end;
public final class Games {
public final static int finish(final int score) {
final int win = 3;
final int result = score++ < 5 ? 2 : win;
return result+=win;
}
public static void main(final String[] v) {
System.out.print(finish(Integer.parseInt(v[0])));
}
}
Fill in the blanks: ____________is used to call a constructor in the parent class, while ____________is used to reference a member of the parent class.
Given the following method signature, which classes can call it?
void run(String government)
Which statement(s) about the following class would help to properly encapsulate the data in the class?
package shield;
public class Protect {
private String material;
protected int strength;
public int getStrength() {
return strength;
}
public void setStrength(int strength) {
this.strength = strength;
}
}
Which of the following is a valid method name in Java?
Which of the following lines of code can be inserted in the line below that would allow the class to compile?
package farm;
public class Coop {
public final int static getNumberOfChickens() {
// INSERT CODE HERE
}
}
Which of the following is a true statement about passing data to a method?
What is a possible output of the following application?
package wrap;
public class Gift {
private final Object contents;
protected Object getContents() {
return contents;
}
protected void setContents(Object contents) {
this.contents = contents;
}
public void showPresent() {
System.out.print("Your gift: "+contents);
}
public static void main(String[] treats) {
Gift gift = new Gift();
gift.setContents(gift);
gift.showPresent();
}
}
Which of the following is a valid JavaBean method prefix?
Given the following two classes, each in a different package, which line inserted below allows the second class to compile?
package clothes;
public class Store {
public static String getClothes() { return "dress"; }
}
package wardrobe;
// INSERT CODE HERE
public class Closet {
public void borrow() {
System.out.print("Borrowing clothes: "+getClothes());
}
}
What access modifier is used to mark class members package-private?
How many lines of the following program contain compilation errors?
package sky;
public class Stars {
private int inThe = 4;
public void Stars() {
super();
}
public Stars(int inThe) {
this.inThe = this.inThe;
}
public static void main(String[] endless) {
System.out.print(new sky.Stars(2).inThe);
}
}
Which of the following statements is true?
Given the following method declaration, which line can be inserted to make the code compile?
public short calculateDistance(double lat1, double lon1,
double lat2, double lon2) {
// INSERT CODE HERE
}
Which of the following statements about overloaded methods are true?
How many lines of code would need to be removed for the following class to compile?
package work;
public class Week {
private static final String monday;
String tuesday;
final static wednesday = 3;
final protected int thursday = 4;
}
What is the output of the following application?
package pet;
public class Puppy {
public static int wag = 5; // q1
public void Puppy(int wag) { // q2
this.wag = wag;
}
public static void main(String[] tail) {
System.out.print(new Puppy(2).wag); // q3
}
}
Fill in the blanks: The ____________access modifier allows access to everything the ____________access modifier does and more.
What is the output of the following application?
package ship;
public class Phone {
private int size;
public Phone(int size) {this.size=size;}
public static void sendHome(Phone p, int newSize) {
p = new Phone(newSize);
p.size = 4;
}
public static final void main(String... params) {
final Phone phone = new Phone(3);
sendHome(phone,7);
System.out.print(phone.size);
}
}
Given the following class, which line of code when inserted below would prevent the class from compiling?
public class Drink {
public static void water() {}
public void get() {
// INSERT CODE HERE
}
}
Given the following method declaration signature, which of the following is a valid call of this method?
public void call(int count, String me, String... data)
Which statement about a static variable is true?
Which of the following is not a true statement?
How many final modifiers would need to be removed for this application to compile?
package park;
public class Tree {
public final static long numberOfTrees;
public final double height;
static {}
{ final int initHeight = 2;
height = initHeight;
}
static {
numberOfTrees = 100;
height = 4;
}
}
What is the output of the following application?
package jungle;
public class RainForest extends Forest {
public RainForest(long treeCount) {
this.treeCount = treeCount+1;
}
public static void main(String[] birds) {
System.out.print(new RainForest(5).treeCount);
}
}
class Forest {
public long treeCount;
public Forest(long treeCount) {
this.treeCount = treeCount+2;
}
}
What is the output of the following application?
public class ChooseWisely {
public ChooseWisely() { super(); }
public int choose(int choice) { return 5; }
public int choose(short choice) { return 2; }
public int choose(long choice) { return 11; }
public static void main(String[] path) {
System.out.print(new ChooseWisely().choose((byte)2+1));
}
}
What is the output of the following application?
package sports;
public class Football {
public static Long getScore(Long timeRemaining) {
return 2*timeRemaining; // m1
}
public static void main(String[] refs) {
final int startTime = 4;
System.out.print(getScore(startTime)); // m2
}
}
Which of the following is a valid method name in Java?
Assume there is a class Bouncer with a protected variable. Methods in which class can access this variable?
Given the following two classes, each in a different package, which line inserted below allows the second class to compile?
package commerce;
public class Bank {
public void withdrawal(int amountInCents) {}
public void deposit(int amountInCents) {}
}
package employee;
// INSERT CODE HERE
public class Teller {
public void processAccount(int depositSlip, int withdrawalSlip) {
withdrawal(withdrawalSlip);
deposit(depositSlip);
}
}