Analyzing Integer Overflow in Java
Consider the following method, which is intended to calculate compound interest.
public static int calculateCompoundInterest(int principal, int rate, int years) {
int amount = principal;
for (int i = 0; i < years; i++) {
amount = amount + (amount * rate / 100);
}
return amount - principal;
}
Which of the following input scenarios is most likely to cause an integer overflow, leading to an incorrect result? An overflow can occur during any intermediate calculation within the loop.
A
principal = 100, rate = 5, years = 10
B
principal = 50000, rate = 8, years = 15
C
principal = 10000, rate = 3, years = 20
D
principal = 100000000, rate = 25, years = 15
Question Leaderboard
Not enough data yet to show leaderboard.
