• 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.9–2.10 MCQ Practice: Loop & String Algorithms

Home » AP CSA MCQ » Unit 2: Selection and Iteration » Loop & String Algorithms

AP CSA Unit 2.9–2.10 MCQ Practice: Loop & String Algorithms

📚 Topics 2.9–2.10 ⏱️ 10 Min Practice ✅ Current AP CSA Framework

Once you understand basic selection and iteration, AP Computer Science A asks you to apply loops to solve algorithmic problems. Topics 2.9 and 2.10 focus on implementing selection and iteration algorithms and processing strings using loops.

In this interactive practice set, you will trace algorithms involving accumulator variables, string processing, character traversal, pattern matching, and digit extraction.

Key Concepts to Remember Before You Start:

  • Accumulator Patterns: Initialize a variable such as sum to 0 or a product accumulator to 1, then update it during each loop iteration.
  • String Processing: Traverse strings using methods such as substring(), indexOf(), and length().
  • Reversing Strings: Build a new string by traversing the original string from the last index toward the first.
  • Pattern Matching: Use loops and String methods to locate characters, substrings, or repeated patterns.
  • Digit Extraction: Use % 10 to isolate the rightmost digit and integer division by 10 to remove it.

Test your knowledge with the interactive quiz below. Review the explanations for any questions you miss to strengthen your algorithm-tracing skills.

AP CSA Topics 2.9–2.10: Loop & String Algorithms

Question 1 of 10 | Score: 0
1. Consider the following code segment designed to process a string:
String str = “java”;
String rev = “”;
for (int i = str.length() – 1; i >= 0; i–) {
    rev += str.substring(i, i + 1);
}
System.out.println(rev);
Correct Answer: B

str.length() is 4, so i starts at index 3 (‘a’). The loop then visits indexes 2, 1, and 0. Concatenating those characters produces the reversed string "avaj".

2. Consider the following code segment:
String s = “code.org”;
int count = 0;
for (int i = 0; i < s.length(); i++) {
    if (s.substring(i, i + 1).equals(“o”)) {
        count++;
    }
}
System.out.println(count);
Correct Answer: B

The loop examines every character in "code.org". The letter 'o' appears twice, so count becomes 2.

3. Consider the following code segment that processes digits:
int n = 4825;
int maxDigit = 0;
while (n > 0) {
    int d = n % 10;
    if (d > maxDigit) {
        maxDigit = d;
    }
    n /= 10;
}
System.out.println(maxDigit);
Correct Answer: C

The algorithm extracts digits from right to left using % 10 and removes each digit using integer division by 10. The digits are 5, 2, 8, and 4. The largest digit is 8.

4. Consider the following code segment:
String word = “computer”;
String result = “”;
for (int i = 1; i < word.length(); i += 2) {
    result += word.substring(i, i + 1);
}
System.out.println(result);
Correct Answer: B

The loop starts at index 1 and increases by 2. It therefore visits indexes 1, 3, 5, and 7, producing o, p, t, and r. The result is "optr".

5. Consider the following code segment using an accumulator:
int sum = 0;
for (int i = 1; i <= 5; i++) {
    sum += (i * 10);
}
System.out.println(sum);
Correct Answer: C

The loop runs five times. The values added are 10 + 20 + 30 + 40 + 50, giving 150.

6. Consider the following code segment:
int n = 1234;
int sum = 0;
while (n > 0) {
    sum += n % 10;
    n /= 10;
}
System.out.println(sum);
Correct Answer: A

The algorithm extracts the digits 4, 3, 2, and 1. Their sum is 4 + 3 + 2 + 1 = 10.

7. Consider the following code segment:
String s = “apple”;
String res = “”;
for (int i = 0; i < s.length(); i++) {
    String c = s.substring(i, i + 1);
    if (res.indexOf(c) < 0) {
        res += c;
    }
}
System.out.println(res);
Correct Answer: C

indexOf(c) returns -1 when the character is not already present in res. The first occurrence of each character is therefore added, producing "aple".

8. Consider the following code segment:
int val = 0;
for (int i = 1; i <= 4; i++) {
    val = val * 2 + i;
}
System.out.println(val);
Correct Answer: B

Trace the value of val:

  • Start: val = 0
  • i = 1: val = 0 * 2 + 1 = 1
  • i = 2: val = 1 * 2 + 2 = 4
  • i = 3: val = 4 * 2 + 3 = 11
  • i = 4: val = 11 * 2 + 4 = 26

The final value printed is 26.

9. Consider the following code segment:
String s = “abracadabra”;
int c = 0;
for (int i = 0; i < s.length() – 1; i++) {
    if (s.substring(i, i + 2).equals(“ab”)) {
        c++;
    }
}
System.out.println(c);
Correct Answer: B

The loop stops at s.length() - 2, so substring(i, i + 2) always has two valid characters. The substring "ab" occurs twice in "abracadabra".

10. Which of the following mathematical expressions is used within a loop to isolate the rightmost digit of a positive integer n?
Correct Answer: B

The remainder when a positive base-10 integer is divided by 10 is its rightmost digit. For example, 345 % 10 is 5. Integer division by 10 removes the rightmost digit.

Unit 2.9–2.10 Algorithms Practice Complete!

0 / 10

Why Loop and String Algorithms Matter

Loop-based algorithms are an important part of AP Computer Science A. You should be comfortable tracing loops, processing strings character by character, counting occurrences, building new strings, and using accumulator variables to solve algorithmic problems.

These skills also provide a foundation for understanding more complex algorithms involving nested iteration and data collections.

Continue Your AP CSA Practice

Next, practice Topic 2.11: Nested Iteration to learn how one loop executes inside another and how to trace the resulting execution.

Continue to Topic 2.11: Nested Iteration Practice .

← Previous: Topic 2.8 For Loops
Next: Topic 2.11 Nested Iteration →

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