QUESTION IMAGE
Question
consider the following program code: var sum = 0; for(var i = 0; i < 5; i++) { sum += i; } println(sum); what is the result of running this program code? 0 is printed 15 is printed 10 is printed the program results in an infinite loop
Step1: Analyze the for loop
The for loop runs when \(i = 0, 1, 2, 3, 4\) (since \(i<5\)).
Step2: Calculate the sum
Initially, \(sum = 0\).
- When \(i = 0\), \(sum = 0 + 0 = 0\)
- When \(i = 1\), \(sum = 0 + 1 = 1\)
- When \(i = 2\), \(sum = 1 + 2 = 3\)
- When \(i = 3\), \(sum = 3 + 3 = 6\)
- When \(i = 4\), \(sum = 6 + 4 = 10\)
Snap & solve any problem in the app
Get step-by-step solutions on Sovi AI
Photo-based solutions with guided steps
Explore more problems and detailed explanations
10 is printed