0% found this document useful (0 votes)
45 views23 pages

Practice Test MC

AP Computer Science Practice Test MCQ

Uploaded by

apatel356
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views23 pages

Practice Test MC

AP Computer Science Practice Test MCQ

Uploaded by

apatel356
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

AP Computer Science A Review Test 1

1. (B5) Assume that p, q, and r are boolean variables. Consider the following expression.

!((p || q) && (q || !r))

Which of the following expressions is equivalent to the given expression?

A. (p || q) && (p || !r) && (q) && (q || !r)


B. (!p && !q) || (!p && r) || (!q) || (!q && r)
C. (!p || !q) && (!q || r)
D. (!p && !q) || (!q && r)
E. (p && q) || (q && !r)

2. (DS9) The simple game of Nim has a single pile of stones and two players who alternately remove at least one
and up to half the stones in the pile, until one player removes the last stone. The player that removes the last
stone loses. A Java implementation of this game includes a class Pile, an abstract class Player with different
subclasses such as HumanPlayer, DumbComputerPlayer, SmartComputerPlayer, and a GameController class.
The GameController is responsible for asking each player in turn to indicate how many stones that player wants
to take, for checking that that number is legal, and, if it is legal to adjust the pile, if not to ask the same player
again. The Game Controller continues to have the players alternate turns until the game ends when one player
takes the last stone. The GameController constructor will initialize all its instance variables with appropriate
parameters.
Which of the following is the best design for the GameController class?

A. GameController has no instance variables. It has a method makePlay that takes a pile and a player as
parameters and completes one play (taking of stones) by that player. Its main loop alternately calls
the makePlay method for each player.
B. GameController has a Pile instance variable. It has a method makePlay that takes a Player parameter
and completes one play (taking of stones) by that player. Its main loop alternately calls the makePlay
method for each player.
C. GameController has a Player instance variable for each player. It has a method makePlay that takes a
Pile parameter and completes one play (taking of stones) by a player. Its main loop alternately calls
the makePlay method for each player.
D. GameController has a Player instance variable for each player and a Pile instance variable. It has a
method makePlay that takes a Player parameter and completes one play (taking of stones) by a
player. Its main loop alternately calls the makePlay method for each player.
E. GameController has a HumanPlayer instance variable, a SmartComputerPlayer instance variable and
a Pile instance variable. It has a method makePlay that takes a Player parameter and completes one
play (taking of stones) by a player. Its main loop alternately calls the makePlay method for each
player.

1
AP Computer Science A Review Test 1
3. (A1) Consider the following data field and method.

private double[] list;

public int getIt(double value)


{
int k = 0;

while(k < list.length && list[k] != value)


k++;

if(k < list.length)


return k;
else
return -1;
}
Which of the following best describes what is returned by this method?

A. The number of times that value occurs in list, or -1 if all items in the list equal value.
B. The number of items in list that are not equal to value, or -1 if value is not in list.
C. The index of the first occurrence of value in list, or -1 if value is not in list
D. The index of the last occurrence of value in list, or -1 if value is not in list
E. The index of the first item in list that is not equal to value, or -1 if value is not in list

4.(B1) A class Indicator has a method level that returns an integer. Consider this method from a client class.

// precondition: low < high


// postcondition: return true if the current level of dial is between low and high
// inclusive; otherwise return false
public boolean inRange(Indicator dial, int low, int high)
{
// missing code
}
The method inRange is intended to return true when the value returned by the dial level method is between low and high
inclusive. Consider the following replacements for // missing code.

I. if(dial.level() < low)


return false;
else if (dial.level() > high)
return false;
else
return true;

II. if((dial.level() < low) && (dial.level() > high))


return false;
else
return true;

III. return ((dial.level() >= low) && (dial.level() <= high));

Which of these replacements for // missing code would make inRange work as intended?

A. I only
B. II only
C. I and II only
D. I and III only
E. I, II, and III

2
AP Computer Science A Review Test 1

5. (C1) Consider the following description.


A forest has trees. A white pine is a pine which is a tree.

