クラスとメソッド計算問題

次のコードが実行されたとき、最終的に出力される値は何ですか? class Account { static double interest = 0.05; double balance; Account(double b) { this.balance = b; } void applyInterest() { balance = balance + (balance * interest); } } public class Main { public static void main(String[] args) { Account acc = new Account(1000); acc.applyInterest(); System.out.println((int)acc.balance); } }

A.1000
✗ balance は初期値の1000ではなく、applyInterest()で利息が加算されます。
B.1050← 正解
✓ 正解です。1000 + (1000 × 0.05) = 1000 + 50 = 1050 となります。
C.1005
✗ balance × interest = 50 と計算されるため、1000 + 50 = 1050 となり、1005ではありません。
D.950
✗ 利息は減算ではなく加算されるため、balance は減りません。

Oracle Certified Java Programmer, Bronze SE の問題一覧