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.


Java
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:


Java
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.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Static Keyword and Class Methods Questions: