Nested Iteration - AP Computer Science A

  1. What is the purpose of nested iteration in array processing?
    To iterate over multi-dimensional arrays. Outer loop accesses rows, inner loop accesses columns of 2D arrays.
  2. What is the time complexity of a nested loop with two loops iterating over nn elements each?
    O(n2)O(n^2). Each outer iteration runs inner loop nn times: n×n=n2n \times n = n^2.
  3. Identify the error in this nested loop: for(int i=0; i < 5; i++) { for(int j=0; j < 5; j++) }
    Missing braces around inner loop body. Loop bodies require braces to contain multiple statements or nested loops.
  4. How can nested loops be used to calculate the sum of elements in a 2D array?
    Sum elements in inner loop, iterate rows in outer loop. Outer loop processes rows, inner loop sums elements within each row.
  5. What will this code output? for(int i=0; i<2; i++) { for(int j=0; j<=i; j++) { System.out.print(j); } }
    001. i=0→j=0, i=1→j=0,1; prints j values for each valid combination.
  6. What is printed by the loop? for(int i=0; i<2; i++) { for(int j=0; j<2; j++) { if(j==1) break; System.out.print(j); } }
    00. Break exits inner loop when j=1, so only j=0 prints each iteration.
  7. What is the output of this code? for(int i=2; i>=0; i--) { for(int j=0; j<=i; j++) { System.out.print(i); } }
    222110. i decreases 2→0; j runs 0 to i, printing i value each time.
  8. Find the output: for(int i=0; i<3; i++) { for(int j=i; j<3; j++) { System.out.print("*"); } }
    ******. i=0→j runs 3 times, i=1→j runs 2 times, i=2→j runs 1 time.
  9. What is the function of the inner loop in this code? for(int i=0; i<3; i++) { for(int j=0; j<2; j++) { System.out.print("*"); } }
    Prints "**" for each iteration of the outer loop. Inner loop prints two stars for each of three outer iterations.
  10. How can nested iteration be used to compare all pairs of elements in an array?
    Use two loops, each iterating over the array. Outer loop selects first element, inner loop compares with remaining elements.
  11. What is the time complexity of three nested loops, each iterating over nn elements?
    O(n3)O(n^3). Three nested loops create cubic complexity: n×n×n=n3n \times n \times n = n^3.
  12. What is the result of these loops? for(int i=2; i>=0; i--) { for(int j=0; j<=i; j++) { System.out.print(j); } }
    0120100. i decreases 2→0; j runs 0 to i, printing j values.
  13. What is the output of this nested loop? for(int i=0; i<2; i++) { for(int j=0; j<2; j++) { System.out.print(i+j); } }
    0011. Outer loop: i=0,1; inner loop: j=0,1; outputs i+j for each combination.
  14. What is a common use case of nested loops in matrix operations?
    Iterating over rows and columns. Outer loop for rows, inner loop for columns in matrix traversal.
  15. Identify the error: for(int i=0; i<2; i++) { for(int j=0; j<2; j++) System.out.print(i j); }
    Missing '+' between 'i' and 'j'. Concatenation operator missing between variables i and j.
  16. Identify the error: for(int i=0; i<2; i++) { for(int j=0; j<2; j++) { System.out.println(i+j); }
    Missing closing brace for loops. Nested loops need closing braces for both inner and outer structures.
  17. How is a nested loop structure typically used in a sorting algorithm?
    To compare and swap elements. Outer loop selects elements, inner loop finds position or comparison.
  18. How many times does the inner loop execute? for(int i=0; i<3; i++) { for(int j=0; j<4; j++) { } }
    12 times. Outer loop runs 3 times, inner loop runs 4 times each: 3×4=123 \times 4 = 12.
  19. What is printed by this loop? for(int i=0; i<1; i++) { for(int j=0; j<3; j++) { System.out.print(j); } }
    012. Outer loop runs once (i=0), inner loop prints j values 0, 1, 2.
  20. How can nested iteration be applied in a search algorithm?
    Check each element against all others. Outer loop examines each element, inner loop searches through collection.
  21. Identify the error: for(int i=0; i<2; i++) { for(int j=0; j<2; j++) { System.out.print(i j); } }
    Missing '+' between 'i' and 'j'. Concatenation operator missing between variables i and j in print statement.
  22. What is the purpose of the outer loop in a nested iteration?
    Controls the number of times the inner loop executes. Outer loop determines how many times the inner loop executes completely.
  23. What is printed by this code? for(int i=1; i<=2; i++) { for(int j=1; j<=i; j++) { System.out.print(i); } }
    122. i=1→j=1 prints 1, i=2→j=1,2 prints 2,2.
  24. What does this loop print? for(int i=0; i<2; i++) { for(int j=0; j<2; j++) { System.out.print(i*j); } }
    0001. i=0,1; j=0,1; outputs i×j: 0×0=0, 0×1=0, 1×0=0, 1×1=1.
  25. What is printed by this loop? for(int i=0; i<1; i++) { for(int j=0; j<3; j++) { System.out.print("*"); } }
    ***. Outer loop runs once, inner loop prints three asterisks.
  26. What does the inner loop control in a nested loop?
    The repeated action for each outer loop iteration. Inner loop defines what happens for each outer loop iteration.
  27. What does this code output? for(int i=0; i<2; i++) { for(int j=0; j<2; j++) { System.out.print(i*j); } }
    0001. i=0,1; j=0,1; outputs i×j: 0×0=0, 0×1=0, 1×0=0, 1×1=1.
  28. What is the result of these nested loops? for(int i=1; i<=2; i++) { for(int j=1; j<=2; j++) { System.out.print(i*j); } }
    1124. i=1,2; j=1,2; outputs products: 1×1=1, 1×2=2, 2×1=2, 2×2=4.
  29. What is printed by this code? for(int i=0; i<3; i++) { for(int j=0; j<i; j++) { System.out.print(i+j); } }
    23. i=1→no output, i=2→j=0,1 giving 2+0=2, 2+1=3.
  30. What is printed by this code? for(int i=1; i<=3; i++) { for(int j=1; j<=i; j++) { System.out.print(j); } }
    112123. Inner loop runs from 1 to i: i=1→j=1, i=2→j=1,2, i=3→j=1,2,3.

Study these flashcards