• 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
      • View all Units
  • Courses
  • Tutorials
    • Java
    • Servlets
    • Struts
    • Spring
    • Webservice
  • FAQ
  • Testimonials
  • Blog
  • CONTACT US

AP CSA Unit 2.11 MCQ Practice: Nested Iteration

Home » AP CSA MCQ » Unit 2: Selection and Iteration » Nested Iteration

AP CSA Unit 2.11 MCQ Practice: Nested Iteration

📚 Topic 2.11 ⏱️ 10 Min Practice ✅ Current AP CSA Framework

When a loop is placed entirely within the body of another loop, it is called nested iteration. AP CSA Topic 2.11 focuses on tracing nested loops, determining execution counts, and understanding how the inner loop behaves for each iteration of the outer loop.

In this interactive practice set, you will work through 10 multiple-choice questions covering nested loop execution, dependent loop bounds, tracing, and output patterns.

Key Concepts to Remember Before You Start:

  • Total Executions: If the outer loop runs x times and the inner loop runs y times independently for each outer iteration, the inner statement executes x × y times.
  • Resetting the Inner Loop: The inner loop starts again from its initialization whenever the outer loop begins another iteration.
  • Dependent Bounds: If the inner loop's boundary or initialization depends on the outer loop's variable, the number of inner-loop executions can change from one outer iteration to the next.

Test your knowledge with the interactive quiz below. Careful tracing is required—work through the values of both loop variables systematically.

AP CSA 2.11: 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. What pattern is printed?
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 triangular pattern.
3. Consider the following code segment. What value is printed?
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 for each outer-loop value:

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. What value is printed?
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: B When i = 0:

(0 + 1) + (0 + 2) + (0 + 3) = 6.

When i = 1:

(1 + 1) + (1 + 2) + (1 + 3) = 9.

Total = 6 + 9 = 15.
5. Consider the following code segment. What is printed?
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, the inner while loop executes twice.

a = 1: prints "11 " and "12 "
a = 2: prints "21 " and "22 "
a = 3: prints "31 " and "32 "
6. Consider the following code segment. What value is printed?
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 is true only when both variables have the same value:

(0,0), (1,1), (2,2), (3,3), and (4,4).

Therefore, k is incremented 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 starting value of 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. What pattern does it print?
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).

These positions form a diagonal running from the top-right corner to the bottom-left corner of the grid.
9. Consider the following code segment. What value is printed?
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.

i = 0: p = 2, result = 2
i = 1: p = 4, result = 6
i = 2: p = 8, result = 14
i = 3: p = 16, result = 30

Therefore, the program 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 another type of loop in Java.

For example, a while loop can be nested inside a for loop, and a for loop can be nested inside a while loop.

Unit 2.11 Nested Iteration Practice Complete!

0 / 10

Why Nested Iteration Matters for AP CSA

Nested iteration requires you to trace an inner loop while keeping track of the current value of the outer-loop variable. The number of inner-loop executions may remain constant or may change depending on the outer-loop value.

The same tracing skills are useful when working with rows and columns of two-dimensional arrays later in Unit 4: Data Collections. Being comfortable with nested loops will help you follow algorithms that process multiple dimensions of data.

Continue Your AP CSA Practice

After practicing nested iteration, continue with the other Unit 2: Selection and Iteration topics.

Review the Unit 2 Selection & Iteration Practice Assessment for additional mixed practice covering the currently available iteration topics.

← Previous: Topics 2.9–2.10 Loop & String Algorithms
Next: Unit 2 Practice Assessment →

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: Examples, Types & Constructor Overloading
  • 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