A Java monitoring program uses the following class. Assume s refers to an existing Sensor object when the program calls Sensor.inspect(s). Which statements about the method are true? Select all that apply.
class Sensor { int reading = 18; static int checks = 0; static int inspect(Sensor sensor) { checks++; return sensor.reading; } }
The expression sensor.reading is valid because the parameter identifies the object whose field is read.
The expression this.reading would be valid because the method also updates a shared field in the class.
The expression reading alone would be valid because Java automatically treats the supplied parameter as the current object.
The increment of checks changes the shared field, so later calls can observe its updated value.
Unlock full course notes by signing up
Tracing a credit ledger
A Java application manages a shared balance of printing credits using the class below. Assume a fresh execution and no other calls.
The following statements execute in order inside main:
What value does combined hold? Answer in credits, to the nearest whole credit.
Separate calls or combined inputs
A delivery service bills each consignment in 600 g weight units, rounding any partial unit upward. Each consignment also incurs a fixed fee of 95 cents. For the masses used in this question, this Java class implements that pricing rule:
Java integer division discards the fractional part. Two items have masses of 2750 g and 850 g. Assume combining them adds no packaging mass and is permitted by the service.
Find these four quantities as exact integers:
Booking through a shared pool
A recreation centre's Java program manages a shared pool of available admission slots. Assume a fresh execution, sequential calls, and no other code that changes availability.
The following statements execute in order inside main. The second booking requests one third of the value stored in quote; the division is exact in this execution.
Determine the final values of booked, PassPool.available, and finalQuote. Give each value in slots as an exact whole number.
A query during class initialization
A venue application loads this Java class for the first time. Predict its query results using the initialization rules supplied below.
For this problem, use these Java rules:
Before the explicit static field assignments run, all static int fields initially contain 0.