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

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

AP CSA Unit 2.7 MCQ Practice: While Loops

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

While loops are an important part of AP Computer Science A Unit 2: Selection and Iteration. Topic 2.7 focuses on using while loops to repeat instructions while a Boolean condition remains true. You should be able to trace loop control variables, evaluate conditions accurately, determine how many times a loop executes, and identify potential infinite loops.

In this interactive practice set, you will work through 10 multiple-choice questions covering Topic 2.7: while Loops. The questions emphasize code tracing, loop conditions, control-variable updates, integer arithmetic, and common off-by-one situations.

Key Concepts to Remember Before You Start:

  • Pre-Test Evaluation: The Boolean condition in a while loop is evaluated before each iteration. If it evaluates to false initially, the loop body never runs.
  • Loop Control Variable Updates: The loop control variable should be updated appropriately so that the condition can eventually become false.
  • Infinite Loop Traps: Failing to update a loop control variable can cause a loop to continue indefinitely.
  • Off-by-One Errors: Pay close attention to strict inequalities such as < and > versus inclusive inequalities such as <= and >=.

Work through the interactive quiz below. After each question, review the explanation to strengthen your code-tracing and loop-analysis skills.

AP CSA 2.7: While Loops Practice

Question 1 of 10 | Score: 0
1. Consider the following code segment:
int count = 1;
int sum = 0;
while (count <= 4) {
    sum += count;
    count++;
}
System.out.println(sum);
What value is printed to the console after the loop terminates?
Correct Answer: B

count starts at 1. The loop adds 1, 2, 3, and 4 to sum, giving 10. After the fourth iteration, count becomes 5, so 5 <= 4 is false and the loop terminates.

2. Consider the following code segment:
int x = 10;
while (x > 0) {
    System.out.print(x + ” “);
    x -= 2;
}
Which of the following best describes what happens when this code is executed?
Correct Answer: A

The loop prints 10, 8, 6, 4, and 2. After printing 2, x becomes 0. The next condition check is 0 > 0, which is false, so 0 is not printed.

3. Consider the following code segment:
int num = 3;
while (num < 10) {
    System.out.print(num + ” “);
    num += 3;
}
What is printed as a result of executing the code segment?
Correct Answer: B

num is 3, then 6, then 9. After printing 9, num becomes 12. The next condition, 12 < 10, is false, so the loop stops.

4. Consider the following code segment intended to compute the product of numbers from 1 to 5. What is wrong with this code?
int product = 1;
int i = 1;
while (i <= 5) {
    product *= i;
}
Correct Answer: B

i remains 1 because it is never updated inside the loop. Therefore, 1 <= 5 remains true and the loop does not terminate.

5. Consider the following digit-extraction algorithm:
int n = 345;
int sum = 0;
while (n > 0) {
    sum += n % 10;
    n /= 10;
}
System.out.println(sum);
What value is printed after the loop finishes?
Correct Answer: C

The algorithm adds the digits of 345. First, 345 % 10 = 5. Then 34 % 10 = 4. Finally 3 % 10 = 3. Therefore, sum = 5 + 4 + 3 = 12.

6. Consider the following code segment:
int val = 2;
while (val < 30) {
    val *= 2;
}
System.out.println(val);
What is printed as a result of executing the code segment?
Correct Answer: B

The values of val are 2, 4, 8, 16, and 32. When val becomes 32, the condition 32 < 30 is false, so the loop terminates and 32 is printed.

7. Consider the following code segment:
int k = 5;
while (k > 5) {
    System.out.print(k + ” “);
    k–;
}
System.out.println(“Done”);
What is printed as a result of executing the code segment?
Correct Answer: C

The condition is checked before the loop body executes. Since k = 5, the condition 5 > 5 is false immediately. Therefore, the loop body is skipped and only Done is printed.

8. Consider the following code segment:
int a = 1;
int b = 20;
while (a < b) {
    a += 3;
    b -= 2;
}
System.out.println(a + ” ” + b);
What is printed to the console after the loop terminates?
Correct Answer: B

Trace the values after each iteration:

  • Start: a = 1, b = 20
  • Iteration 1: a = 4, b = 18
  • Iteration 2: a = 7, b = 16
  • Iteration 3: a = 10, b = 14
  • Iteration 4: a = 13, b = 12

The next condition check is 13 < 12, which is false. Therefore, the loop terminates and prints 13 12.

9. Consider the following code segment:
int n = 12;
int count = 0;
while (n > 1) {
    n /= 2;
    count++;
}
System.out.println(count);
What value is printed after execution?
Correct Answer: B

Java integer division truncates the fractional part:

  • Start: n = 12 → n = 6, count = 1
  • n = 6 → n = 3, count = 2
  • n = 3 → n = 1, count = 3

The next condition is 1 > 1, which is false. Therefore, the loop terminates and prints 3.

10. Which of the following statements regarding while loops in Java is FALSE?
Correct Answer: C

Variables declared inside a block, such as a while loop body, have block scope. They cannot be accessed outside that block.

Unit 2.7 While Loops Practice Complete!

0 / 10

Why Mental Trace Tables Matter for While Loops

In AP CSA multiple-choice questions, you may need to determine what a program does without executing it. Questions involving while loops often require you to maintain the values of loop control variables and accumulators across multiple iterations. A quick trace table can help you track those values and avoid off-by-one errors.

Continue Your AP CSA Practice

Once you are comfortable tracing while loops, continue with Topic 2.8: For Loops Practice , where you will practice initialization, condition checks, update expressions, and loop tracing.

← Back to Unit 2: Selection & Iteration
Next: Topic 2.8 For Loops →

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