What will be the output of the following Java program? public class SumOfArray {
public static void main(String[ ] args) {
int [ ] arr = new int [ ] {1, 2, 3, 4};
int sum = 0;
for (int i = 0; i < arr.length; i++)
{
sum = sum + arr[i];
}
System.out.println("Sum of all the elements of an arry: " + sum);
}
}
The provided Java program calculates the sum of all integer elements within an array. Let's break down the code to understand how it achieves this.
public class SumOfArray{
public static void main(String[ ] args) {
int [ ] arr = new int [ ] {1, 2, 3, 4};
int sum = 0;
for (int i = 0; i < arr.length; i++)
{
sum = sum + arr[i];
}
System.out.println("Sum of all the elements of an arry: " + sum);
}
}
The program starts by defining a class named SumOfArray with a standard main method, which is the entry point for Java applications.
arr is declared and initialized with the values {1, 2, 3, 4}. This array contains four elements.sum is declared and initialized to 0. This variable will store the cumulative sum of the array elements.for loop is used to iterate through each element of the array.
i initialized to 0, which is the index of the first element.i is less than arr.length (which is 4). This ensures the loop visits indices 0, 1, 2, and 3.arr[i] is added to the sum variable (sum = sum + arr[i];).i is incremented by 1 after each iteration (i++).System.out.println("Sum of all the elements of an arry: " + sum);. This statement prints a string followed by the final value stored in the sum variable to the console. Note the minor typo "arry" in the string literal, but this doesn't affect the calculation.
Let's trace the execution of the loop and see how the sum variable changes:
| Iteration | i |
arr[i] |
sum before addition |
sum = sum + arr[i] |
sum after addition |
|---|---|---|---|---|---|
| 1 | 0 | arr[0] = 1 |
0 | 0 + 1 | 1 |
| 2 | 1 | arr[1] = 2 |
1 | 1 + 2 | 3 |
| 3 | 2 | arr[2] = 3 |
3 | 3 + 3 | 6 |
| 4 | 3 | arr[3] = 4 |
6 | 6 + 4 | 10 |
The loop continues as long as i < 4. When i becomes 4, the condition i < arr.length (4 < 4) is false, and the loop terminates.
After the loop finishes, the final value of the sum variable is 10. The program then prints the string "Sum of all the elements of an arry: " concatenated with the value of sum.
Therefore, the complete output printed to the console will be:
Sum of all the elements of an arry: 10
Comparing this with the options, we look for the string "Sum of all the elements of an array: " followed by the number 10. Option 2 matches this value, correcting the typo in the source code's literal string. The case of "Sum" also matches. Option 3 has a different sum (12) and lowercase "sum". Option 1 has a different sum (9). Option 4 is incorrect as the program compiles and runs without errors. The typo "arry" in the print statement is just a string literal issue, not a compilation error.
The Java program successfully iterates through the array {1, 2, 3, 4} and adds each element to the sum variable, which starts at 0. The sum accumulates as 1, then 1+2=3, then 3+3=6, and finally 6+4=10. The program then prints a string followed by this final sum. The output is "Sum of all the elements of an arry: 10", which corresponds to option 2 (allowing for the 'arry'/'array' variation and case matching).
| Code Section | Purpose |
|---|---|
int [ ] arr = new int [ ] {1, 2, 3, 4}; |
Declares and initializes an integer array. |
int sum = 0; |
Initializes a variable to hold the sum, starting at zero. |
for (int i = 0; i < arr.length; i++) |
Loop to iterate through the array elements from index 0 up to (but not including) the length of the array. |
sum = sum + arr[i]; |
Adds the current array element to the sum variable in each iteration. |
System.out.println("Sum of all the elements of an arry: " + sum); |
Prints the final sum to the console with a descriptive message. |
Understanding arrays and loops is fundamental in Java programming.
dataType[] arrayName; and initialized either explicitly like new dataType[] {value1, value2, ...}; or by size like new dataType[size];.for loop is commonly used to iterate a specific number of times, making it ideal for processing arrays where the number of iterations (equal to the array length) is known beforehand. The structure is for (initialization; condition; increment/decrement) { // loop body }.arr[0] accesses the first element.When an applet begins, which of the following sequence methods is CORRECT?
1) start()
2) paint()
3) init()
What will be the output of the following Java program?
class test
{public static void main (String args [ ])
{
System.out.println(20 + 30 + "Java");
System.out.println("Java" + 20 + 30);
}
}
In Java, the 'PreparedStatement' interface is a sub interface which is used to execute __________ query.
Which of the following is NOT a java primitive type?
Which of the following is/are the rules to declare variables in Java?