0% found this document useful (0 votes)
2 views

Unit 2 Topic Questions - Using objects

The document contains a series of multiple-choice questions related to AP Computer Science A, covering topics such as string manipulation, method calls, class definitions, and basic programming concepts. Each question presents a code segment or scenario and asks for the expected output or correct implementation. The questions are designed to test knowledge of Java programming and object-oriented principles.

Uploaded by

Tanisha Bentick
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Unit 2 Topic Questions - Using objects

The document contains a series of multiple-choice questions related to AP Computer Science A, covering topics such as string manipulation, method calls, class definitions, and basic programming concepts. Each question presents a code segment or scenario and asks for the expected output or correct implementation. The questions are designed to test knowledge of Java programming and object-oriented principles.

Uploaded by

Tanisha Bentick
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

AP COMPUTER SCIENCE A Test Booklet

Unit 2 Topic Questions

1. Consider the following code segment.

String oldStr = "ABCDEF";


String newStr = oldStr.substring(1, 3) + oldStr.substring(4);
System.out.println(newStr);

What is printed as a result of executing the code segment?


(A) ABCD
(B) BCDE
(C) BCEF
(D) BCDEF
(E) ABCDEF

2. Consider the following methods, which appear in the same class.

public void slope(int x1, int y1, int x2, int y2)
{
int xChange = x2 - x1;
int yChange = y2 - y1;
printFraction(yChange, xChange);
}

public void printFraction(int numerator, int denominator)


{
System.out.print(numerator + "/" + denominator);
}

Assume that the method call slope(1, 2, 5, 10) appears in a method in the same class. What is printed as
a result of the method call?
(A) 8/4
(B) 5/1
(C) 4/8
(D) 2/1
(E) 1/5

3.
Consider the following method, which is intended to calculate and return the expression .

public double calculate(double x, double y, double a, double b)


{
return /* missing code */;
}

Which of the following can replace /* missing code */ so that the method works as intended?

AP Computer Science A Page 1 of 16


Test Booklet

Unit 2 Topic Questions

(A) Math.sqrt(x ^ 2, y ^ 2, a - b)
(B) Math.sqrt((x + y) ^ 2) / Math.abs(a, b)
(C) Math.sqrt((x + y) ^ 2 / Math.abs(a - b))
(D) Math.sqrt(Math.pow(x + y, 2) / Math.abs(a, b))
(E) Math.sqrt(Math.pow(x + y, 2) / Math.abs(a - b))

4. Consider the following method.

public double myMethod(int a, boolean b)


{ /* implementation not shown */ }

Which of the following lines of code, if located in a method in the same class as myMethod, will compile without
error?
(A) int result = myMethod(2, false);
(B) int result = myMethod(2.5, true);
(C) double result = myMethod(0, false);
(D) double result = myMethod(true, 10);
(E) double result = myMethod(2.5, true);

5. Consider the following code segment.

String temp = "comp";


System.out.print(temp.substring(0) + " " +
temp.substring(1) + " " +
temp.substring(2) + " " +
temp.substring(3));

What is printed when the code segment is executed?


(A) comp
(B) c o m p
(C) comp com co c
(D) comp omp mp p
(E) comp comp comp comp

6. Consider the following code segment.

String str = "CompSci";


System.out.println(str.substring(0, 3));
int num = str.length();

What is the value of num when the code segment is executed?

Page 2 of 16 AP Computer Science A


Test Booklet

Unit 2 Topic Questions

(A) 3
(B) 4
(C) 5
(D) 6
(E) 7

7. Consider the following code segment.

String str = "0";


str += str + 0 + 8;
System.out.println(str);

What is printed as a result of executing the code segment?


(A) 8
(B) 08
(C) 008
(D) 0008
(E) Nothing is printed, because numerical values cannot be added to a String object.

8. Consider the following code segment.

int one = 1;
int two = 2;
String zee = "Z";
System.out.println(one + two + zee);

What is printed as a result of executing the code segment?


(A) 12Z
(B) 3Z
(C) 12zee
(D) 3zee
(E) onetwozee

AP Computer Science A Page 3 of 16


Test Booklet

Unit 2 Topic Questions

9. The Student class has been defined to store and manipulate grades for an individual student. The following
methods have been defined for the class.

/* Returns the sum of all of the student’s grades */


