Bird
Raised Fist0
Javaprogramming~10 mins

Common string methods in Java - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to get the length of the string.

Java
String text = "Hello";
int length = text.[1]();
Drag options to blanks, or click blank then click option'
Asize
BgetLength
Clength
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using size() instead of length()
Trying to access length as a property without parentheses
2fill in blank
medium

Complete the code to convert the string to uppercase letters.

Java
String word = "java";
String upper = word.[1]();
Drag options to blanks, or click blank then click option'
AtoLowerCase
BtoUpperCase
Cuppercase
DmakeUpper
Attempts:
3 left
💡 Hint
Common Mistakes
Using toLowerCase() which converts to lowercase
Trying to use a method named uppercase which does not exist
3fill in blank
hard

Fix the error in the code to check if the string starts with "Hi".

Java
String greeting = "Hi there!";
boolean starts = greeting.[1]("Hi");
Drag options to blanks, or click blank then click option'
AstartsWith
BbeginsWith
CstartWith
Dstarts
Attempts:
3 left
💡 Hint
Common Mistakes
Using startWith instead of startsWith
Using beginsWith which is not a Java String method
4fill in blank
hard

Fill both blanks to extract the substring "cat" from the word "concatenate".

Java
String word = "concatenate";
String part = word.[1](3, [2]);
Drag options to blanks, or click blank then click option'
Asubstring
BsubString
C9
D6
Attempts:
3 left
💡 Hint
Common Mistakes
Using subString with capital S which is incorrect
Using wrong end index causing wrong substring
5fill in blank
hard

Fill all three blanks to create a new string with trimmed spaces and replaced spaces with underscores.

Java
String messy = "  hello world  ";
String clean = messy.[1]().[2](" ", [3]);
Drag options to blanks, or click blank then click option'
Atrim
Breplace
C"_"
Dstrip
Attempts:
3 left
💡 Hint
Common Mistakes
Using strip() instead of trim() which is similar but less common
Forgetting to put quotes around the underscore character

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