• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar

JavaTutorOnline

1-on-1 Online Java Training by a Senior Software Engineer

  • Home
  • AP CSA
    • FRQ Practice
      • Arrays
      • ArrayList
      • Strings
      • 2D Arrays
    • MCQ Practice
      • Booleans
      • Iteration
      • Arrays
  • Courses
  • Tutorials
    • Java
    • Servlets
    • Struts
    • Spring
    • Webservice
  • FAQ
  • Testimonials
  • Blog
  • CONTACT US

AP CSA Unit 4.4 Practice: Nested Iteration MCQs & Exam Prep

Home » AP CSA MCQ » Unit 4 » Nested Loops

AP CSA Practice: Nested Iteration

📚 Topic 4.4 ⏱️ 10 Min Practice ✅ Updated for 2027 Exam

When a loop is placed entirely within the body of another loop, it is called nested iteration. Topic 4.4 challenges your ability to mentally track two different loop variables simultaneously. This topic is the gateway to understanding Unit 8 (2D Arrays), so mastering it now is critical.

In this interactive practice set, we cover Topic 4.4: Nested Iteration. These 10 multiple-choice questions will test your ability to calculate total executions, trace matrix-style printed patterns, and manage inner-loop conditions that depend on outer-loop variables.

Key Concepts to Remember Before You Start:

  • Total Executions: If the outer loop runs x times and the inner loop runs y times (independently), the inner statement executes x * y times.
  • Resetting the Inner Loop: The inner loop completes its entire cycle and its variable resets to the initial value for every single iteration of the outer loop.
  • Dependent Bounds: If the inner loop’s boundary relies on the outer loop’s variable (e.g., for (int j = 0; j < i; j++)), the execution count changes dynamically (often forming triangular patterns).

Test your knowledge with the interactive quiz below. Careful tracing is required—grab some scratch paper before you begin!

AP CSA 4.4: Nested Iteration