public double sumOfGrades()
{ /* implementation not shown */ }
/* Returns the total number of grades the student has received */
public int numberOfGrades()
{ /* implementation not shown */ }
/* Returns the lowest grade the student has received */
public double lowestGrade()
{ /* implementation not shown */ }

Which of the following statements, if located in a method in the Student class, will determine the average of all
of the student’s grades except for the lowest grade and store the result in the double variable newAverage ?
(A) newAverage = sumOfGrades() / numberOfGrades() - 1;
(B) newAverage = sumOfGrades() / (numberOfGrades() - 1);
(C) newAverage = sumOfGrades() - lowestGrade() / (numberOfGrades() - 1);
(D) newAverage = (sumOfGrades() - lowestGrade()) / numberOfGrades() - 1;
(E) newAverage = (sumOfGrades() - lowestGrade()) / (numberOfGrades() - 1);

10. Consider the following method.

public void doSomething()


{
System.out.println("Something has been done");
}

Each of the following statements appears in a method in the same class as doSomething. Which of the
following statements are valid uses of the method doSomething ?

I. doSomething();
II. String output = doSomething();
III. System.out.println(doSomething());
(A) I only
(B) II only
(C) I and II only
(D) I and III only
(E) I, II, and III

Page 4 of 16 AP Computer Science A


Test Booklet

Unit 2 Topic Questions

11. Consider the following code segment.

double d1 = 10.0;
Double d2 = 20.0;
Double d3 = new Double(30.0);
double d4 = new Double(40.0);

System.out.println(d1 + d2 + d3.doubleValue() + d4);

What, if anything, is printed when the code segment is executed?


(A) 100.0
(B) 10.050.040.0
(C) 10.020.070.0
(D) 10.020.030.040.0
(E) There is no output due to a compilation error.

12. Consider the following class definition.

public class ExamScore


{
private String studentId;
private double score;
public ExamScore(String sid, double s)
{
studentId = sid;
score = s;
}
public double getScore()
{
return score;
}
public void bonus(int b)
{
score += score * b/100.0;
}
}

Assume that the following code segment appears in a class other than ExamScore.

ExamScore es = new ExamScore("12345", 80.0);


es.bonus(5);
System.out.println(es.getScore());

What is printed as a result of executing the code segment?

AP Computer Science A Page 5 of 16


Test Booklet

Unit 2 Topic Questions

(A) 4.0
(B) 5.0
(C) 80.0
(D) 84.0
(E) 85.0

13. Consider the following methods, which appear in the same class.

public int function1(int i, int j)


{
return i + j;
}

public int function2(int i, int j)


{
return j - i;
}

Which of the following statements, if located in a method in the same class, will initialize the variable x to 11?
(A) int x = function2(4, 5) + function1(1, 3);
(B) int x = function1(4, 5) + function2(1, 3);
(C) int x = function1(4, 5) + function2(3, 1);
(D) int x = function1(3, 1) + function2(4, 5);
(E) int x = function2(3, 1) + function1(4, 5);

Page 6 of 16 AP Computer Science A


Test Booklet

Unit 2 Topic Questions

14. Consider the following class declaration.

public class GameClass


{
private int numPlayers;
private boolean gameOver;

public Game()
{
numPlayers = 1;
gameOver = false;
}

public void addPlayer()


{
numPlayers++;
}

public void endGame()


{
gameOver = true;
}
}

Assume that the GameClass object game has been properly declared and initialized in a method in a class
other than GameClass. Which of the following statements are valid?

I. game.numPlayers++;
II. game.addPlayer();
III. game.gameOver();
IV. game.endGame();
(A) IV only
(B) I and III only
(C) I and IV only
(D) II and IV only
(E) II, III, and IV only

15. A student has created a Car class. The class contains variables to represent the following.

A String variable called color to represent the color of the car


An int variable called year to represent the year the car was made
A String variable called make to represent the manufacturer of the car
A String variable called model to represent the model of the car

The object vehicle will be declared as type Car.

Which of the following descriptions is accurate?

AP Computer Science A Page 7 of 16


Test Booklet

Unit 2 Topic Questions

(A) An instance of the vehicle class is Car.


(B) An instance of the Car object is vehicle.
(C) An attribute of the year object is int.
(D) An attribute of the vehicle object is color.
(E) An attribute of the Car instance is vehicle.

16. Consider the code segment below.

int a = 1988;
int b = 1990;

String claim = " that the world’s athletes " +


