filmov
tv
What Does This Java Array Sum Loop Output? 💻

Показать описание
📢 What is the output of the following code?
java
Copy
int[] arr = {3, 6, 9, 12};
int sum = 0;
sum += arr[i];
}
Options:
A) 30
B) 40
C) 25
D) 20
📝 Solution Breakdown:
1️⃣ The array arr is initialized with {3, 6, 9, 12}.
2️⃣ The for loop iterates through the array, adding each element to the variable sum.
3️⃣ The total sum is 3 + 6 + 9 + 12 = 30.
🔥 Final Answer: Option A — 30
🔑 Key Takeaway:
The loop sums the elements of the array and prints the result.
#Java #ArraySum #ForLoop #JavaCoding #LearnJava #Programming #APCSA
java
Copy
int[] arr = {3, 6, 9, 12};
int sum = 0;
sum += arr[i];
}
Options:
A) 30
B) 40
C) 25
D) 20
📝 Solution Breakdown:
1️⃣ The array arr is initialized with {3, 6, 9, 12}.
2️⃣ The for loop iterates through the array, adding each element to the variable sum.
3️⃣ The total sum is 3 + 6 + 9 + 12 = 30.
🔥 Final Answer: Option A — 30
🔑 Key Takeaway:
The loop sums the elements of the array and prints the result.
#Java #ArraySum #ForLoop #JavaCoding #LearnJava #Programming #APCSA