A Playlist class describes how playlists work. The variable roadTrip refers to one particular playlist. Which is the class, and which is an object?
Playlist is the class. roadTrip refers to an object, an instance of Playlist
What two features distinguish a constructor header from an ordinary method header?
A constructor has the same name as the class and has no return type. Not even void.
A Robot object named rover has a public instance method named wave() with no parameters. Write the call
rover.wave();
What data type does Math.random() return, and what is its range?
A double greater than or equal to 0.0 and less than 1.0.
What is printed?
String word = "Comet";
System.out.println(word.length());
5
A SmartLamp has a brightness level and can dim. Identify one attribute and one behavior
Brightness is an attribute. Dimming is a behavior or method
Identify the parameter and the argument.
public static void announce(String message)
announce("Welcome");
message is the parameter. "Welcome" is the argument
Which calls compile? Give every valid letter. Assume the calls appear in the defining class.
public static double total(int count, double price)
A. total(3, 2.5)
B. total(3.0, 2.5)
C. total(3, 2)
D. total(3)
and C. An int can be passed to a double parameter. B supplies a double where an int is required. D is missing an argument
What is printed?
public static int increase(int value) {
value += 4;
return value;
}
// Inside main:
int points = 6;
int bonus = increase(points);
System.out.println(points + ":" + bonus);
6:10
What is printed?
String word = "Adventure";
System.out.println(word.substring(2, 5));
ven
How many objects are created? How many reference variables are declared?
Gadget first = new Gadget();
Gadget second = first;
Gadget third = new Gadget();
Two objects and three reference variables.
Which constructor calls are valid?
public Ticket(String event)
public Ticket(String event, int row)
A. new Ticket("Concert")
B. new Ticket("Concert", 4)
C. new Ticket(4, "Concert")
D. new Ticket()
A and B. C reverses the argument types. D has no matching no-argument constructor.
What is my cats name?
Maya
What are the smallest and largest possible values of roll?
int roll = (int) (Math.random() * 8) + 2;
Smallest: 2. Largest: 9
What is printed?
String label = "Mars";
label.toUpperCase();
System.out.println(label);
System.out.println("Score " + 4 + 2);
Mars
Score 42
Does this code compile? Explain why or why not. Assume it is inside main and Timer has an instance method named start().
Timer clock;
clock.start();
No. The local variable clock has not been initialized. Declaring a reference variable does not create an object.
Explain why the last statement fails, then write a valid call.
public static void ring() {
System.out.println("Ding");
}
// Inside main in the same class:
int sound = ring();
ring() returns void, so it does not provide a value to store in sound.
ring();
What is printed? These instance methods belong to the same class, and bell refers to a valid object of that class.
public void tap() {
System.out.print("tap ");
}
public void chime() {
tap();
System.out.print("ding ");
}
bell.chime();
bell.tap();
tap ding tap
Write one statement that stores a random integer from 20 through 30, inclusive, in an int named pick.
int pick = (int) (Math.random() * 11) + 20;
Explain the errors and correct the code to print the last three characters of planet in lowercase.
String planet = "NEPTUNE";
String ending = planet.substring(4, 8);
ending.toLowerCase();
System.out.println(ending);
The ending index 8 exceeds the string’s length of 7. Also, the result of toLowerCase() must be saved or used.
String planet = "NEPTUNE";
String ending = planet.substring(4).toLowerCase();
System.out.println(ending);
What do a and b refer to after these statements? Can b.start() run? Assume Timer has a public no-argument constructor and an instance method named start().
Timer a = new Timer();
Timer b = a;
a = null;
b.start();
a is null. b still refers to the Timer object, so b.start() can run. Changing a does not change b.
What is printed?
class Signal {
public Signal() {
System.out.print("Ready ");
}
public Signal(String label) {
System.out.print(label + " ");
}
}
// Inside main:
Signal x = new Signal("Go");
Signal y = new Signal();
Signal z = x;
Go Ready followed by a space. Assigning x to z does not call another constructor
What is printed? Assume c is an initialized object whose count starts at 4.
public void add(int amount) {
count += amount;
}
public int read() {
return count;
}
c.add(3);
c.add(c.read());
System.out.println(c.read());
14
This code is intended to print an integer from -3 through 3. What does it actually print, and how do you fix it?
int value = (int) Math.random() * 7 - 3;
System.out.println(value);
It always prints -3. Casting Math.random() to int before multiplying always produces 0
int value = (int) (Math.random() * 7) - 3;
System.out.println(value);
Write code that finds the colon and prints only the text after it in uppercase. Do not hard-code the colon’s position.
String entry = "room:library";
int colon = entry.indexOf(":");
String place = entry.substring(colon + 1);
System.out.println(place.toUpperCase());