"competed in Olympic Games in ";

String s = "It is " + true + claim + a +


" but " + false + claim + b + ".";

System.out.println(s);

What, if anything, is printed when the code segment is executed?


(A) It is trueclaima but falseclaimb.
(B) It is trueclaim1998 but falseclaim1990.
It is true that the world’s athletes competed in Olympic Games in a but
(C)
false that the world’s athletes competed in Olympic Games in b.
It is true that the world’s athletes competed in Olympic Games in 1988
(D)
but false that the world’s athletes competed in Olympic Games in 1990.
(E) Nothing is printed because the code segment does not compile.

17. A student has created an OrderedPair class to represent points on an -plane. The class contains the
following.

An int variable called x to represent an -coordinate.


An int variable called y to represent a -coordinate.
A method called printXY that will print the values of x and y.

The object origin will be declared as type OrderedPair.

Which of the following descriptions is accurate?


(A) origin is an instance of the printXY method.
(B) origin is an instance of the OrderedPair class.
(C) origin is an instance of two int objects.
(D) OrderedPair is an instance of the origin object.
(E) printXY is an instance of the OrderedPair class.

Page 8 of 16 AP Computer Science A


Test Booklet

Unit 2 Topic Questions

18. Consider the following Point2D class.

public class Point2D


{
private double xCoord;
private double yCoord;

public Point2D(double x, double y)


{
xCoord = x;
yCoord = y;
}
}

Which of the following code segments, appearing in a class other than Point2D, will correctly create an instance
of a Point2D object?
(A) Point2D p = (3.0, 4.0);
(B) Point2D p = Point2D(3.0, 4.0);
(C) new p = Point2D(3.0, 4.0);
(D) new Point2D = p(3.0, 4.0);
(E) Point2D p = new Point2D(3.0, 4.0);

19. Consider the following methods, which appear in the same class.

public void printSum(int x, double y)


{
System.out.println(x + y);
}

public void printProduct(double x, int y)


{
System.out.println(x * y);
}

Consider the following code segment, which appears in a method in the same class as printSum and
printProduct.

int num1 = 5;
double num2 = 10.0;
printSum(num1, num2);
printProduct(num1, num2);

What, if anything, is printed as a result of executing the code segment?

AP Computer Science A Page 9 of 16


Test Booklet

Unit 2 Topic Questions

15
(A) 50

15
(B) 50.0
15.0
(C) 50
15.0
(D) 50.0

(E) Nothing is printed because the code does not compile.

20. Consider the following method.

public double puzzle(int x)


{
Double y = x / 2.0;
y /= 2;

return y.doubleValue();
}

Assume that the method call puzzle(3) appears in a method in the same class as puzzle. What value is
returned as a result of the method call?
(A) 0.0
(B) 0.5
(C) 0.75
(D) 1.0
(E) 1.5

21. Which of the following statements assigns a random integer between 1 and 10, inclusive, to rn ?
(A) int rn = (int) (Math.random()) * 10;
(B) int rn = (int) (Math.random()) * 10 + 1;
(C) int rn = (int) (Math.random() * 10);
(D) int rn = (int) (Math.random() * 10) + 1;
(E) int rn = (int) (Math.random() + 1) * 10;

22. Consider the following code segment, which is intended to assign to num a random integer value between min
and max, inclusive. Assume that min and max are integer variables and that the value of max is greater than
the value of min.

double rn = Math.random();
int num = /* missing code */;

Which of the following could be used to replace /* missing code */ so that the code segment works as
intended?

Page 10 of 16 AP Computer Science A


Test Booklet

Unit 2 Topic Questions

(A) (int) (rn * max) + min


(B) (int) (rn * max) + min - 1
(C) (int) (rn * (max - min)) + min
(D) (int) (rn * (max - min)) + 1
(E) (int) (rn * (max - min + 1)) + min

23. A student has created a Song class. The class contains the following variables.

A String variable called artist to represent the artist name


A String variable called title to represent the song title
A String variable called album to represent the album title

The object happyBirthday will be declared as type Song.

Which of the following statements is true?


(A) artist, title, and album are instances of the Song class.
(B) happyBirthday is an instance of three String objects.
(C) happyBirthday is an instance of the Song class.
(D) Song is an instance of the happyBirthday object.
(E) Song is an instance of three String objects.

AP Computer Science A Page 11 of 16


Test Booklet

