A Java application manages a shared balance of printing credits using the class below. Assume a fresh execution and no other calls.
class CreditLedger { static int credits = 24; static int spend(int amount) { credits -= amount; return credits; } static void add(int amount) { credits += amount; } }
The following statements execute in order inside main:
int afterFirst = CreditLedger.spend(5); CreditLedger.add(8); int afterSecond = CreditLedger.spend(6); int combined = afterFirst + afterSecond;
What value does combined hold? Answer in credits, to the nearest whole credit.
Unlock full course notes by signing up
Object access inside a static method
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.
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.