Which of the following partial declarations would be the best choice for representing the relationships among these
things?

A. public class Forest


public class Tree extends Forest
public class Pine extends Tree
public class WhitePine extends Pine

B. public class WhitePine


public class Pine extends WhitePine
public class Tree extends Pine
public class Forest extends Tree

C. public class WhitePine


public class Pine extends WhitePine
public class Tree extends Pine

public class Forest


{
private Pine[] myPines;
private WhitePine[] myWhitePines;
. . .
}

D. public class Tree


public class Pine extends Tree
public class WhitePine extends Pine

public class Forest


{
private Tree[] myTrees;
. . .
}

E. public class Forest

public class Tree


{
private Forest myForest;
. . .
}
public class Pine extends Tree
public class WhitePine extends Pine

3
AP Computer Science A Review Test 1

6. (C6) Assume that the following partial declarations have been made, with default constructors for the classes.

public abstract class Controller

public class Widget extends Controller

public class Thingy extends Widget

Consider the following declarations.

I. Widget myThing = new Thingy();

II. Thingy myWidge = new Widget();

III. Controller myControl = new Thingy();

Which of these declarations will compile correctly?

A. I only
B. I and II only
C. I and III only
D. II and III only
E. I, II, and III

7. (C8) Which of the following statements about an abstract class is true?

A. An abstract class can only have a default (no parameter) constructor.


B. An abstract class cannot have instance variables.
C. An abstract class can only have methods that are public.
D. An abstract class can have some methods that are implemented and some that are not.
E. An abstract class can only have methods that are declared abstract and are not implemented.

4
AP Computer Science A Review Test 1

8 (SL1) Consider the following declarations.

public class Something


{
public String name()
{ /* code not shown */ }

public int value()


{ /* code not shown */ }

// instance variables, constructors, other methods not shown


}

Something[] list = new Something[10];

Assume that the elements of the array list have been initialized.

Which of the following gives a reference to the name of the second item in list?

A. list.name(1)
B. list.name()[1]
C. list[1].name()
D. name.list[1]
E. name(list[1])

9. (CTA1) Consider the following code segment.

for(int num = 20; num >= 0; num--)


{
if(num % 4 == 2)
System.out.print(num + " ");
}

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

A. 19 18 17 15 14 13 11 10 9 7 6 5 3 2 1
B. 20 18 16 14 12 10 8 6 4 2 0
C. 18 14 10 6 2
D. 20 16 12 8 4 0
E. 6

5
AP Computer Science A Review Test 1

10. (B3) A guidebook places hotel prices into four categories: category 1 ($) means the price is $50 or below; category 2
($$) means the price is greater than $50 but less than or equal $80; category 3 ($$$) means the price is greater than $80
but less than or equal $120; category 4 ($$$$) means the price is greater than $120. The method priceCategory is
intended to return a string with the number of '$'s for the category of the parameter price.

public String priceCategory(double price)


{
// missing code
}

Consider the following replacements for // missing code.

I. String symbol;
if(price <= 50.00)
symbol = "$";
if(price <= 80.00)
symbol = "$$";
if(price <= 120.00)
symbol = "$$$";
else
symbol = "$$$$";
return symbol;

II. String symbol;


if(price <= 50.00)
symbol = "$";
if(50.00 < price <= 80.00)
symbol = "$$";
if(80.00 < price <= 120.00)
symbol = "$$$";
else
symbol = "$$$$";
return symbol;

III. if(price <= 50.00)


return "$";
if(price <= 80.00)
return "$$";
if(price <= 120.00)
return "$$$";
return "$$$$";

Which of these replacements for // missing code would make priceCategory work as intended?

A. I only
B. II only
C. III only
D. I and II
E. I and III

6
AP Computer Science A Review Test 1

11. (C11) Consider the following declarations.

public class ColorBox


{
public ColorBox()
{
System.out.print("black ");
}

public void showColor()


{
System.out.print("red ");
}
}

public class BlueGreenBox extends ColorBox


