2012 AP Java Quiz 3-2
Part 1: GridWorld Case
Study - Method Identification
1. For each of the method described below, provide the complete name of the method
to include return type and any parameters.
a. _______________________________________________________________
Returns the object at location loc (or null if the location is unoccupied.
E get(Location loc)
b. _______________________________________________________________
Returns an array list of all occupied locations in this grid
ArrayList<Location> getOccupiedLocations()
c. _______________________________________________________________
Returns the grid of an actor, or null if the actor is not contained in a grid
Grid<Actor> getGrid()
d. _______________________________________________________________
Returns an array list of objects in the occupied locations adjacent to loc in the grid
ArrayList<E> getNeighbors(Location loc)
e. _______________________________________________________________
Returns the closest compass direction from this location toward target
int getDirectionToward(Location target)
2. ______________________________________ is the only abstract class in GridWorld
AbstractGrid
3. ______________________________________ is the only Interface in Gridworld
Grid
4. The ________________________________ method compared the characters inside a
String object. equals
5.
The ___________________operator
compares two object references to see whether they
refer to the same instance (same location in memory). ==
6. An instance variable is always called for a particular object. This variable is an implicit
parameter for the method and is referred to with the __________keyword. this
7.
Casting a superclass to
a subclass type is called a _________________downcast
Part 2: GridWorld Case
Study - Fill In The Blanks and Short Code
1. The
Bug, Flower, and Rock classes extend the
_________________ class in
different
ways. Their behavior is
specified by how they override the _____________
method.
Actor Act
2.
The _____________________instance variable defines the number of
steps a
BoxBug moves
on each side of its box.
sideLength
3.
Assume that greenBug inherits from Bug and that, in that class,
gr has been
declared to be a reference to
a Grid object. Write a line of code that will obtain a
count of the number
of objects in the grid and assign it to an integer variable named
theCount.
int theCount =
gr.getOccupiedLocations().size()
4.
List 3 properties that every Actor has
a.
______________________A
Color
b. _____________________
A Direction
c.
______________________A
reference to its grid
5.
Composition is a "________________________" relationship
Has A
6.
Inheritance is a " _________________________" relationship
Is A
7. If a bug needs to turn 180 degrees, how many times should it call the turn method
_____________ times.
Four. Each turn is a 45 degree turn.
8. The __________________method of the Comparable class compares one object
with another object and returns a negative integer, zero, or a positive integer
depending on whether one object is less than, equal to, or greater than another
object. compareTo
9. The ______________ keyword is a reference to the current object. this
10. Assume that you have established and populated an ArrayList of strings named
theList. Using an enhanced for loop, write code that will print out the elements of the
ArrayList.
for(String counter :
theList)
{
System.out.print(counter);
}
Part 3: GridWorld Case
Study Multiple Choice: Circle Correct Answer
1. A
FastCritter is to behave exactly like a Critter, except that it can move up
to two grid
locations in each direction (assuming that it is
not blocked by another actor). To
achieve this behavior, which method should be
overridden?
A. getActors
B. processActors
C. getMoveLocations
Answer
D. selectMoveLocation
E. makeMove
2.
Consider the following code segment.
Grid<Actor> grid = new BoundedGrid<Actor>(10, 10);
Bug bug1 = new Bug();
bug1.setDirection(Location.EAST);
bug1.putSelfInGrid(grid, new Location(3, 5));
Bug bug2 = new Bug();
bug2.setDirection(Location.SOUTH);
bug2.putSelfInGrid(grid, new Location(2, 6));
bug1.act();
bug2.act();
What is the result of executing the code segment?
A. bug1 and bug2 will both occupy location (3, 6).
B. bug1 will move into location (3, 6) and bug2
will turn.
Answer
C. bug1 will move into location (3, 6) and bug2
will do nothing.
D. bug1 will move into location (3, 6) and bug2
will throw an exception.
E. bug1 will move into location (3, 6) but will be
removed when bug2 moves into the same
location, (3, 6). 3
3.
Consider the following code segment.
Grid<Actor> grid = new BoundedGrid<Actor>(10, 10);
Bug ladybug = new Bug();
ladybug.putSelfInGrid(grid, new Location(2, 8));
ladybug.setDirection(Location.WEST);
Under which of the following circumstances can
ladybug advance to location (2,7) ?
I. The location (2,7) contains another Bug.
II. The location (2,7) contains a Flower.
III. The location (2,7) contains a Rock.
A.
I only
B. II only Answer
C. III only
D. I and II only
E. I, II, and III
4. What would be the effect of having a subclass of Actor called SubActor that does not
override the act method?
I. A subActor object would always be blue.
II. A SubActor object would always face north.
III. A SubActor object would remain in its original location when it acts.
A. I only
B. II only
C.
III only
Answer
D. I and III only
E. I, II, and III
5. Suppose the Actor class is modified to add a Color parameter to its constructor. If
the Critter class is not changed, what will happen when the modified constructor is
called to create a Critter in a client class?
A. A compile-time error
will occur
Answer
B. An exception will be thrown as soon as a color is selected.
C. A blue Critter will be created.
D. A Critter will be created with the same color as the Actor
E. A dialog box will appear, allowing a Critter of any color to be created.
6. Which is a good reason for using the Location class constants for the compass
directions and commonly used turn angles?
I. They enhance readability of code.
II. There is no built-in Degree type in Java
III. They distinguish locations from directions
A. I only
Answer
B. II only
C. III only
D. I and III only
E. I, II, and III
Part 4: Items Not Graded
on Last Quiz - Fill in the Blanks and Code
1. Provide the order of the following algorithms
a. recursive merge sort algorithm_________________ O(NlogN)
b. sequential search _____________O(N) c, binary search ______________ O(logN)
2. The type of sort you would use if the data was already sorted except for the last item
being added is ______________________________ Insertion
3. Write a code segment that will declare a one-dimensional array object named
theMass that stores values that do not contain decimal places. Do not allocate
memory.
int theMass[ ];
4. Write a code segment that will declare and construct a one-dimensional array object
named temperatures that stores 25 values that contain decimal places.
float temperatures [ ] = new float
[25]; //can also use double
5. Write a code segment that will pass the array named temperatures to a method
named populate.
populate(temperatures);
6. Write a code segment that will declare a reference variable named myListOne for an
ArrayList such that it contains references to objects of type string.
ArrayList <String>
myListOne;
7. Write a code segment that will declare a reference variable named myListTwo and
construct an ArrayList such that it contains references to objects of type double with
an initial capacity of 20.
ArrayList <Double>
myListTwo = new ArrayList <Double> (20);
8. Write a code segment that will print the element of myListTwo above whose index is
5.
System.out.println(myListTwo.get(5));
9. Write a code segment that will add the names Jonathan and Moses to myListOne.
myListOne.add(“Jonathan”);
myListOne.add(“Moses”);
10. Write a code segment that will remove the name Jonathan from myListOne.
myListOne.remove(0);
11. Assume that code has been written so that myListOne now contains 10 names. Write
code that will add the name Abrazot so that it is the 5th element (element whose
index is 4).
myLIstOne.set(4, “Abrazot”);
12. Use an Iterator object to print all the names currently in myListOne. Write a code
segment for this problem.
Iterator
iter = myListOne.iterator();
while
(iter.hasNext())
{
System.out.println(iter.next());
}
13. Write code that will create a two-D array of integers named theArray and populate it
with elements in the rows and columns as indicated below. Do this using an initializer
list.
1 1
2 2
a. Do this using an initializer list.
int theArray[][] =
{{1,1},{2,2}};
b. Do this using a for loop.
for (row = 0; row<2; row++)
{
for (col = 0; col<2; col++)
{
theArray[row][col] = row+1;
}
}
14. Write a line of code that will create a square two-D array of 100 doubles named
anotherArray. Do not initialize the array. Use the new keyword.
double anotherArray[ ][ ] = new
double[10][10];
15. Write a line of code that will print the number of columns in the second row of an
array named countThings
System.out.println(countThings[1].length);