• 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 Mock Exam: Comprehensive Iteration Practice

Home » AP CSA MCQ » Unit 4 » Mock Exam

AP CSA Unit 4 Mock Exam: Comprehensive Iteration

📚 Comprehensive ⏱️ 15-20 Min Practice ✅ Mixed Concepts

You have completed the core topics for Unit 4: Iteration. This comprehensive mock exam blends while loops, for loops, string algorithms, and nested loops into a single 15-question test designed to mimic the difficulty of the AP Computer Science A exam.

Exam Tips:

  • Trace carefully: Do not guess. Write out a trace table for variables before selecting an answer.
  • Check bounds: Pay close attention to < vs <=, and whether strings process to length() or length() - 1.
  • Watch the scope: Remember that loop variables declared in a for loop header do not exist outside the loop.

Unit 4 Iteration Mock Exam

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. It only exists within the loop’s scope. 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… meaning the method extracts and returns the characters located at the even indices of the string.
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). Total executions = 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: j prints * for 1, 2, 3 (***)
i=2: j prints * for 2, 3 (**)
i=3: j prints * for 3 (*)
The pattern decreases by one star 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 multiplies by 2 each iteration: i=2, i=4, i=8. When i=16, 16 < 10 is false.
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 for overlapping adjacent pairs matching “ss”. In “Mississippi”, the substring “ss” appears exactly twice.
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 inner loop executes 9 times total. The condition i != j is true for every pair EXCEPT (0,0), (1,1), and (2,2). So it executes 9 – 3 = 6 times.
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 body. Option B correctly replicates this logic by placing i++ at the very end of the while loop body.
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: result = 1 * 4 = 4. Then 4 * 3 = 12. Then 12 * 2 = 24. When i=1, 1 > 1 is false. Output is 24.
13. Which condition will prevent an infinite loop in the following structure?
int x = 10;
while (x ___ 0) {
    x -= 2;
}
Correct Answer: B Using != 0 can be risky if x misses 0 (e.g. if x started odd). However, x > 0 guarantees the loop terminates safely once x becomes 0 or negative.
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’. Next i=4, w.substring(3, 4) returns ‘l’. This is a standard reverse string loop, printing “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: j<0 (0 times)
i=1: j<1 (1 time)
i=2: j<2 (2 times)
i=3: j<3 (3 times)
i=4: j<4 (4 times)
Total = 0 + 1 + 2 + 3 + 4 = 10.

Unit 4 Mock Exam Complete!

0 / 15

Congratulations on Completing Unit 4!

Iteration is arguably the most important foundational concept in all of computer science. By mastering while loops, for loops, and nested iteration, you now have the tools required to traverse data structures, process strings, and write complex algorithms.

Now that you know how to control the flow of your programs, it is time to learn how to design your own custom objects and behaviors. Continue your AP CSA journey by moving on to the next major module: Unit 5: Writing Classes.

← Back to Unit 4 Iteration Hub
Return to AP CSA MCQ 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 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