String Manipulation - AP Computer Science A

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

Study these flashcards