• 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 1.15 MCQ Practice: String Manipulation

Home » AP CSA MCQ Practice » Unit 1: Using Objects and Methods » String Manipulation

AP CSA Unit 1.15 MCQ Practice: String Manipulation

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

The String class is an important part of AP Computer Science A. In this interactive practice set, you will practice Topic 1.15: String Manipulation, including String immutability, indexing, concatenation, substring(), indexOf(), equals(), and compareTo().

Key Concepts to Remember Before You Start:

  • Immutability: Strings in Java are immutable. Methods called on a String do not change the original String object. For example, toUpperCase() returns a new String.
  • Zero-Based Indexing: The first character of a String is at index 0. The last character is at index length() - 1.
  • Substring: The method substring(int from, int to) includes the character at from but excludes the character at to. The returned String has length to - from.
  • String Methods: The current AP CSA framework includes length(), substring(), indexOf(), equals(), and compareTo() as String methods students should know.

Test your knowledge of these important String concepts with the 10 practice questions below.

Topic 1.15 Practice

Question 1 of 10 | Score: 0
1. What is printed as a result of executing the following code segment?
String s = “computer”;
System.out.println(s.substring(3, 6));
Correct Answer: A The substring(int from, int to) method includes the character at from and excludes the character at to. The indices of “computer” are c(0), o(1), m(2), p(3), u(4), t(5), e(6), r(7). Therefore, the result is "put".
2. What is output by the following code?
String word = “hello”;
word.toUpperCase();
System.out.println(word);
Correct Answer: B Strings are immutable in Java. The method word.toUpperCase() returns a new String object containing "HELLO", but the result is not assigned back to word. Therefore, word remains "hello".
3. What is printed as a result of executing the following code segment?
String str = “banana”;
int index1 = str.indexOf(“a”);
int index2 = str.indexOf(“n”);
System.out.println(index1 + index2);
Correct Answer: C The indexOf(String str) method returns the index of the first occurrence of the specified String. The first "a" is at index 1 and the first "n" is at index 2. Therefore, 1 + 2 = 3.
4. Consider the following code segment. What will occur when it runs?
String s = “apcsa”;
System.out.println(s.substring(2, 6));
Correct Answer: D The String "apcsa" has length 5. Its valid character indices are 0 through 4. For substring(2, 6), the ending index must not exceed the String length. Because 6 is greater than 5, Java throws a StringIndexOutOfBoundsException.
5. What is printed as a result of executing the following code segment?
String a = new String(“test”);
String b = new String(“test”);
System.out.println((a == b) + ” ” + a.equals(b));
Correct Answer: C The new keyword creates two separate String objects. The == operator compares the object references, so a == b is false. The equals() method compares the sequence of characters, so a.equals(b) is true.
6. What is output by the following code segment?
String x = “12”;
String y = “34”;
System.out.println(x.length() + x + y);
Correct Answer: A x.length() evaluates to the integer 2. Java then evaluates 2 + "12". Because one operand is a String, the result becomes the String "212". The final concatenation with "34" produces "21234".
7. What is printed as a result of executing the following code segment?
String s1 = “apple”;
String s2 = “ape”;
System.out.println(s1.compareTo(s2) > 0);
Correct Answer: A The compareTo() method compares Strings lexicographically. At the first position where the Strings differ, "apple" has 'p' while "ape" has 'e'. Since 'p' comes after 'e', the result of s1.compareTo(s2) is positive. Therefore, the expression is true.
8. What is printed as a result of executing the following code segment?
String str = “science”;
System.out.println(str.substring(str.indexOf(“e”)));
Correct Answer: B Evaluate the expression from the inside out. str.indexOf("e") finds the first "e" at index 3. The expression therefore becomes str.substring(3). The one-argument form of substring() returns the characters from that index through the end of the String, producing "ence".
9. What is output by the following code segment?
String word = “a\”b\\c”;
System.out.println(word.length());
Correct Answer: B Each escape sequence represents one character in the resulting String. The characters are: 'a', '"', 'b', '\', and 'c'. Therefore, the String contains 5 characters.
10. What is output by the following code segment?
int a = 5;
int b = 10;
System.out.println(“Sum is: ” + a + b);
Correct Answer: B Java evaluates this expression from left to right. Once Java evaluates "Sum is: " + a, the result is a String. Therefore, the following + b performs String concatenation rather than numerical addition. The result is "Sum is: 510".

Topic 1.15 Practice Complete!

0 / 10

Why Do Students Struggle with String.substring()?

The most common mistake is misunderstanding the to index in the substring(from, to) method. Remember this rule: the character at the to index is not included. For example, str.substring(0, 3) returns the characters at indices 0, 1, and 2.

Continue Your AP CSA Practice

You have now practiced the core String concepts covered in AP CSA Unit 1 Topic 1.15: String Manipulation. For additional Unit 1 practice, continue with the current AP CSA Unit 1 topics below.

Math Class: AP CSA Unit 1.11 Math Class MCQ Practice

Object References: AP CSA Unit 1.12–1.14 Object References & Instance Methods

← Back to AP CSA MCQ Practice
Unit 1: Using Objects & Methods →

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