{
public BlueGreenBox()
{
System.out.print("blue ");
}

public void showColor()


{
super.showColor();
System.out.print("green ");
}
}

The following statements occur in a client method:

ColorBox box = new BlueGreenBox();


box.showColor();

What is printed when these two lines are executed?

A. black red
B. blue green
C. blue red green
D. blue black red green
E. black blue red green

12. (DS3) The principle of information hiding suggests that in Java all variable data stored in an object should be in
private data fields for the class defining the object, and accessed or changed only using appropriate public methods.
Which of the following is the most accurate statement about this principle?

A. When this principle is used, the representation of the data can be changed without any code in client programs needing
to be changed.
B. When this principle is used, the representation of the data can be changed without any code in the public methods of
the defining class needing to be changed.
C. This principle only applies to objects that have multiple methods for changing their internal data values; objects that
have a single method for changing each data field should use public data fields.
D. This principle only applies to objects that have multiple methods for accessing their internal data values; objects that
have a single method for accessing each data field should use public data fields.
E. This principle only applies to objects that have mutator methods that can change the values of one or more data fields.
It does not apply to immutable objects.
7
AP Computer Science A Review Test 1

13. (CTA10) Consider the following static method

public static double getSomething(int val)


{
val = 2 + val;
val = val + 3*val;

return val;
}

Which of the following could be used to replace the body of getSomething so that the modified version will return the
same result as the original version for all values of the parameter val.

A. return 4*val + 2;

B. return 4*val + 6;

C. return 4*val + 8;

D. return 7*val + 6;

E. return 7*val + 8;

14 (A21). Consider the incomplete method headCount(int numFlips).

public int headCount(int numFlips)


{
int count = 0;
// missing code
return count;
}

Method headCount(int numFlips)is intended to return the number of "heads" from numFlips flips of a fair coin
(the probability of "heads" on one flip is ½ ). Consider the following replacements for // missing code.

I while(count <= numFlips && Math.random() < 0.5)


count++;

II for(int flip = 0; flip < numFlips; flip++)


{
if(Math.random() < 0.5)
count++;
}

III for(int flip = 0; flip < numFlips; flip++)


{
if((int)(2 * Math.random()) == 1)
count++;
}

Which if these replacements for // missing code will work as intended?

A. I only B. II only C. I and II only D. II and III only E. I, II and III

8
AP Computer Science A Review Test 1

15. (SL10) Consider the following partial class declarations.

public abstract class RentalProperty


{
public double monthlyPayment()
{ /* code segment 1 not shown */ }

// other methods not shown


}

public class HouseRental extends RentalProperty


{
public double monthlyPayment()
{ /* code segment 2 not shown */ }

// constructors, instance variables, and other methods not shown


}

The following code appears in a method in another class.

RentalProperty property = new HouseRental();

System.out.println(property.monthlyPayment());

Which of the following is true?

A. The abstract class RentalProperty will not compile.

B. The call to property.monthlyPayment() will cause a run-time error due to the ambiguity about which code
segment should be executed, code segment 1 or code segment 2.

C. The call to property.monthlyPayment() will first execute code segment 1 followed by code segment 2.

D. The call to property.monthlyPayment() will first execute code segment 2 followed by code segment 1.

E. The call to property.monthlyPayment() will execute code segment 2.

16. (SL3) Consider the following partial class declaration.

public class Student


{
private String myName;
private double myGPA;

public String name()


{ return myName; }

public double GPA()


{ return myGPA; }

// returns the name and GPA in string format


public String toString()
{
return // String expression
}
9
AP Computer Science A Review Test 1
// constructors and other methods not shown
}

Consider the following replacements for // string expression in the toString method.

I. myName + " " + myGPA;


II. Student.name() + " " + Student.GPA();
III. name() + " " + GPA();

Which of these replacements will make the method toString work as intended?

A. I only
B. I and II only
C. I and III only
D. II and III only
E. I, II, and III

10
AP Computer Science A Review Test 1

17. (CTA3) The following code segment is intended to sum the first 10 positive odd integers.

