Professional Documents
Culture Documents
_____ 2) The following code appears in the main method of another class.
Which of the following best describes what appears on the screen when this code is executed?
(A) The values "Harry Potter", "JK Rowling" and "1997" with each on a new line, and the second two lines
indented.
(B) The text "Book@" followed by a memory address made up of numbers and letters.
(C) The values "Harry Potter", "JK Rowling" and "1997" all on the same line.
(D) The letter "b" only.
(E) An error message.
_____ 3) The year variable for a Book is set to 2001. Suppose the variable b in a separate class points to this
book. Which of the following calls would return the year value of 2001?
(A) b.getYear(2001)
(B) b.year
(C) b.setYear(2001)
(D) b.setYear()
(E) b.getYear()
_____ 4) What is printed when the following code, which appears in the main method of a separate class to
Book, is run?
Book b1 = new Book("JRR Tolkien", "Lord of the Rings", 1954);
Book b2 = new Book(""JRR Tolkien", "Lord of the Rings", 1954);
if (b1 == b2){
System.out.print("equal, ");
}
else{
System.out.print("not equal, ");
}
if (b1.equals(b2)){
System.out.print("equal");
}
else{
System.out.print("not equal");
}
_____ 5) Consider the following class that stores information the temperature.
Consider the following code segments that are potential replacements for /* missing code */.
Which of the above code segments could be used to replace /* missing code */ so that equals
method works as intended?
(A) II only
(B) I and II only
(C) II and IV only
(D) I and IV only
(E) I, II, and IV
6) Create a class named Vehicle that simulates a car moving along a 40 block stretch of road.
Your class will build a vehicle and keep track of its location on the road. Location values may range from -20 to
20. A location value of 0 represents block 0, a location value of 1 represents block 1, a location value of 2
represents block 2, etc. If the user tries to move the vehicle beyond block +20 or -20, set the location to +/- 20
respectively.
Variables
int location - An integer that holds the current block location of the car on the road, with possible
values ranging from -20 to 20.
Methods
Vehicle() - Sets location to 0.
Vehicle(int loc) - If loc is between -20 and 20 inclusive, sets location to loc. Otherwise, sets
location to 0 using this.
void forward() - Increments the vehicle forward one block. Do not let the user move past block
20.
void backward() - Increments the vehicle backward one block. Do not let the user move past block
-20.
int getLocation() - Returns an integer representing the block location of the car.
String toString() - Returns a String representation showing a visual of the vehicle location. The
vehicle is an @ character, with underscores and commas to show its location. When the vehicle is at
location -20 the @ character appears at the start of the String. When the vehicle is at a higher position,
one space for each number from -20 to the car's current location appears before the @. For example:
o If a vehicle is at -13, there should be 7 underscores and commas before the vehicle:
_,_,_,_,_,_,_,@
o If a vehicle is at 5, there should be 25 underscores and commas before the vehicle:
_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,@