Note: GridWorld is no longer featured on the AP CS A Exam (since the 2015 Exam).
The Location
class is an important part of the GridWorld Case Study. This practice test is intended to check your understanding of how to use the Location
class to accomplish common tasks. You should have a copy of the Quick Reference available as you take this practice test.
For each question, you must use constants and existing methods where provided. You will not receive full credit for using a numeric value where an equivalent constant exists. You will not receive full credit for writing code that duplicates the functionality of an existing method.
Question 1
Consider a 5 x 7 bounded grid.
Location loc1 = /* construct */;
Give code to replace /* construct */
such that loc1
refers to a Location
object representing row 4 column 0.
Question 2
Location loc2 = loc1.getAdjacentLocation(/* parameter */);
Assume that your answer to the previous question was correct. What code should replace /* parameter */
if loc2
is to refer to the location 1 row below loc1
?
Question 3
Assume that your answer to the previous question was correct. What will be the result of running the completed statement?
(A) loc2
will refer to a Location
object representing (4, 1)
.
(B) loc2
will refer to a Location
object representing (5, 0)
.
(C) loc2
will refer to a Location
object representing (1, 4)
.
(D) loc2
will refer to a Location
object representing (0, 5)
.
(E) loc2
will refer to null
since the location 1 row below loc1
is not valid in the grid.
(F) An exception will be thrown since the location 1 row below loc1
is not valid in the grid.
Question 4
Location loc3 = /* construct a Location object representing row 2 column 3 */;
Location loc4 = /* construct a Location object representing row 4 column 3 */;
/* type */ dir1 = loc3.getDirectionToward(loc4);
Which of the following should replace /* type */
?
(A) int
(B) double
(C) Location
(D) Direction
(E) String
Question 5
Give code to print "pirates"
if and only if dir1
is equal to Location.SOUTH
.
Question 6
Give code to print "treasure"
if and only if loc5
refers to a Location
object representing row 3 column 3.
Question 7
Location loc5 = /* construct */;
Give code to replace /* construct */
such that loc5
refers to a Location
object representing a random valid location in a grid with r
rows and c
columns. loc5
must have an equal probability of referring to any valid location in the grid. Remember that row and column numbering each start at 0 and end at r-1
and c-1
, respectively.
Question 8
A treasure hunter is searching for the treasure located at row 3 column 3 in a grid. She starts at the top left corner of the grid and visits each location in row 0 from left to right. She then moves to the left most location in row 1 and visits each location in row 1 from left to right. She continues to visit the locations in the remaining rows in the same manner.
You are observing the treasure hunt and you can communicate with the treasure hunter. You can tell her whether she has not yet reached the treasure ("Keep going!"
), whether she is at the treasure ("You found it!"
), or whether she has passed the treasure ("Turn around!"
). Give code to replace /* check hunter's position and print appropriate message */
in the following loop to help the treasure hunter find the treasure. You do NOT have to give code to replace the other commented parts.
Location hunterLoc = /* construct */;
while(/* check if hunter at end of grid */)
{
/* check hunter's position and print appropriate message */
/* update hunter's location */
}
See the Location class practice test solutions and explanations or review them with AP CS Tutor Brandon Horn.