String Manipulation and 2D Arrays Flashcards
6 cards from real AP CSA practice questions. Tap to flip, then mark Knew It or Still Learning — missed cards come back until you master them.
Read the first 6 String Manipulation and 2D Arrays flashcards as text
Which method returns the number of characters in a String in Java?
Answer: length()
The String.length() method returns the number of characters (including spaces) in the string.
What does `str.substring(2, 5)` return for `str = "ABCDEFG"`?
Answer: "CDE"
substring(2,5) returns characters from index 2 up to but not including index 5, giving 'C','D','E' → "CDE".
What does `str.charAt(0)` return for `str = "Hello"`?
Answer: 'H'
charAt(0) returns the char at index 0, which is 'H' — a primitive char, not a String.
How do you compare two Strings for equal content in Java?
Answer: str1.equals(str2)
The equals() method compares String content; using == compares object references, which may give unexpected results for Strings.
What does `str.indexOf("lo")` return for `str = "Hello"`?
Answer: 3
indexOf returns the starting index of the first occurrence of the substring; "lo" starts at index 3 in "Hello".
What is the result of `"Hello" + " " + "World"`?
Answer: "Hello World"
The + operator concatenates Strings in Java, combining "Hello", " ", and "World" into "Hello World".