sum = 0;
for(int k = 1; k <= 10; k++)
{
sum += 2*k + 1;
}

Which of the following best describes the error, if any, in this code.

A. The segment works as intended.


B. The segment sums the first 9 odd integers
C. The segment sums the first 20 odd integers
D. The segment leaves out the first odd integer and includes the eleventh odd integer in the sum.
E. The variable sum is incorrectly initialized. The segment would work as intended if sum were initialized to 1.

11
AP Computer Science A Review Test 1

Questions 18-19 refer to code from the GridWorld case study. A copy of the code is supplied in the appendix.

18. (GW1).
Assume that the BugRunner program is being executed. Assume the grid currently contains a single instance of Bug
with a flower immediately in front of it. What happens when the step button is clicked?

A. The bug does not change state


B. The bug turns 45 degrees to the left
C. The bug turns 45 degrees to the right
D. The bug moves into the space occupied by the flower, removing the flower from the grid
E. The bug turns into a rock

19. (GW9).
Suppose the grid has the following configuration after adding some objects.

Let bug1 be the one at position (1, 2) (the upper bug) and bug2 the one at position(2,2) (the lower bug). What will be the
results of the calls to canMove() for bug 1 and bug 2?

A. bug1.canMove() returns false; bug2.canMove() returns false


B. bug1.canMove() returns true; bug2.canMove() returns false
C. bug1.canMove() returns false; bug2.canMove() returns true
D. bug1.canMove() returns 3; bug2.canMove() returns -1
E. bug1.canMove() returns 0; bug2.canMove() returns 1

12
AP Computer Science A Review Test 1

20. (A5) Consider the following declaration

public class HasHeight


{
// returns the height of an item
double height();
}

Assume the following data field and method are declared in another class.

private HasHeight[] list;

public void inSort()


{
for(int start = 1; start < list.length; start++)
{
HasHeight temp = list[start];
int index = start;
while(/* condition */)
{
/* statement */
index--;
}
list[index] = temp;
}
}

The method inSort is intended to sort list into increasing order by height. Which of the following replacements for
/* condition */ and /* statement */ will make inSort work as intended?

condition statement

A. index >= 0 && temp.height() < list[index].height()


list[index+1] = list[index]

B. index >= 0 && temp.height() < list[index].height()


list[index] = list[index-1]

C. index > 0 && temp.height() < list[index-1].height()


list[index] = list[index-1]

D. index >= 0 && temp.height() < list[index-1].height()


list[index] = list[index-1]

E. index > 0 && temp.height() < list[index-1].height()


list[index+1] = list[index]

13
AP Computer Science A Review Test 1

21. (DS5) A class CardDeck will keep track of a deck of playing cards, a sequence of instances of the class
PlayingCard. The chief responsibility for an instance of CardDeck is to deal, to remove and return one card at a time in
random order, as if the card deck had been shuffled. Consider the following designs for the CardDeck class.

I. When the CardDeck is created, store the instances of PlayingCard in a partially-filled array in sorted
order. When a card needs to be dealt, generate a random number and remove the PlayingCard at that
index from the array and return it.

II. When the CardDeck is created, store the instances of PlayingCard in a partially-filled array in sorted
order; then run an auxiliary method that randomizes the order of the PlayingCards in the array. When
a card needs to be dealt, remove the last card from the array and return it.

III. When the CardDeck is created, store the instances of PlayingCard in an array in sorted order; then run
an auxiliary method that randomizes the order of the PlayingCards in the array. Initialize an instance
variable top to equal the length of the array. When a card needs to be dealt, decrement top by one and
return a reference to the card at index top in the array.

Which of these designs will satisfy the responsibility of the CardDeck class to deal a card as described above?

A. I only
B. II only
C. I and II only
D. I and III only
E. I, II and III

14
AP Computer Science A Review Test 1

22. (C10) Consider the following declarations, where some lines have been numbered for reference.

public abstract class Controller


{
1 void doIt();
}

2 public abstract class Widget extends Controller


