Unit 1 Review: What Do These String Calls Return? 🌶️🌶️
A review for the end of Unit 1: the String methods that the quiz and the test keep asking about. Write each result as just the characters, without quotation marks. If a call would throw an exception, type the exception's full name instead. Every question asks you to predict the result before you check, the way the exam does.
| Call | What it returns |
|---|---|
s.length() | the number of characters |
s.substring(a) | everything from index a to the end |
s.substring(a, b) | the characters from index a up to, but not including, index b |
s.indexOf(t) | the index where t first starts, or -1 if t is not in s |
s.compareTo(t) | a negative number if s comes before t, 0 if they are equal, a positive number if s comes after t |
The first character is at index 0, so the last is at
s.length() - 1. A substring whose indexes fall outside 0 to s.length(), or whose start is after its end, throws a StringIndexOutOfBoundsException.
substring. The word is "programming": p is index 0, r 1, o 2, g 3, r 4, a 5, m 6, m 7, i 8, n 9, g 10, so it has 11 characters.1. What does "programming".substring(7) return?
ming
2. What does "programming".substring(3, 7) return?
gram
3. What does "programming".substring(2, 3) return?
o
4. How many characters are in "programming".substring(4, 9)? (Type a number.)
5
5. What happens with "programming".substring(5, 12)? Type the exception's name if it throws, otherwise the result.
StringIndexOutOfBoundsException
6. What happens with "programming".substring(8, 4)? Type the exception's name if it throws, otherwise the result.
StringIndexOutOfBoundsException
7. How many characters are in "programming".substring(11)? The String has exactly 11 characters. (Type a number, or the exception's name if it throws.)
0
indexOf. The word is "Mississippi": M 0, i 1, s 2, s 3, i 4, s 5, s 6, i 7, p 8, p 9, i 10.8. What does "Mississippi".indexOf("ss") return?
2
9. What does "Mississippi".indexOf("i") return? The letter i appears four times.
1
10. What does "Mississippi".indexOf("S") return?
-1
11. What does "Mississippi".indexOf("ppi") return?
8
12. What does "Mississippi".indexOf("sip") return? Read the word letter by letter.
6
13. What is the value of "Mississippi".indexOf("s") + "Mississippi".indexOf("p")?
10
compareTo. It compares character by character using each character's code (a is 97, b is 98, ... and every capital letter has a SMALLER code than every lowercase letter). At the first position where they differ it returns the difference of the two codes; if one String is a prefix of the other it returns the difference in their lengths.14. What does "apple".compareTo("banana") return?
-1
15. What does "cat".compareTo("cab") return?
18
16. What does "cat".compareTo("catalog") return? (The first three characters match.)
-4
17. What does "Zebra".compareTo("apple") return? Remember the codes: Z is 90 and a is 97.
-7
18. Which statement about "dog".compareTo("dot") is true?
Your score for your class is recorded when you click Submit all — checking individual questions doesn't save it. You can submit again to improve your score; your best one counts.