Overall Score: 63/67 ≈ 94%

Question 38:

Consider the following two class definitions.

public class Bike

{

private int numOfWheels = 2;

public int getNumOfWheels()

{

return numOfWheels;

}

}

public class EBike extends Bike

{

private int numOfWatts;

public EBike(int watts)

{

numOfWatts = watts;

}

public int getNumOfWatts()

{

return numOfWatts;

}

}

The following code segment occurs in a class other than Bike or EBike.

Bike b = new EBike(250);

System.out.println(b.getNumOfWatts());

System.out.println(b.getNumOfWheels());

Which of the following best explains why the code segment does not compile?

I chose C: The first line of the subclass constructor is not a call to the superclass constructor, but the superclass constructor can be called from the first line of a subclass constructor, but it is not required. When the superclass constructor call is not present, Java automatically provides a call to the superclass’s no-argument constructor.
The correct answer was D: The getNumOfWatts method is not found in the Bike class, because at compile time, the declared type of b determines where the compiler looks for methods during method calls. In this case, b is declared to be of type Bike, but the getNumOfWatts method does not appear in the Bike class.

Question 45:

The following questions refer to the code in the GridWorld case study. A copy of the code is provided below.

Consider the design of a Grasshopper class that extends Bug. When asked to move, a Grasshopper moves to a randomly chosen empty adjacent location that is within the grid. If there is no empty adjacent location that is within the grid, the Grasshopper does not move, but turns 45 degrees to the right without changing its location.

Appendix B — Testable API

info.gridworld.grid.Location class (implements Comparable)

public Location(int r, int c)

constructs a location with given row and column coordinates

public int getRow()

returns the row of this location

public int getCol()

returns the column of this location

public Location getAdjacentLocation(int direction)

returns the adjacent location in the direction that is closest to direction

public int getDirectionToward(Location target)

returns the closest compass direction from this location toward target

public boolean equals(Object other)

returns true if other is a Location with the same row and column as this location; false otherwise

public int hashCode()

returns a hash code for this location

public int compareTo(Object other)

returns a negative integer if this location is less than other, zero if the two locations are equal, or a positive integer if this location is greater than other. Locations are ordered in row-major order. Precondition: other is a Location object.

public String toString()

returns a string with the row and column of this location, in the format (row, col)

Compass directions:

public static final int NORTH = 0;

public static final int EAST = 90;

public static final int SOUTH = 180;

public static final int WEST = 270;

public static final int NORTHEAST = 45;

public static final int SOUTHEAST = 135;

public static final int SOUTHWEST = 225;

public static final int NORTHWEST = 315;

Turn angles:

public static final int LEFT = -90;

public static final int RIGHT = 90;

public static final int HALF_LEFT = -45;

public static final int HALF_RIGHT = 45;

public static final int FULL_CIRCLE = 360;

public static final int HALF_CIRCLE = 180;

public static final int AHEAD = 0;

info.gridworld.grid.Grid interface

int getNumRows()

returns the number of rows, or -1 if this grid is unbounded

int getNumCols()

returns the number of columns, or -1 if this grid is unbounded

boolean isValid(Location loc)

returns true if loc is valid in this grid, false otherwise Precondition: loc is not null

E put(Location loc, E obj)

puts obj at location loc in this grid and returns the object previously at that location (or null if the location was previously unoccupied). Precondition: (1) loc is valid in this grid (2) obj is not null

E remove(Location loc)

removes the object at location loc from this grid and returns the object that was removed (or null if the location is unoccupied) Precondition: loc is valid in this grid

E get(Location loc)

returns the object at location loc (or null if the location is unoccupied) Precondition: loc is valid in this grid

ArrayList getOccupiedLocations()

returns an array list of all occupied locations in this grid

ArrayList getValidAdjacentLocations(Location loc)

returns an array list of the valid locations adjacent to loc in this grid Precondition: loc is valid in this grid

ArrayList getEmptyAdjacentLocations(Location loc)

returns an array list of the valid empty locations adjacent to loc in this grid Precondition: loc is valid in this grid

ArrayList getOccupiedAdjacentLocations(Location loc)

returns an array list of the valid occupied locations adjacent to loc in this grid Precondition: loc is valid in this grid

ArrayList getNeighbors(Location loc)

returns an array list of the objects in the occupied locations adjacent to loc in this grid Precondition: loc is valid in this grid

info.gridworld.actor.Actor class

public Actor()

constructs a blue actor that is facing north

public Color getColor()

returns the color of this actor

public void setColor(Color newColor)

sets the color of this actor to newColor

public int getDirection()

returns the direction of this actor, an angle between 0 and 359 degrees

public void setDirection(int newDirection)

sets the direction of this actor to the angle between 0 and 359 degrees that is equivalent to newDirection

public Grid getGrid()

returns the grid of this actor, or null if this actor is not contained in a grid

public Location getLocation()

returns the location of this actor, or null if this actor is not contained in a grid

public void putSelfInGrid(Grid gr, Location loc)

puts this actor into location loc of grid gr. If there is another actor at loc, it is removed. Precondition: (1) This actor is not contained in a grid (2) loc is valid in gr

public void removeSelfFromGrid()

removes this actor from its grid. Precondition: this actor is contained in a grid

public void moveTo(Location newLocation)

moves this actor to newLocation. If there is another actor at newLocation, it is removed. Precondition: (1) This actor is contained in a grid (2) newLocation is valid in the grid of this actor

public void act()

reverses the direction of this actor. Override this method in subclasses of Actor to define types of actors with different behavior

public String toString()

returns a string with the location, direction, and color of this actor

info.gridworld.actor.Rock class (extends Actor)

public Rock()

constructs a black rock

public Rock(Color rockColor)

constructs a rock with color rockColor

public void act()

overrides the act method in the Actor class to do nothing

info.gridworld.actor.Flower class (extends Actor)

public Flower()

constructs a pink flower

public Flower(Color initialColor)

constructs a flower with color initialColor

public void act()

causes the color of this flower to darken Which method(s) of the Bug class should the Grasshopper class override so that a Grasshopper can behave as described above?

act() move() canMove()

I chose A: I only, but the correct answer was D: II and III only.

Question 53:

Consider the following method.

public int mystery(int num)
{
int x = num;
while (x > 0)
{
if (x / 10 % 2 == 0)
return x;
x = x / 10;
}
return x;
}

What value is returned as a result of the call mystery(1034) ?

I chose B: 10, but the correct answer was D: 103.

Question 65:

Assume that the following variable declarations have been made.

double d = Math.random();

double r;

Which of the following assigns a value to r from the uniform distribution over the range 0.5 ≤ r < 5.5 ?

I chose E: r = d * 5.5;, but the correct answer was D: r = d * 5.0 + 0.5;