{
private double myValue;

public Widget()
{
myValue = 10;
}

public double value()


{
return myValue;
}
}

public class Thingy extends Widget


{
public void doIt()
{
3 System.out.println( 3.0 * value());
}
}

public class Client


{
public void doSomething()
{
4 Controller myControl = new Thingy();
myControl.doIt();
}
}

You attempt to compile and run these classes (with a class that correctly calls the Client doSomething method).
Which of the following best describes the outcome?

A. The code does not compile because in line 1 the doIt method is not declared public.
B. The code does not compile because in line 2 the class Widget implements Controller, but it has no implementation
of the method doIt.
C. The code does not compile because the variable myValue cannot be accessed directly or indirectly within the class
Thingy, as it is in line 3.
D. The code does not compile because an instance of class Thingy is not type-compatible with Controller, as
indicated in line 4.
E. The code compiles and runs with the value 30.0 output when the call to doSomething is made.

15
AP Computer Science A Review Test 1

23. (A7) Consider the following partially-filled array and associated methods.

private String[] list;


private int size;

public void change()


{
for(int k = 0; k < size - 1; k++)
{
if(list[k].equals(list[k+1]))
{
shift(k+1);
size--;
}
}
}

public void shift(int x)


{
for(int i = x; i < size - 1; i++)
{
list[i] = list[i+1]
}
}

Assume that list and size are initialized as shown below.

list alex alex brad nils nils nils pete pete pete pete
size = 10

Which of the following shows the first size elements of list after a call to method change()?

A. alex brad nils pete

B. alex brad nils pete pete

C. alex brad nils nils pete pete

D. alex brad nils nils pete pete pete

E. alex alex brad nils nils nils pete pete pete pete

16
AP Computer Science A Review Test 1

24. (SL7) Consider the following partial class declaration.

public class Point2D


{
private double myX;
private double myY;

public Point2D(double x, double y)


{
myX = x;
myY = y;
}

public boolean equals(Object obj)


{
Point2D other = (Point2D)obj;

return (myX == other.myX) && (myY == other.myY);


}

// other methods not shown


}

Consider the following method isOrigin from another class. Method isOrigin is intended to return true if and only
if point has both coordinates equal to 0.0.

// returns true if the coordinates of point are both 0.0,


// otherwise returns false
public boolean isOrigin(Point2D point)
{
Point2D origin = new Point2D(0.0, 0.0);
return /* expression */;
}
Which of the following expressions substituted for /* expression */ will make isOrigin work as intended?

A. origin.myX == point.myX && origin.myY == point.myY

B. origin.myX.equals(point.myX) && origin.myY.equals(point.myY)

C. origin == point

D. origin.equals(point)

E. Math.equals(origin, point)

17
AP Computer Science A Review Test 1

25. (A3) Consider the following declaration

public class Landmark


{
// returns the distance from home
double distanceFromHome()
}

Assume the following data field and method are declared in another class.

private Landmark[] map;

public Landmark getNextClosest(Landmark marker)


{
Landmark item = marker;

for(Landmark current: map)


{
if(current.distanceFromHome() > marker.distanceFromHome() &&
current.distanceFromHome() < item.distanceFromHome())
item = current;
}
return item;
}

Assume that tower is a Landmark. The call to the method getNextClosest(tower) is intended to return the item
from map that has the smallest distance from home, among those items that have a distance from home that is greater than
the distance from home for tower. If there is no such item, the method should return tower itself. What does call
getNextClosest(tower) actually return?

A. The call always works as intended.


B. The call always returns the item in map that has the smallest distance from home.
C. The call always returns the item in map that has the largest distance from home.
D. The call always returns the last item in map.
E. The call always returns tower.

18
AP Computer Science A Review Test 1
26. (CTA5) Consider the following code segment.

for(int num = 5; num > 0; num = num - 2)


{
for(int star = 0; star < num; star++)
System.out.print("*");
System.out.println();
}

What will be printed when this code segment is executed?

A. * D. *****
** ***
*** *
****
*****

B. * E. ****
*** **
*****

C. *****
****
***
**
*

