Java 17 Silver ラボ

学習の画面へ

同じクラスの別インスタンスの private

出力はどれか。

public class Main {
    public static void main(String[] args) {
        Wallet a = new Wallet(100);
        Wallet b = new Wallet(50);
        a.takeAll(b);
        System.out.println(a.total() + " " + b.total());
    }
}

class Wallet {
    private int money;

    Wallet(int money) { this.money = money; }

    void takeAll(Wallet other) {
        money += other.money;
        other.money = 0;
    }

    int total() { return money; }
}
  1. A 150 0
  2. B 100 50
  3. C コンパイルエラーになる
  4. D 実行時に例外が発生する
正解と解説を見る

正解: A

ポイント

「アクセス修飾子の使用」をこのサイトで解く (記録・間違えた問題の解き直し・AI教師への質問)