Java For Loop Code Tracing
What is printed when the following code segment is executed?
double principal = 1000.0;
double rate = 0.05;
int years = 3;
double amount = principal;
for (int i = 0; i < years; i++) {
amount = amount * (1 + rate);
}
System.out.println((int) amount);
A
1157
B
1150
C
1158
D
1200
Question Leaderboard
| Rank | |||||
|---|---|---|---|---|---|
| #1 | hitrishabhatia | 1 | 1 | 0m 00s | 100 |
| #2 | mertdeg | 1 | 1 | 0m 00s | 100 |
| #3 | sgarv2513 | 1 | 2 | 0m 00s | 90 |
| #4 | midnightasdark | 1 | 1 | 1m 11s | 29 |
| #5 | suhanakochhar006 | 1 | 1 | 2m 15s | -35 |
| #6 | gtsak31 | 1 | 1 | 5m 01s | -201 |
| #7 | y.seong2027 | 0 | 2 | 4m 07s | -267 |
| #8 | kazvin.tjakradinata | 1 | 4 | 6m 19s | -309 |
| #9 | Pelisx | 1 | 1 | 30m 48s | -1,748 |
Items per page:
10
1 – 9 of 9
APFIVE