19
AP Computer Science A Review Test 1

27. (C3) Consider the following partial declarations.

public class Location


{
public Location(int xCoord, int yCoord)
. . .
}
public class Color
{
public Color(int redVal, int greenVal, int blueVal)
. . .
}
public class Widget
{
private Location myLoc;

public Widget(int x, int y)


{
myLoc = new Location(x, y);
}
. . .
}
public class Thingy extends Widget
{
private Color myColor;

public Thingy(int x, int y, Color col)


{
super(x, y);
myColor = col;
}
. . .
}

Assume that the following statement appears in a client program.

Widget widg = new Thingy(100, 100, new Color(100, 100, 100));

Which of the following best describes the order on which the constructors will complete execution?

A. Color, Location, Widget, Thingy


B. Widget, Thingy, Location, Color
C. Color, Widget, Location, Thingy
D. Thingy, Color, Widget, Location
E. Color, Thingy, Location, Widget

20
AP Computer Science A Review Test 1

28. (B8) Assume that an object referenced by the variable gadget has two boolean methods, tooWide and tooLong.
Consider the following expression.

(gadget.tooWide() && !gadget.tooLong()) || gadget.tooLong()

Which of the following is equivalent to the expression above?

A. true
B. false
C. gadget.tooWide()
D. gadget.tooWide() || gadget.tooLong()
E. (!gadget.tooWide() || gadget.tooLong()) && !gadget.tooLong()

21
AP Computer Science A Review Test 1

29. (A9) Consider the partial method definition.

// len > 3
public void printTriangle(int len)
{
/* method body */
}

This method is intended to print a hollow triangle as diagrammed below, where the number of stars along each side is
given by the parameter len, where len > 3. The result of the call printTriangle(6) would be this diagram.

*
**
* *
* *
* *
******

Which of the following replacements for /* method body */ would make the method printTriangle work as
intended?

A. for(row = 0; row < len; row++)


{
for(k = 0; k < row+1; k++)
System.out.print("*");
System.out.println();
} D. System.out.println("*");
for(row = 1; row < len-1; row++)
B. System.out.println("*"); {
for(row = 0; row < len; row++) System.out.print("*");
{ for(k = 0; k < row-1; k++)
System.out.print("*"); System.out.print(" ");
for(k = 0; k < row; k++) System.out.println("*");
System.out.print(" "); }
System.out.println("*"); for(star = 0; star < len; star++)
} System.out.print("*");
for(star = 0; star < len; star++)
System.out.print("*");
E. System.out.println("*");
C. System.out.println("*"); for(row = 1; row < len-1; row++)
for(row = 1; row < len-1; row++) {
{ System.out.print("*");
System.out.print("*"); for(k = 0; k < row-2; k++)
for(k = 0; k < row; k++) System.out.print(" ");
System.out.print(" "); System.out.println("*");
System.out.println("*"); }
} for(star = 0; star < len; star++)
for(star = 0; star < len; star++) System.out.print("*");
System.out.print("*");

22
AP Computer Science A Review Test 1

30. (A11) Consider the following data field and method.

private int[] valList;

public void changeList()


{
int currentLoc = 0;
int nonZeroLoc = 0;

while(nonZeroLoc < valList.length && valList[nonZeroLoc] == 0)


nonZeroLoc++;

while(nonZeroLoc < valList.length)


{
valList[currentLoc] = valList[nonZeroLoc];
valList[nonZeroLoc] = 0;
currentLoc++;
nonZeroLoc++;

while(nonZeroLoc < valList.length && valList[nonZeroLoc] == 0)


nonZeroLoc++;
}
}

Assume that valList is initially the following array.

4 0 0 3 2 0 6 5 0

Which of the following best represents valList after the call changeList()?

A. 4 0 0 3 2 0 6 5 0

B. 4 3 2 6 5 0 0 0 0

C. 0 3 2 6 5 0 0 0 0

D. 0 0 0 0 2 3 4 5 6

E. The call will result in an ArrayIndexOutOfBoundsException.

23

You might also like