Unit 2 Topic Questions

24. Consider the following class definition.

public class Student


{
private int studentID;
private int gradeLevel;
private boolean honorRoll;

public Student(int s, int g)


{
studentID = s;
gradeLevel = g;
honorRoll = false;
}

public Student(int s)
{
studentID = s;
gradeLevel = 9;
honorRoll = false;
}
}

Which of the following code segments would successfully create a new Student object?

I. Student one = new Student(328564, 11);


II. Student two = new Student(238783);
III. int id = 392349;
int grade = 11;
Student three = new Student(id, grade);
(A) I only
(B) II only
(C) III only
(D) I and II only
(E) I, II, and III

Page 12 of 16 AP Computer Science A


Test Booklet

Unit 2 Topic Questions

25. SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.

Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods are
called only when their preconditions are satisfied.
In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined
in that question. Writing significant amounts of code that can be replaced by a call to one of these methods will
not receive full credit.

Consider the following Thing class. Each Thing object has a name attribute, which can be set in the constructor or by
using the setName method. The name of a Thing object can be returned by the getName method.

public class Thing


{
// attributes not shown

/** Constructs a new Thing named myName


*/
public Thing(String myName)
{ /* implementation not shown */ }

/** Returns this Thing’s name


*/
public String getName()
{ /* implementation not shown */ }

/** Sets this Thing’s name to newName


*/
public void setName(String newName)
{ /* implementation not shown */ }

/** Returns a message as described in part (b)


*/
public void printMessage()
{ /* implementation not shown */ }
}

(a) Write a statement to create a new Thing object snack that has the name "potato chip".

Write the statement below.

(b) The Thing method printMessage prints a string consisting of the name of the object followed by
"_is_great".

Suppose the name of the Thing object favFood is "pizza". Write a statement that uses the printMessage

AP Computer Science A Page 13 of 16


Test Booklet

Unit 2 Topic Questions

method to print the string "pizza_is_great".

Write the statement below.

(c) Write a code segment to change the name of the Thing object something such that the new name consists of
the old name with one character removed at random. For example, if something has name "ABCD", its new name
could be set to "ACD".

Write the code segment below.

26. Consider the following class definition.

public class Thing


{
public void talk()
{
System.out.print("Hello ");
}

public void name()


{
System.out.print("my friend");
}

public void greet()


{
talk();
name();
}
/* Constructors not shown */
}

Which of the following code segments, if located in a method in a class other than Thing, will cause the message
"Hello my friend" to be printed?
Thing a = new Thing();
(A) Thing.talk();
Thing.name();
Thing a = new Thing();
(B)
Thing.greet();
Thing a = new Thing();
(C)
a.talk();
Thing a = new Thing();
(D)
a.greet();
Thing a = new Thing();
(E) a.name();
a.talk();

Page 14 of 16 AP Computer Science A


Test Booklet

Unit 2 Topic Questions

27. Consider the following method.

public int timesTwo (int n)


{
return n * 2;
}

The following code segment appears in a method in the same class as the timesTwo method.

Integer val = 10;


int result1 = timesTwo(val);
Integer result2 = result1;
System.out.print(result2);

What, if anything, is printed as a result of executing the code segment?


(A) 10
(B) 20
(C) Nothing; the code segment will not compile because timesTwo cannot accept an Integer parameter.
Nothing; the code segment will not compile because the value returned by timesTwo cannot be assigned
(D)
to result1.
Nothing; the code segment will not compile because the int variable result1 cannot be assigned to
(E)
the Integer variable result2.

28. Consider the following class.

public class WindTurbine


{
private double efficiencyRating;
public WindTurbine()
{
efficiencyRating = 0.0;
}
public WindTurbine(double e)
{
efficiencyRating = e;
}
}

Which of the following code segments, when placed in a method in a class other than WindTurbine, will
construct a WindTurbine object wt with an efficiencyRating of 0.25 ?

AP Computer Science A Page 15 of 16


Test Booklet

Unit 2 Topic Questions

(A) WindTurbine wt = new WindTurbine(0.25);


(B) WindTurbine wt = 0.25;
WindTurbine wt = new WindTurbine();
(C)
wt = 0.25;
WindTurbine wt = new WindTurbine();
(D)
wt.efficiencyRating = 0.25;
(E) new WindTurbine wt = 0.25;

Page 16 of 16 AP Computer Science A

You might also like