Scope and Access

AP Computer Science A· difficulty 3/5

public class Account {
    private double balance;
    public Account(double b) { balance = b; }
}
// ...
Account a = new Account(100.0);
System.out.println(a.balance);

What is the result?

  • A

    Run-time error

  • B

    100.0

  • C

    0.0

  • D

    Compile-time error: balance has private access

    check_circle

Explanation

The instance variable balance is declared private, so it cannot be accessed from outside the class.

Want 10 more like this — adaptive to your weak spots?

Related questions