Practice Test MC
Practice Test MC
1. (B5) Assume that p, q, and r are boolean variables. Consider the following expression.
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.
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.
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
Which of the following partial declarations would be the best choice for representing the relationships among these
things?
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.
A. I only
B. I and II only
C. I and III only
D. II and III only
E. I, II, and III
4
AP Computer Science A Review Test 1
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])
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.
I. String symbol;
if(price <= 50.00)
symbol = "$";
if(price <= 80.00)
symbol = "$$";
if(price <= 120.00)
symbol = "$$$";
else
symbol = "$$$$";
return symbol;
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
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
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;
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.
8
AP Computer Science A Review Test 1
System.out.println(property.monthlyPayment());
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.
Consider the following replacements for // string expression in the toString method.
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.
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?
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?
12
AP Computer Science A Review Test 1
Assume the following data field and method are declared in another class.
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
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 Widget()
{
myValue = 10;
}
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.
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()?
E. alex alex brad nils nils nils pete pete pete pete
16
AP Computer Science A Review Test 1
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.
C. origin == point
D. origin.equals(point)
E. Math.equals(origin, point)
17
AP Computer Science A Review Test 1
Assume the following data field and method are declared in another class.
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?
18
AP Computer Science A Review Test 1
26. (CTA5) Consider the following code segment.
A. * D. *****
** ***
*** *
****
*****
B. * E. ****
*** **
*****
C. *****
****
***
**
*
19
AP Computer Science A Review Test 1
Which of the following best describes the order on which the constructors will complete execution?
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.
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
// 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?
22
AP Computer Science A Review Test 1
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
23