Bird
Raised Fist0
Javaprogramming~20 mins

Common string methods in Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
String Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of substring and length methods
What is the output of the following Java code?
Java
public class Main {
    public static void main(String[] args) {
        String s = "HelloWorld";
        System.out.println(s.substring(2, 7) + " " + s.length());
    }
}
AlloWo 9
BlloWo 10
CloWor 10
DloWor 9
Attempts:
2 left
💡 Hint
Remember substring(start, end) includes start but excludes end index.
❓ Predict Output
intermediate
2:00remaining
Result of toUpperCase and trim methods
What will be printed by this Java program?
Java
public class Main {
    public static void main(String[] args) {
        String s = "  java Programming  ";
        System.out.println(s.trim().toUpperCase());
    }
}
AJAVA PROGRAMMING
BGNIMMARGORP AVAJ
Cjava programming
DAVA PROGRAMMING
Attempts:
2 left
💡 Hint
trim() removes spaces at start and end, toUpperCase() converts all letters to uppercase.
❓ Predict Output
advanced
2:00remaining
Output of replace and equals methods
What is the output of this Java code snippet?
Java
public class Main {
    public static void main(String[] args) {
        String s1 = "apple";
        String s2 = s1.replace('p', 'b');
        System.out.println(s2.equals("abble"));
    }
}
Atrue
Bfalse
CCompilation error
DRuntime exception
Attempts:
2 left
💡 Hint
replace(char oldChar, char newChar) replaces all occurrences of oldChar with newChar.
❓ Predict Output
advanced
2:00remaining
Output of indexOf and charAt methods
What will this Java program print?
Java
public class Main {
    public static void main(String[] args) {
        String s = "Programming";
        int index = s.indexOf('g');
        char c = s.charAt(index + 1);
        System.out.println(c);
    }
}
Ai
Bm
Cr
Da
Attempts:
2 left
💡 Hint
indexOf('g') returns the first position of 'g'. charAt gets character at given index.
❓ Predict Output
expert
3:00remaining
Final value of string after chained methods
What is the final output of this Java code?
Java
public class Main {
    public static void main(String[] args) {
        String s = "  Java123 Programming456  ";
        s = s.trim().replaceAll("\\d", "").toLowerCase();
        System.out.println(s);
    }
}
Ajavaprogramming
Bjava123 programming456
Cgnimmargorp avaj
Djava programming
Attempts:
2 left
💡 Hint
trim() removes spaces, replaceAll("\\d", "") removes digits, toLowerCase() converts to lowercase.

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