APCSA Practice: What Does This Java Sum Calculation Loop Output? 💻

preview_player
Показать описание
📢 What is the output of the following code?

java
Copy
int[] arr = {1, 2, 3, 4};
int sum = 0;
sum += arr[i];
}
Options:
A) 7
B) 10
C) 6
D) 5

📝 Solution Breakdown:
1️⃣ The array arr is initialized with {1, 2, 3, 4}.
2️⃣ The for loop iterates through the array, adding each element to the variable sum.
3️⃣ The sum is calculated as 1 + 2 + 3 + 4 = 10.
🔥 Final Answer: Option B — 10
🔑 Key Takeaway:
The loop computes the sum of the array elements and prints the result.

#Java #ArraySum #ForLoop #JavaCoding #LearnJava #Programming #APCSA
Рекомендации по теме
visit shbcf.ru