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
What does `String.valueOf(42)` return?
Answer: "42"
String.valueOf(int) converts the integer 42 to its String representation "42".
What is the purpose of `str.trim()` in Java?
Answer: Returns a new String with leading and trailing whitespace removed
trim() returns a copy of the String with whitespace (spaces, tabs, newlines) stripped from both the beginning and end.
What is the sum of all elements in a 2D array `int[][] a = {{1,2},{3,4},{5,6}}`?
Answer: 21
Summing all elements: 1+2+3+4+5+6 = 21, using a nested loop to add each element.
What does `str.contains("ell")` return for `str = "Hello"`?
Answer: true
contains() returns true if the specified sequence appears anywhere within the String; "ell" is found within "Hello".
What keyword makes a String variable refer to a string literal shared in the string pool?
Answer: No special keyword — string literals are automatically pooled
Java automatically places String literals in the string pool; no keyword is needed, though intern() can force pool placement for non-literal strings.
In a 2D array, what does `matrix[0].length` represent?
Answer: The number of columns in row 0
matrix[0] refers to the first row (itself an array), and .length on that row gives the number of columns.