Consider the following Java class, written for a bookstore inventory program.
public class Book { private String title; private double price; public Book(String title, double price) { title = title; this.price = price; } public String getTitle() { return title; } public double getPrice() { return price; } }
What is printed when the following code runs?
Book b = new Book("Dune", 18.99); System.out.println(b.getTitle() + " " + b.getPrice());
Nothing, because the constructor does not compile.
Dune 18.99
Dune 0.0
null 18.99
null 0.0
Unlock full course notes by signing up
Tracing two step counters
A fitness app uses the following Java class to track daily steps against a goal.
The app then runs these statements in order.
After the last line runs, find the value stored in x, the value stored in y, and the value of the steps field in the object that raj refers to. Give each as an integer.
Track a shared seat pool
A theatre uses the following Java class to track a pool of 24 available seats and the seats reserved by each booking object. The statements run once in a fresh program, in the order shown.
Complete the table with the value of each field at each checkpoint. Enter exact whole numbers of seats. Assume no other code changes these fields.
A local variable that shadows a field
A scorekeeping program uses the following Java class.
The program runs the statements below. What value does the final statement print? Give your answer as an integer.
Initialize individual targets from a changing default
A greenhouse program uses a shared default temperature when creating individual heater settings. All temperatures are in °C. The following Java statements run once in a fresh program, in the order shown, with no other assignments.
Determine the final values of first.target, second.target, third.target, and HeaterSetting.defaultTarget. Give every temperature in °C, to zero decimal places.