Question 1 of 10 | Score: 0
1. Consider the following code segment. How many times will the string "*" be printed?
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 4; j++) {
        System.out.print(“*”);
    }
}
Correct Answer: B The outer loop runs 3 times (i = 0, 1, 2). For each outer iteration, the inner loop runs 4 times (j = 0, 1, 2, 3). Total executions = 3 * 4 = 12.
2. Consider the following code segment:
for (int r = 1; r <= 3; r++) {
    for (int c = 1; c <= r; c++) {
        System.out.print(c + ” “);
    }
    System.out.println();
}
Correct Answer: A When r=1, the inner loop prints ‘1 ‘. When r=2, it prints ‘1 2 ‘. When r=3, it prints ‘1 2 3 ‘. The println() at the end of the outer loop moves the output to a new line, creating a triangle.
3. Consider the following code segment:
int count = 0;
for (int x = 1; x <= 5; x++) {
    for (int y = 5; y > x; y–) {
        count++;
    }
}
System.out.println(count);
Correct Answer: A Trace the inner loop executions per outer loop:
x=1: y goes 5, 4, 3, 2 (4 times)
x=2: y goes 5, 4, 3 (3 times)
x=3: y goes 5, 4 (2 times)
x=4: y goes 5 (1 time)
x=5: y > 5 is false (0 times)
Total: 4 + 3 + 2 + 1 = 10.
4. Consider the following code segment:
int total = 0;
for (int i = 0; i < 2; i++) {
    for (int j = 1; j < 4; j++) {
        total += (i + j);
    }
}
System.out.println(total);
Correct Answer: C When i=0: add (0+1) + (0+2) + (0+3) = 6.
When i=1: add (1+1) + (1+2) + (1+3) = 12.
Total = 6 + 12 = 18.
5. Consider the following code segment:
for (int a = 1; a <= 3; a++) {
    int b = 1;
    while (b <= 2) {
        System.out.print(a + “” + b + ” “);
        b++;
    }
}
Correct Answer: B For each outer iteration `a`, the inner while loop executes twice (b=1, then b=2).
a=1: prints “11 “, “12 “
a=2: prints “21 “, “22 “
a=3: prints “31 “, “32 ”
6. Consider the following code segment:
int k = 0;
for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
        if (i == j) {
            k++;
        }
    }
}
System.out.println(k);
Correct Answer: A The inner loop executes a total of 25 times. However, the condition i == j only evaluates to true when both variables are equal: (0,0), (1,1), (2,2), (3,3), and (4,4). This happens 5 times.
7. Consider the following code segment. What value is printed?
int sum = 0;
for (int i = 0; i < 3; i++) {
    for (int j = i; j < 3; j++) {
        sum++;
    }
}
System.out.println(sum);
Correct Answer: B The inner loop’s start variable `j` depends on `i`.
i=0: j goes 0, 1, 2 (3 iterations)
i=1: j goes 1, 2 (2 iterations)
i=2: j goes 2 (1 iteration)
Total iterations = 3 + 2 + 1 = 6.
8. Consider the following code segment:
for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
        if (i + j == 4) {
            System.out.print(“X”);
        } else {
            System.out.print(“O”);
        }
    }
    System.out.println();
}
Correct Answer: B The condition `i + j == 4` is true for (0,4), (1,3), (2,2), (3,1), and (4,0). This forms a diagonal line running from the top-right corner to the bottom-left corner of the grid, which is known as the minor diagonal.
9. Consider the following code segment:
int n = 4;
int result = 0;
for (int i = 0; i < n; i++) {
    int p = 1;
    for (int j = 0; j <= i; j++) {
        p *= 2;
    }
    result += p;
}
System.out.println(result);
Correct Answer: B The inner loop calculates powers of 2 (2^(i+1)).
i=0: j goes 0. p = 2. result = 2.
i=1: j goes 0, 1. p = 4. result = 2 + 4 = 6.
i=2: j goes 0, 1, 2. p = 8. result = 6 + 8 = 14.
i=3: j goes 0, 1, 2, 3. p = 16. result = 14 + 16 = 30.
Prints 30.
10. Which of the following statements about nested loops in Java is FALSE?
Correct Answer: B Any type of loop can be nested inside any other type of loop in Java. You can freely nest `while` inside `for`, `for` inside `while`, or `while` inside `while`.

Unit 4.4 Nested Iteration Complete!

0 / 10

Why Nested Loops Bridge Unit 4 and Unit 8

Nested iteration is one of the most conceptually challenging parts of Unit 4. You must be able to hold the state of the outer loop steady in your mind while the inner loop rapidly cycles through all its values. This exact same mental mechanism is required in Unit 8: 2D Arrays to process grids of data.

Ready for the Ultimate Challenge?

You have completed all the core iteration practice modules! It is time to test your overall mastery of Unit 4 with a comprehensive exam.

Continue your AP CSA review by proceeding to the Unit 4 Mock Exam, featuring 15 timed, mixed-concept questions spanning while loops, for loops, algorithms, and nested iteration.

← Previous: Topic 4.3 Loop Algorithms
Next: Unit 4 Mock Exam →

Primary Sidebar

Mr Chinmay

Chinmay Patel
Book a Demo Class

Phone & Whatsapp +919853166385
javatution@gmail.com

Recent Posts

  • Learn Java in One Day: 10-Hour 1-on-1 Crash Course | JavaTutorOnline
  • Constructor in Java and Overloaded Constructor Example Program
  • Important Interview Questions on Java Multithreading
  • React Spring Boot Web Services React Integration
  • Spring Boot RESTful Web Services Example
  • Top Spring MVC Interview Questions and Answers for Developers
  • Top Spring Core Interview Questions and Answers for Developers
  • Host Java Web Apps for Free on Mobile with Tomcat and Termux
  • How to Deploy Java Web Application on Aws EC2 with Elastic IP
  • Simple Jsp Servlet Jdbc User Registration using Tomcat Mysql and Eclipse

Additional Resources

  • AP Computer Science A Summer Prep
Copyright © 2026 JavaTutorOnline