AP - Section II - QP
AP - Section II - QP
COMPUTER SCIENCE
SECTION II
Diagnostic Test
Time—1 hour and 45 minutes
Number of questions—4
Percent of total grade—50
(a) Consider the following incomplete ArrayUtil class, which contains a static
reverseArray method.
public class ArrayUtil
{
/** Reverses elements of array arr.
* Precondition: arr.length > 0.
* Postcondition: The elements of arr have been reversed.
* @param arr the array to manipulate
*/
public static void reverseArray(int[] arr)
{ /* to be implemented in part (a) */ }
✐ ✐
✐ ✐
✐ ✐
“ap” — 2014/11/4 — 11:10 — page 34 — #48
✐ ✐
34 Practice Exam
Write the ArrayUtil method reverseArray. For example, if arr is the array
{2,7,5,1,0}, the call to reverseArray changes arr to be {0,1,5,7,2}.
Diagnostic Test
(b) Consider the following incomplete Matrix class, which represents a two-
dimensional matrix of integers. Assume that the matrix contains at least
one integer.
✐ ✐
✐ ✐
✐ ✐
“ap” — 2014/11/4 — 11:10 — page 35 — #49
✐ ✐
Write the Matrix method reverseAllRows. This method reverses the ele-
ments of each row. For example, if mat1 refers to a Matrix object, then the
Diagnostic Test
call mat1.reverseAllRows() will change the matrix as shown below.
(c) Write the Matrix method reverseMatrix. This method reverses the ele-
ments of a matrix such that the final elements of the matrix, when read in
row-major order, are the same as the original elements when read from the
bottom corner, right to left, going upward. Again let mat1 be a reference to
a Matrix object. The the call mat1.reverseMatrix() will change the matrix
as shown below.
Before call After call
0 1 0 1
0 1 2 0 6 5
1 3 4 1 4 3
2 5 6 2 2 1
✐ ✐
✐ ✐
✐ ✐
“ap” — 2014/11/4 — 11:10 — page 36 — #50
✐ ✐
36 Practice Exam
sentence is a letter, and the last character is a punctuation mark. Any two words
in the sentence are separated by a single blank. A partial implementation of the
Sentence class is as follows.
✐ ✐
✐ ✐
✐ ✐
“ap” — 2014/11/4 — 11:10 — page 37 — #51
✐ ✐
Diagnostic Test
/** @return an ArrayList of integer positions containing a
* blank in this sentence. If there are no blanks in the
* sentence, returns an empty list.
*/
public List<Integer> getBlankPositions()
(b) Write the Sentence method countWords, which returns the number of words
in a sentence. Words are sequences of letters or punctuation, separated by
a single blank. You may assume that every sentence contains at least one
word.
For example:
(c) Write the Sentence method getWords, which returns an array of words in
the sentence. A word is defined as a string of letters and punctuation, and
does not contain any blanks. You may assume that a sentence contains at
least one word.
Some examples of calling getWords are shown below.
✐ ✐
✐ ✐
✐ ✐
“ap” — 2014/11/4 — 11:10 — page 38 — #52
✐ ✐
38 Practice Exam
3. In this question you will implement two methods for a class Tournament that
keeps track of the players who have registered for a tournament. The Tournament
Diagnostic Test
class uses the Player class shown below. A Player has a name and player number
specified when a player is constructed.
An incomplete declaration for the Tournament class is shown below. There are
100 available slots for players in the tournament, and the players are numbered
0, 1, 2, . . . , 99.
✐ ✐
✐ ✐
✐ ✐
“ap” — 2014/11/4 — 11:10 — page 39 — #53
✐ ✐
Diagnostic Test
* empty slot. Create and return the new Player.
* If there are no available slots, add the player’s name
* to the end of the waiting list and return null.
* @playerName the name of the person requesting a slot
* @return the new Player
*/
public Player requestSlot(String playerName)
{ /* to be implemented in part (a) */ }
/** Release the slot for player p, thus removing that player
* from the tournament. If there are any names in waitingList,
* remove the first name and create a Player in the
* canceled slot for this person. Return the new Player.
* If waitingList is empty, mark the slot specified by p as
* empty and return null.
* Precondition: p is a valid Player for some slot in
* this tournament.
* @param p the player who will be removed from the tournament
* @return the new Player placed in the canceled slot
*/
public Player cancelAndReassignSlot(Player p)
{ /* to be implemented in part (b) */ }
✐ ✐
✐ ✐
✐ ✐
“ap” — 2014/11/4 — 11:10 — page 40 — #54
✐ ✐
40 Practice Exam
tains any names, the newly available slot is reassigned to the person at the
front of the list. That person’s name is removed from the waiting list, and
the newly created Player is returned. If the waiting list is empty, the newly
released slot is marked as empty, and null is returned.
In writing cancelAndReassignSlot, you may use any accessible methods
in the Player and Tournament classes. Assume that these methods work as
specified.
/** Release the slot for player p, thus removing that player
* from the tournament. If there are any names in waitingList,
* remove the first name and create a Player in the
* canceled slot for this person. Return the new Player.
* If waitingList is empty, mark the slot specified by p as
* empty and return null.
* Precondition: p is a valid Player for some slot in
* this tournament.
* @param p the player who will be removed from the tournament
* @return the new Player placed in the canceled slot
*/
public Player cancelAndReassignSlot(Player p)
✐ ✐
✐ ✐
✐ ✐
“ap” — 2015/3/24 — 19:36 — page 41 — #55
✐ ✐
Diagnostic Test
/** @return an integer value that ranges from 1 (very acidic)
* to 14 */
int getPH();
The experiment keeps track of the solutions and the mechanical arm. The figure
below represents the solutions and mechanical arm in an experiment. The arm,
indicated by the arrow, is currently at index 4 and is facing left. The second row
of integers represents the pH values of the solutions.
index 0 1 2 3 4 5 6
pH 7 4 10 5 6 7 13
In this experiment, the most acidic solution is at index 1, since its pH value is the
lowest.
The state of the mechanical arm includes the index of its location and direction
it is facing (to the right or to the left). A mechanical arm is specified by the
MechanicalArm interface below.
✐ ✐
✐ ✐
✐ ✐
“ap” — 2014/11/4 — 11:10 — page 42 — #56
✐ ✐
42 Practice Exam
/** Finds and returns the index of the most acidic solution.
* @return index the location of the most acidic solution
* or -1 if there are no acidic solutions
* Postcondition:
* - The mechanical arm is facing right.
* - Its current index is at the most acidic solution, or at
* 0 if there are no acidic solutions.
*/
public int mostAcidic()
{ /* to be implemented in part (b) */ }
}
(a) Write the Experiment method reset that places the mechanical arm facing
right, at index 0.
For example, suppose the experiment contains the solutions with pH values
shown. The arrow represents the mechanical arm.
index 0 1 2 3 4 5 6
pH 7 4 10 5 6 7 13
index 0 1 2 3 4 5 6
pH 7 4 10 5 6 7 13
✐ ✐
✐ ✐
✐ ✐
“ap” — 2014/11/4 — 11:10 — page 43 — #57
✐ ✐
Diagnostic Test
public interface Solution
int getPH()
void setPH(int newValue)
int getCurrentIndex()
boolean isFacingRight()
void changeDirection()
void moveForward(int numLocs)
(b) Write the Experiment method mostAcidic that returns the index of the
most acidic solution and places the mechanical arm facing right at the lo-
cation of the most acidic solution. A solution is acidic if its pH is less than
7. The lower the pH, the more acidic the solution. If there are no acidic
solutions in the experiment, the mostAcidic method should return -1 and
place the mechanical arm at index 0, facing right.
For example, suppose the experiment has this state:
index 0 1 2 3 4 5 6
pH 7 4 10 5 6 7 13
A call to mostAcidic should return the value 1 and result in the following
state for the experiment:
index 0 1 2 3 4 5 6
pH 7 4 10 5 6 7 13
✐ ✐
✐ ✐
✐ ✐
“ap” — 2014/11/4 — 11:10 — page 44 — #58
✐ ✐
44 Practice Exam
index 0 1 2 3 4 5 6
pH 7 9 8 8 12 13 14
a call to mostAcidic should return the value -1 and result in the following
state for the experiment:
index 0 1 2 3 4 5 6
pH 7 9 8 8 12 13 14
int getPH()
void setPH(int newValue)
int getCurrentIndex()
boolean isFacingRight()
void changeDirection()
void moveForward(int numLocs)
END OF EXAMINATION
✐ ✐
✐ ✐