Challenge - 5 Problems
StringBuilder and StringBuffer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of StringBuilder append operations
What is the output of the following Java code snippet?
Java
StringBuilder sb = new StringBuilder("Hello"); sb.append(" World"); sb.append('!'); System.out.println(sb.toString());
Attempts:
2 left
💡 Hint
Remember that append adds to the end of the existing string.
✗ Incorrect
The StringBuilder starts with "Hello". Then " World" is appended, followed by '!'. The final string is "Hello World!".
❓ Predict Output
intermediateThread safety difference between StringBuilder and StringBuffer
What will happen if multiple threads modify a StringBuffer and a StringBuilder concurrently without synchronization?
Attempts:
2 left
💡 Hint
Consider which class was designed for synchronization.
✗ Incorrect
StringBuffer methods are synchronized, making it thread-safe. StringBuilder is not synchronized and can cause data corruption if used by multiple threads concurrently.
🔧 Debug
advancedIdentify the error in StringBuilder usage
What error will the following code produce?
Java
StringBuilder sb = null; sb.append("Test");
Attempts:
2 left
💡 Hint
Check if the StringBuilder object is initialized before use.
✗ Incorrect
The StringBuilder reference is null, so calling append on it causes a NullPointerException at runtime.
📝 Syntax
advancedCorrect way to convert StringBuilder to String
Which option correctly converts a StringBuilder object to a String?
Java
StringBuilder sb = new StringBuilder("example");
Attempts:
2 left
💡 Hint
Look for the standard method to get a String from StringBuilder.
✗ Incorrect
The toString() method returns the string representation of the StringBuilder content. Other methods do not exist or cause errors.
🚀 Application
expertPredict the final content after multiple StringBuffer operations
Given the following code, what is the final output?
Java
StringBuffer sb = new StringBuffer("Start"); sb.insert(5, " Middle"); sb.replace(0, 5, "Begin"); sb.delete(6, 13); System.out.println(sb.toString());
Attempts:
2 left
💡 Hint
Follow each operation step-by-step on the string.
✗ Incorrect
Initial: "Start"
After insert(5, " Middle"): "Start Middle"
After replace(0,5,"Begin"): "Begin Middle"
After delete(6,13): deletes from index 6 to 12 (7 chars), removing "Middle"
Result: "Begin " (with trailing space)
