NEW
Font size
WorksheetsAP Computer Science Unit 2
Total questions: 26
Worksheet time: 20mins
A student has created a Dog class. The class contains variables to represent the following.
A String variable called breed to represent the breed of the dog
An int variable called age to represent the age of the dog
A String variable called name to represent the name of the dog
The object pet is declared as type Dog. Which of the following descriptions is accurate?
An attribute of the name object is String.
An attribute of the pet object is name.
An instance of the pet class is Dog.
An attribute of the Dog instance is pet.
An instance of the Dog object is pet.
A student has created a Party class. The class contains variables to represent the following.
An int variable called numOfPeople to represent the number of people at the party.
A boolean variable called discoLightsOn to represent whether the disco ball is on.
A boolean variable called partyStarted to represent whether the party has started.
The object myParty is declared as type Party. Which of the following descriptions is accurate?
boolean is an attribute of the myParty object.
myParty is an attribute of the Party class.
myParty is an instance of the Party class.
myParty is an attribute of the Party instance.
numOfPeople is an instance of the Party object.
Consider the following class. Which of the following successfully creates a new Cat object?
public class Cat{
private String color;
private String breed;
private boolean isHungry;
public Cat(){
color = "unknown";
breed = "unknown";
isHungry = false;
}
public Cat(String c, String b, boolean h){
color = c;
breed = b;
isHungry = h;
}
}
I. Cat a = new Cat();
II. Cat b = new Cat("Shorthair", true);
III. String color = "orange";
boolean hungry = false;
Cat c = new Cat(color, "Tabby", hungry);
I only
I and II
I and III
I, II, and III
II and III
Consider the following class. Which of the following code segments will construct a Movie object m with a title of “Lion King” and rating of 8.0?
public class Movie{
private String title;
private String director;
private double rating;
private boolean inTheaters;
public Movie(String t, String d, double r){
title = t;
director = d;
rating = r;
inTheaters = false;
}
public Movie(String t){
title = t;
director = "unknown";
rating = 0.0;
inTheaters = false;
}
}
Movie m = new Movie(8.0, "Lion King");
Movie m = Movie("Lion King", 8.0);
Movie m = new Movie();
Movie m = new Movie("Lion King", "Disney", 8.0);
Movie m = new Movie("Lion King");
Consider the following class definition.
public class Party{
private int numInvited;
private boolean partyCancelled;
public Party(){
numInvited = 1;
partyCancelled = false;
}
public void inviteFriend(){
numInvited++;
}
public void cancelParty(){
partyCancelled = true;
}
}
Assume that a Party object called myParty has been properly declared and initialized in a class other than Party. Which of the following statements are valid?
myParty.cancelParty();
myParty.inviteFriend(2);
myParty.endParty();
myParty.numInvited();
System.out.println( myParty.cancelParty() );
Consider the following class definition.
public class Cat{
public void meow(){
System.out.print("Meow ");
}
public void purr(){
System.out.print("purr");
}
public void welcomeHome(){
purr();
meow();
}
/* Constructors not shown */
}
Which of the following code segments, if located in a method in a class other than Cat, will cause the message “Meow purr” to be printed?
Cat a = new Cat();
Cat.meow();
Cat.purr();
Cat a = new Cat();
a.welcomeHome();
Cat a = new Cat();
a.meow();
a.purr();
Cat a = new Cat().welcomeHome();
Cat a = new Cat();
a.meow();
Consider the following methods:
public void inchesToCentimeters(double I){
double c = i * 2.54;
printInCentimeters(i, c);
}
public void printInCentimeters(double inches, double centimeters){
System.out.print(inches + "-->" + centimeters);
}
Assume that the method call inchesToCentimeters(10) appears in a method in the same class. What is printed as a result of the method call?
inches –> centimeters
10 –> 25
25.4 –> 10
10 –> 12.54
10.0 –> 25.4
Consider the following methods, which appear in the same class.
public void splitPizza(int numOfPeople){
int slicesPerPerson = 8/numOfPeople;
/* INSERT CODE HERE */
}
public void printSlices(int slices){
System.out.println("Each person gets " + slices + " slices each");
}
Which of the following lines would go into /* INSERT CODE HERE */ in the method splitPizza in order to call the printSlices method to print the number of slices per person correctly?
printSlices(slicesPerPerson);
printSlices(numOfPeople);
printSlices(8);
splitPizza(8);
splitPizza(slicesPerPerson);
What does the following code print out?
public class MethodTrace{
public void square(int x){
System.out.print(x*x);
}
public void divide(int x, int y){
System.out.println(x/y);
}
public static void main(String[] args) {
MethodTrace traceObj = new MethodTrace();
traceObj.square(5);
System.out.print(" and ");
traceObj.divide(4,2);
}
}
25 and 2
25 and .5
2 25
25 2
Nothing, it does not compile.
Consider the following method.:
public double calculatePizzaBoxes(int numOfPeople, double slicesPerBox) { /*implementation not shown */}
Which of the following lines of code, if located in a method in the same class as calculatePizzaBoxes, will compile without an error?
int result = calculatePizzaBoxes(45, 9.0);
double result = calculatePizzaBoxes(45.0, 9.0);
int result = calculatePizzaBoxes(45.0, 9);
double result = calculatePizzaBoxes(45, 9.0);
result = calculatePizzaBoxes(45, 9);
Consider the following class definition.
public class Liquid{
private double boilingPoint;
private double freezingPoint;
private double currentTemp;
public Liquid(){
currentTemp = 50;
}
public void lowerTemp(){
currentTemp -= 10;
}
public double getTemp(){
return currentTemp;
}
}
Assume that the following code segment appears in a class other than Liquid.
Liquid water = new Liquid();
water.lowerTemp();
System.out.println(water.getTemp());
What is printed as a result of executing the code segment?
-10
50
water.getTemp()
The code will not compile.
40.0
What does the following code print out?
public class MethodTrace{
public int square(int x){
return x*x;
}
public int divide(int x, int y){
return x/y;
}
public static void main(String[] args) {
MethodTrace traceObj = new MethodTrace();
System.out.println( traceObj.square(2) + traceObj.divide(6,2) );
}
}
5
7
4 3
2 3
Does not compile.
What is the value of pos after the following code executes?
String s1 = "abccba";
int pos = s1.indexOf("b");
2
1
4
-1
What is the value of len after the following code executes?
String s1 = "baby";
int len = s1.length();
2
3
4
-1
What is the value of s2 after the following code executes?
String s1 = "baby";
String s2 = s1.substring(0,3);
baby
b
ba
bab
What is the value of s2 after the following code executes?
String s1 = "baby";
String s2 = s1.substring(2);
by
aby
a
b
ba
What is the value of s2 after the following code executes?
String s1 = new String("hi there");
int pos = s1.indexOf("e");
String s2 = s1.substring(0,pos);
hi th
hi the
hi ther
hi there
What is the value of s1 after the following code executes?
String s1 = "Hi";
String s2 = s1.substring(0,1);
String s3 = s2.toLowerCase();
Hi
hi
h
H
What is the value of s3 after the following code executes?
String s1 = "Hi";
String s2 = s1.substring(0,1);
String s3 = s2.toLowerCase();
Hi
hi
H
h
What is the value of answer after the following code executes?
String s1 = "Hi";
String s2 = "Bye";
int answer = s1.compareTo(s2);
positive (>0)
0
negative (<0)
Which of the following would be true about 40% of the time?
Math.random() < 0.4
Math.random() > 0.4
Math.random() == 0.4
Which of the following would return a random number from 1 to 5 inclusive?
((int) (Math.random() * 5))
((int) (Math.random() * 6))
((int) (Math.random() * 5) + 1)
Which of the following would return a random number from 0 to 10 inclusive?
((int) (Math.random() * 10))
((int) (Math.random() * 11))
((int) (Math.random() * 10) + 1)
Which of the following would be true about 75% of the time?
Math.random() < 0.25
Math.random() > 0.25
Math.random() == 0.25
Which of the following statements assigns a random integer between 25 and 60, inclusive, to rn?
int rn = (int) (Math.random() * 25) + 36;
int rn = (int) (Math.random() * 25) + 60;
int rn = (int) (Math.random() * 26) + 60;
int rn = (int) (Math.random() * 36) + 25;
int rn = (int) (Math.random() * 60) + 25;
Given the following code segment, what is in the string referenced by s1?
String s1 = "xy";
String s2 = s1;
s1 = s1 + s2 + "z";
xyz
xyxyz
xy xy z
xy z
z
