Q. What is the output of the following code?

Code:
int[] nums = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
}
System.out.println(sum);
  • (A) 1
  • (B) 3
  • (C) 6
  • (D) 15
πŸ’¬ Discuss
βœ… Correct Answer: (D) 15
Explanation: This for loop iterates through the elements of the nums array, using the index variable i, and adds each element to the variable sum. The System.out.println statement then prints out the final value of sum, which is 15.
Explanation by: Pushkar
This for loop iterates through the elements of the nums array, using the index variable i, and adds each element to the variable sum. The System.out.println statement then prints out the final value of sum, which is 15.

πŸ’¬ Discussion


πŸ“Š Question Analytics

πŸ‘οΈ
307
Total Visits
πŸ“½οΈ
3 y ago
Published
πŸŽ–οΈ
Pushkar
Publisher
πŸ“ˆ
85%
Success Rate