String Manipulation - AP Computer Science A
- Which method checks if a string ends with a specific suffix?
- endsWith(). Tests if the string concludes with specified text.
- What method compares two strings lexicographically in Java?
- compareTo(). Returns negative, zero, or positive for alphabetical ordering.
- What method checks if a string contains a specified sequence of char values?
- contains(). Tests if string includes specified character sequence.
- What is the output of '"Hello World".indexOf("o");'?
- 4. First 'o' appears at position 4 in 'Hello World'.
- Find and correct: 'String s = "Java"; s = s.toLowercase();'
- Correct: 's = s.toLowerCase();'. Method name is 'toLowerCase', not 'toLowercase'.
- Which method returns a string representation of an object?
- toString(). Converts any object to its string representation.
- What is the output of '" hello ".trim();'?
- "hello". Removes whitespace from both ends of string.
- What method converts a string to lowercase in Java?
- toLowerCase(). Converts all characters to their lowercase equivalents.
- What is the output of '"Hello".substring(1, 4);'?
- "ell". Extracts characters from index 1 to 3 (exclusive).
- Which method checks if a string starts with a specific prefix?
- startsWith(). Tests if the string begins with specified text.
- Find and correct the error: 'System.out.println(s.charAt(5));' for 'String s = "Java";'
- Correct: 'System.out.println(s.charAt(3));'. String 'Java' has length 4, so valid indices are 0-3.
- What method returns a new string resulting from replacing all occurrences of oldChar?
- replace(). Replaces characters without regex pattern matching.
- What is the output of '"Java".charAt(0);'?
- 'J'. Returns the character at index 0 (first position).
- What method returns the last index of a character in a string?
- lastIndexOf(). Returns position of final occurrence of specified character.
- Identify the method to trim whitespace from both ends of a string.
- trim(). Removes leading and trailing whitespace characters.
- Which method checks if two strings are equal in Java?
- equals(). Compares string content for exact equality.
- What method returns the length of a string in Java?
- length(). Returns the number of characters in the string.
- What is the result of '"Hello".equalsIgnoreCase("hello");'?
- true. Case-insensitive comparison returns true for same content.
- Identify the method that returns the index of a character in a string.
- indexOf(). Returns position of first occurrence of specified character.
- Find and correct: 'String s = "abc"; s = s.concat('d');'
- Correct: 's = s.concat("d");'. Character literals use single quotes, not double quotes.
- Which method concatenates two strings in Java?
- concat(). Appends one string to the end of another.
- Identify the method to replace characters in a string.
- replace(). Substitutes all occurrences of a character or substring.
- Identify the method to convert a string to uppercase in Java.
- toUpperCase(). Converts all characters to their uppercase equivalents.
- Find and correct the error: 'String s = new String("hello").toLowerCase();'
- Correct: 'String s = "hello".toLowerCase();'. String literals don't need 'new String()' constructor.
- What is the output of '"abcdef".substring(2);'?
- "cdef". Substring from index 2 to end of string.
- What is the output of '"abcdef".lastIndexOf("e");'?
- 4. Last occurrence of 'e' is at index 4.
- What method returns a substring from a string in Java?
- substring(). Extracts a portion of the string by index range.
- What is the result of '"abc".concat("xyz");'?
- "abcxyz". Concatenation combines strings into single result.
- Which method returns a character array representing the string?
- toCharArray(). Converts string into array of individual characters.
- What method checks if a string is empty in Java?
- isEmpty(). Returns true if string length is zero.
- What is the output of '"Java".substring(1, 2);'?
- "a". Extracts single character from index 1 to 2 (exclusive).