• 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 Practice Assessment: Iteration & Algorithms

Home » AP CSA MCQ » Unit 2: Selection and Iteration » Practice Assessment

AP CSA Unit 2 Practice Assessment: Iteration & Algorithms

📚 15 Questions ⏱️ 15–20 Min Practice ✅ Topics 2.7–2.11

This 15-question AP CSA practice assessment covers the currently available iteration topics in this collection: Topics 2.7–2.11.

The questions include while loops, for loops, loop tracing, string algorithms, nested iteration, and common loop-control patterns.

Important: This is a focused practice assessment for the currently available Topics 2.7–2.11. It is not a comprehensive assessment of every Unit 2 topic.

Practice Tips:

  • Trace carefully: Write down the values of important variables rather than guessing.
  • Check loop bounds: Pay close attention to < versus <=, and determine exactly when a loop stops.
  • Watch scope: A variable declared in a for loop header has scope limited to that loop.
  • Trace nested loops systematically: Complete the inner loop for each iteration of the outer loop.

Unit 2 Iteration & Algorithms Practice Assessment

Question 1 of 15 | Score: 0
1. What is printed as a result of executing the following code segment?
int k = 10;
while (k > 0) {
    System.out.print(k + " ");
    k -= 3;
}
Correct Answer: A k starts at 10 and decreases by 3 each iteration: 10, 7, 4, 1.When k becomes -2, the condition -2 > 0 is false, so the loop ends.
2. Consider the following code segment:
int sum = 0;

for (int i = 0; i < 5; i++) {
    sum += i;
}

System.out.println(i);
Correct Answer: D The variable i is declared in the for loop header. Its scope is limited to the loop. Attempting to print it outside the loop causes a compilation error.
3. What does the following method do?
public String process(String str) {
    String res = "";

    for (int i = 0; i < str.length(); i++) {
        if (i % 2 == 0) {
            res += str.substring(i, i + 1);
        }
    }

    return res;
}
Correct Answer: C The condition i % 2 == 0 is true for indices 0, 2, 4, 6, and so on. The method therefore extracts the characters located at the even indices.
4. How many times will the word "Hi" be printed?
for (int a = 0; a < 4; a++) {
    for (int b = 1; b < 3; b++) {
        System.out.println("Hi");
    }
}
Correct Answer: B The outer loop runs 4 times: a = 0, 1, 2, 3.The inner loop runs 2 times: b = 1, 2.Therefore:

4 × 2 = 8.
5. What value is stored in result after the code executes?
int result = 0;
int x = 5;

while (x > 5) {
    result += x;
    x--;
}
Correct Answer: A The initial condition 5 > 5 is false. The loop body executes zero times, leaving result at its initial value of 0.
6. Which algorithm computes the sum of the digits of a positive integer num?
Correct Answer: B Option B correctly isolates the rightmost digit using % 10, adds it to the sum, and then removes it from the number using integer division / 10.
7. What is printed by the following code?
for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
        if (j >= i) {
            System.out.print("*");
        }
    }
    System.out.println();
}
Correct Answer: A i = 1: the condition is true for j = 1, 2, 3 → ***
i = 2: the condition is true for j = 2, 3 → **
i = 3: the condition is true for j = 3 → *

The pattern therefore decreases by one star on each row.
8. What does val contain after the loop executes?
int val = 0;

for (int i = 2; i < 10; i *= 2) {
    val += i;
}
Correct Answer: B i starts at 2 and doubles after each iteration:

i = 2, i = 4, i = 8.

When i becomes 16, 16 < 10 is false.

Therefore:val = 2 + 4 + 8 = 14.
9. What does the following code print?
String s = "Mississippi";
int c = 0;

for (int i = 0; i < s.length() - 1; i++) {
    if (s.substring(i, i + 2).equals("ss")) {
        c++;
    }
}

System.out.print(c);
Correct Answer: B The loop checks adjacent two-character substrings.In "Mississippi", the substring "ss" occurs twice.Therefore, c becomes 2.
10. How many times does count++ execute?
int count = 0;

for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        if (i != j) {
            count++;
        }
    }
}
Correct Answer: B The nested loops execute 3 × 3 = 9 times in total.The condition i != j is false for exactly three pairs:

(0,0), (1,1), (2,2).

Therefore:9 – 3 = 6.
11. Which of the following is equivalent to the code below?
for (int i = 0; i < 10; i++) {
    /* body */
}
Correct Answer: B In a for loop, the update expression i++ executes after the loop body.Option B preserves that order:

1. Test i < 10
2. Execute the body
3. Execute i++
4. Repeat
12. What is the value of result?
int result = 1;

for (int i = 4; i > 1; i--) {
    result *= i;
}
Correct Answer: B The loop multiplies:

1 × 4 = 4
4 × 3 = 12
12 × 2 = 24

When i becomes 1, the condition 1 > 1 is false.Therefore, result is 24.
13. Which condition will prevent an infinite loop in the following structure?
int x = 10;

while (x ___ 0) {
    x -= 3;
}
Correct Answer: B With x > 0, the loop continues while x is positive.Starting from 10, the values are:

10 → 7 → 4 → 1 → -2

Once x becomes -2, the condition -2 > 0 is false, so the loop terminates.

By contrast, x != 0 would not be safe here because the sequence skips over 0.
14. What does the following code print?
String w = "hello";

for (int i = w.length(); i > 0; i--) {
    System.out.print(w.substring(i - 1, i));
}
Correct Answer: B i starts at 5.

w.substring(4, 5) returns "o".
Then i = 4 and w.substring(3, 4) returns "l".

Continuing in this way produces:olleh.
15. What is printed by this nested structure?
int c = 0;

for (int i = 0; i < 5; i++) {
    for (int j = 0; j < i; j++) {
        c++;
    }
}

System.out.println(c);
Correct Answer: B i = 0: inner loop executes 0 times.
i = 1: inner loop executes 1 time.
i = 2: inner loop executes 2 times.
i = 3: inner loop executes 3 times.
i = 4: inner loop executes 4 times.

Total:0 + 1 + 2 + 3 + 4 = 10.

Unit 2 Iteration & Algorithms Practice Complete!

0 / 15

What This Unit 2 Practice Assessment Covers

This assessment focuses on the currently available iteration and algorithm practice in this collection:

  • Topic 2.7: while Loops
  • Topic 2.8: for Loops
  • Topic 2.9: Implementing Selection and Iteration Algorithms
  • Topic 2.10: Implementing String Algorithms
  • Topic 2.11: Nested Iteration

These questions are designed to give you additional practice tracing Java code and reasoning about loop behavior. For the complete current Unit 2 framework, use the official Unit 2 practice hub.

Continue Your AP CSA Practice

Return to the AP CSA Unit 2: Selection and Iteration MCQ Practice hub to review the complete Unit 2 topic structure and access the currently available practice pages.

← Previous: Topic 2.11 Nested Iteration
Back to Unit 2 Hub →

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