Challenge - 5 Problems
String Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput 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()); } }
Attempts:
2 left
💡 Hint
Remember substring(start, end) includes start but excludes end index.
✗ Incorrect
The substring from index 2 to 7 is 'lloWo'. The length of 'HelloWorld' is 10.
❓ Predict Output
intermediateResult 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()); } }
Attempts:
2 left
💡 Hint
trim() removes spaces at start and end, toUpperCase() converts all letters to uppercase.
✗ Incorrect
trim() removes leading and trailing spaces, then toUpperCase() converts all letters to uppercase.
❓ Predict Output
advancedOutput 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")); } }
Attempts:
2 left
💡 Hint
replace(char oldChar, char newChar) replaces all occurrences of oldChar with newChar.
✗ Incorrect
Replacing 'p' with 'b' in 'apple' results in 'abble'. equals() compares strings for exact match.
❓ Predict Output
advancedOutput 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); } }
Attempts:
2 left
💡 Hint
indexOf('g') returns the first position of 'g'. charAt gets character at given index.
✗ Incorrect
The first 'g' is at index 3. charAt(4) is 'r'.
❓ Predict Output
expertFinal 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); } }
Attempts:
2 left
💡 Hint
trim() removes spaces, replaceAll("\\d", "") removes digits, toLowerCase() converts to lowercase.
✗ Incorrect
After trimming spaces, digits are removed, and all letters become lowercase with spaces preserved.
