Bird
0
0

Given a 2D array int[][] data = {{1,2},{3,4},{5,6}}, write the output of this code snippet:

hard📝 Application Q8 of 15
Java - Arrays
Given a 2D array int[][] data = {{1,2},{3,4},{5,6}}, write the output of this code snippet:
int sum = 0;
for(int i = 0; i < data.length; i++) {
  for(int j = 0; j < data[i].length; j++) {
    sum += data[i][j];
  }
}
System.out.println(sum);
A20
B21
C15
D16
Step-by-Step Solution
Solution:
  1. Step 1: Sum all elements in the 2D array

    Elements are 1,2,3,4,5,6. Their sum is 1+2+3+4+5+6 = 21.
  2. Step 2: Confirm loop covers all elements

    Outer loop runs 3 times, inner loop runs 2 times each, covering all elements.
  3. Final Answer:

    21 -> Option B
  4. Quick Check:

    Sum of all elements = 21 [OK]
Quick Trick: Sum all elements by nested loops over rows and columns [OK]
Common Mistakes:
  • Adding only first row
  • Off-by-one in loops
  • Ignoring inner loop length

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes