• 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.8 MCQ Practice: For Loops

Home » AP CSA MCQ » Unit 2: Selection and Iteration » For Loops

AP CSA Unit 2.8 MCQ Practice: For Loops

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

Counting loops are an important Java iteration structure for AP Computer Science A questions involving repetition, counting, traversal, and algorithmic reasoning. Topic 2.8 focuses on for loops, including initialization, Boolean condition checks, and update expressions.

In this interactive practice set, we cover Topic 2.8: For Loops. These 10 multiple-choice questions test your ability to trace counting sequences, manage loop control variable scope, and identify boundary conditions without an IDE.

Key Concepts to Remember Before You Start:

  • Header Anatomy: A for loop header consists of three parts: initialization (runs once), Boolean condition (checked before each iteration), and update expression (runs after the loop body).
  • Variable Scope: A loop control variable declared inside the for header (for example, for (int i = 0; ...)) is local to the loop and cannot be accessed outside of it.
  • Step Sizes and Countdown Loops: Iteration step values can increment by custom amounts (i += 2) or decrement backward (i--).
  • Off-By-One Traps: Carefully verify whether boundaries use strict inequalities (<) or inclusive inequalities (<=) to determine exact iteration counts.

Test your knowledge with the interactive quiz below. Review the explanations for any questions you miss to strengthen your mental tracing accuracy.

AP CSA 2.8: For Loops Practice

Question 1 of 10 | Score: 0
1. Consider the following code segment. How many times is the string "Java" printed to the console?
for (int i = 1; i < 8; i += 2) {
    System.out.print(“Java “);
}
Correct Answer: C Trace table breakdown: i takes values 1, 3, 5, and 7. Each of these satisfies i < 8, printing “Java ” 4 times. When i becomes 9, 9 < 8 is false, terminating the loop.
2. Consider the following code segment. What value is stored in total after the loop finishes executing?
int total = 0;
for (int i = 5; i > 0; i–) {
    if (i % 2 == 0) {
        total += i;
    }
}
System.out.println(total);
Correct Answer: C The loop evaluates i from 5 down to 1. The even values of i in this range are 4 and 2. Adding these together gives 4 + 2 = 6.
3. Consider the following code segment:
for (int i = 0; i < 5; i++) {
    System.out.print(i + ” “);
}
System.out.println(i);
What is the result of attempting to compile and run this code?
Correct Answer: C Because i is declared inside the for loop header, its scope is restricted to the loop. Attempting to reference i in System.out.println(i); outside the loop causes a compile-time error.
4. Consider the following code segment:
int sum = 0;
for (int i = 1; i <= 6; i += 2) {
    sum += i;
}
System.out.println(sum);
Correct Answer: B The loop control variable i takes odd values: 1, 3, and 5. When i becomes 7, 7 <= 6 is false. Summing these values gives 1 + 3 + 5 = 9.
5. Consider the following code segment:
int count = 0;
for (int i = 10; i > 0; i /= 2) {
    count++;
}
System.out.println(count);
Correct Answer: B Trace with integer division:

i = 10 → count = 1, i becomes 5.
i = 5 → count = 2, i becomes 2.
i = 2 → count = 3, i becomes 1.
i = 1 → count = 4, i becomes 0.
i = 0 → condition is false and the loop ends.

Therefore, the loop executes 4 times.
6. Which of the following for loop headers will result in an infinite loop?
Correct Answer: C In option C, i starts at 0 and decreases on every iteration. It becomes -1, -2, -3, and so on. Because every negative value is still less than 10, the condition i < 10 remains true and the loop never terminates.
7. Consider the following code segment:
String s = “computer”;
String result = “”;
for (int i = 0; i < s.length(); i += 2) {
    result += s.substring(i, i + 1);
}
System.out.println(result);
Correct Answer: A The loop increments i by 2, accessing characters at indices 0, 2, 4, and 6:

Index 0: 'c'
Index 2: 'm'
Index 4: 'u'
Index 6: 'e'

Combining these characters gives "cmue".
8. Consider the following code segment:
int p = 1;
for (int i = 1; i <= 4; i++) {
    p *= i;
}
System.out.println(p);
Correct Answer: B This loop calculates the factorial of 4 (4!):

i = 1: p = 1 × 1 = 1
i = 2: p = 1 × 2 = 2
i = 3: p = 2 × 3 = 6
i = 4: p = 6 × 4 = 24

Therefore, the program prints 24.
9. Consider the following code segment:
int val = 5;
for (int i = 0; i < 3; i++) {
    val += i;
}
System.out.println(val);
Correct Answer: C Trace table:

Start: val = 5
i = 0: val = 5 + 0 = 5
i = 1: val = 5 + 1 = 6
i = 2: val = 6 + 2 = 8

The loop terminates when i reaches 3, so the program prints 8.
10. Which of the following statements about for loops in Java is FALSE?
Correct Answer: C The execution order for a for loop is:

1) Initialization
2) Boolean condition check
3) Loop body execution
4) Update expression

The update expression executes after the loop body. The condition is then checked again before the next iteration.

Unit 2.8 For Loops Practice Complete!

0 / 10

Why For Loop Practice Matters for AP CSA

For loops are an important Java iteration structure for AP CSA questions involving repetition, counting, traversal, and algorithmic reasoning. Understanding loop boundaries, update expressions, variable scope, and trace patterns can help you solve code-tracing questions accurately.

Continue Your AP CSA Practice

Now that you have practiced for loops, continue with loop-based algorithms involving accumulators, string processing, and other common iteration patterns.

Continue your AP CSA review with Topics 2.9–2.10: Loop Algorithms Practice , where you can practice implementing selection and iteration algorithms and string algorithms.

← Previous: Topic 2.7 While Loops
Next: Topics 2.9–2.10 Loop Algorithms →

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