Are you taking CLEP classes? Try clep.ai
| preferred AP College board partner for AP classes
AP Computer Science A/Unit 2: Using Objects
Start Practice TestPractice Test
About Exam
easy Solved by 2 students

For Loop Summation Error

Mark
< Prev
Next >

The following code segment is intended to compute the sum of the integers from 1 to 10. However, it fails to produce the correct result due to a logical error. Which statement best explains this error?

public class SumError {
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 1; i < 10; i++) {
            sum += i;
        }
        System.out.println("Sum: " + sum);
    }
}
A

The variable ‘sum’ should be initialized to 1 instead of 0.

B

The loop’s termination condition ‘i < 10’ mistakenly excludes the number 10, resulting in an incorrect sum.

C

The print statement is incorrectly placed, causing the sum to be printed multiple times.

D

The loop should be a while loop instead of a for loop to correctly accumulate the sum.

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer
Report Question

Question Leaderboard

Not enough data yet to show leaderboard.

AI Tutor

How can I help?

APFIVE © 2020.
Email: apfive@apfive.org|Privacy Policy|