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 indexlength() - 1. -
Substring:
The method
substring(int from, int to)includes the character atfrombut excludes the character atto. The returned String has lengthto - from. -
String Methods:
The current AP CSA framework includes
length(),substring(),indexOf(),equals(), andcompareTo()as String methods students should know.
Test your knowledge of these important String concepts with the 10 practice questions below.
Topic 1.15 Practice
System.out.println(s.substring(3, 6));
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".word.toUpperCase();
System.out.println(word);
word.toUpperCase() returns a new String object
containing "HELLO", but the result is not assigned
back to word. Therefore, word remains
"hello".int index1 = str.indexOf(“a”);
int index2 = str.indexOf(“n”);
System.out.println(index1 + index2);
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.System.out.println(s.substring(2, 6));
"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.String b = new String(“test”);
System.out.println((a == b) + ” ” + a.equals(b));
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.String y = “34”;
System.out.println(x.length() + x + y);
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".String s2 = “ape”;
System.out.println(s1.compareTo(s2) > 0);
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.System.out.println(str.substring(str.indexOf(“e”)));
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".System.out.println(word.length());
'a', '"', 'b',
'\', and 'c'.
Therefore, the String contains 5 characters.int b = 10;
System.out.println(“Sum is: ” + a + b);
"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!
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