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);
β
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.