Bird
Raised Fist0
Javaprogramming~5 mins

Common string methods in Java - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does the length() method do in a Java String?
It returns the number of characters in the string, including spaces and special characters.
Click to reveal answer
beginner
How does the toUpperCase() method affect a string?
It creates a new string where all letters are converted to uppercase. The original string stays the same.
Click to reveal answer
intermediate
What is the purpose of the substring(int beginIndex, int endIndex) method?
It extracts a part of the string starting at beginIndex and ending just before endIndex, returning that part as a new string.
Click to reveal answer
beginner
Explain what equals(String anotherString) does.
It checks if two strings have exactly the same characters in the same order and returns true if they do, otherwise false.
Click to reveal answer
beginner
What does the trim() method do to a string?
It removes any whitespace at the start and end of the string but does not change spaces inside the string.
Click to reveal answer
Which method returns the number of characters in a Java string?
Asize()
Blength()
Ccount()
DgetLength()
What does toLowerCase() do to a string?
AConverts all letters to lowercase
BRemoves spaces
CChecks if string is lowercase
DChanges the original string
How do you get a part of a string from index 2 to 5 (exclusive) in Java?
Asubstring(2, 5)
Bslice(2, 5)
Csubstr(2, 5)
Dpart(2, 5)
Which method compares two strings for exact equality?
Acompare()
Bmatch()
Cequals()
D== operator
What does the trim() method remove from a string?
AAll spaces inside the string
BAll whitespace characters everywhere
CTabs inside the string only
DSpaces at the start and end only
Describe five common string methods in Java and what each one does.
Think about how you check size, change case, cut parts, compare, and clean spaces in strings.
You got /5 concepts.
    Explain how you would extract a substring and compare two strings for equality in Java.
    Focus on methods that cut strings and check if two strings are the same.
    You got /2 concepts.

      Practice

      (1/5)
      1. Which Java String method checks if a string contains a specific sequence of characters?
      easy
      A. equals()
      B. contains()
      C. substring()
      D. replace()

      Solution

      1. Step 1: Understand the purpose of contains()

        The contains() method checks if the string includes a specific sequence of characters and returns true or false.
      2. Step 2: Compare with other methods

        equals() checks full equality, substring() extracts part of a string, and replace() changes characters. None check for containment.
      3. Final Answer:

        contains() -> Option B
      4. Quick Check:

        Check substring presence = contains() [OK]
      Hint: Use contains() to check if text is inside a string [OK]
      Common Mistakes:
      • Confusing contains() with equals()
      • Using substring() to check presence
      • Thinking replace() checks for text
      2. Which of the following is the correct syntax to convert a string str to uppercase in Java?
      easy
      A. str.toUpperCase();
      B. str.uppercase();
      C. toUpperCase(str);
      D. str.toUpper();

      Solution

      1. Step 1: Recall the method name for uppercase conversion

        The correct method name in Java String class is toUpperCase(), called on the string object.
      2. Step 2: Check syntax correctness

        str.toUpperCase(); uses str.toUpperCase(); which is valid. Others use incorrect method names or syntax.
      3. Final Answer:

        str.toUpperCase(); -> Option A
      4. Quick Check:

        Uppercase method = toUpperCase() [OK]
      Hint: Remember Java String methods use camelCase like toUpperCase() [OK]
      Common Mistakes:
      • Using uppercase() instead of toUpperCase()
      • Calling method without object (toUpperCase(str))
      • Using toUpper() which does not exist
      3. What is the output of the following Java code?
      String text = "Hello World";
      int pos = text.indexOf("World");
      System.out.println(pos);
      medium
      A. 6
      B. 0
      C. -1
      D. 5

      Solution

      1. Step 1: Understand what indexOf() does

        The indexOf() method returns the starting index of the substring if found, or -1 if not found.
      2. Step 2: Find the position of "World" in "Hello World"

        "Hello " has 6 characters (index 0 to 5), so "World" starts at index 6.
      3. Final Answer:

        6 -> Option A
      4. Quick Check:

        Substring start index = 6 [OK]
      Hint: indexOf() returns first position of substring or -1 if missing [OK]
      Common Mistakes:
      • Confusing index with length
      • Expecting index 0 for substring
      • Thinking indexOf() returns boolean
      4. Identify the error in this Java code snippet:
      String s = "Java Programming";
      String part = s.substring(5, 3);
      System.out.println(part);
      medium
      A. substring() cannot be used on String objects
      B. Missing semicolon after substring() call
      C. substring() arguments are reversed; start index must be <= end index
      D. The variable 'part' is not declared

      Solution

      1. Step 1: Check substring() method argument rules

        substring(start, end) requires start index less than or equal to end index; here 5 > 3, which is invalid.
      2. Step 2: Confirm other syntax correctness

        Semicolons are present, substring() is valid on String, and 'part' is declared properly.
      3. Final Answer:

        substring() arguments are reversed; start index must be <= end index -> Option C
      4. Quick Check:

        substring(start, end) needs start ≤ end [OK]
      Hint: Start index must not be greater than end index in substring() [OK]
      Common Mistakes:
      • Swapping start and end indexes
      • Thinking substring() changes original string
      • Ignoring exception thrown at runtime
      5. Given the string input = "apple,banana,orange", which code snippet correctly replaces all commas with semicolons and converts the result to uppercase?
      hard
      A. input.replaceFirst(",", ";").toUpperCase();
      B. input.toUpperCase().replaceFirst(",", ";");
      C. input.replace(",", ";").toUpper();
      D. input.replaceAll(",", ";").toUpperCase();

      Solution

      1. Step 1: Understand replace() vs replaceAll()

        Both replace() and replaceAll() can replace characters, but replaceAll() uses regex and is safer for multiple replacements.
      2. Step 2: Check method chaining and correctness

        A and B use replaceFirst() which only replaces the first comma. C uses invalid toUpper().
      3. Step 3: Choose best practice

        Using replaceAll() with chaining to toUpperCase() is correct and clear.
      4. Final Answer:

        input.replaceAll(",", ";").toUpperCase(); -> Option D
      5. Quick Check:

        Replace commas then uppercase = replaceAll + toUpperCase() [OK]
      Hint: Chain replaceAll() then toUpperCase() for clean transformation [OK]
      Common Mistakes:
      • Using toUpper() instead of toUpperCase()
      • Using replaceFirst() which only replaces the first occurrence
      • Confusing replace() and replaceAll() usage
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard