0
0
Javaprogramming~15 mins

StringBuilder and StringBuffer in Java - Practice Problems & Coding Challenges

Choose your learning style8 modes available
trophyChallenge - 5 Problems
🎖️
StringBuilder and StringBuffer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 code output
intermediate
2:00remaining
Output 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());
A!dlroW olleH
BHello World
CHelloWorld!
DHello World!
Attempts:
2 left
💻 code output
intermediate
2:00remaining
Thread safety difference between StringBuilder and StringBuffer
What will happen if multiple threads modify a StringBuffer and a StringBuilder concurrently without synchronization?
AStringBuffer operations are thread-safe; StringBuilder operations may cause data corruption.
BNeither StringBuffer nor StringBuilder is thread-safe.
CBoth StringBuffer and StringBuilder are thread-safe.
DStringBuilder is thread-safe; StringBuffer is not.
Attempts:
2 left
🔧 debug
advanced
2:00remaining
Identify the error in StringBuilder usage
What error will the following code produce?
Java
StringBuilder sb = null;
sb.append("Test");
ANullPointerException
BStringIndexOutOfBoundsException
CNo error, prints 'Test'
DCompilation error
Attempts:
2 left
📝 syntax
advanced
2:00remaining
Correct way to convert StringBuilder to String
Which option correctly converts a StringBuilder object to a String?
Java
StringBuilder sb = new StringBuilder("example");
AString s = (String) sb;
BString s = sb.convert();
CString s = sb.toString();
DString s = sb.stringValue();
Attempts:
2 left
🚀 application
expert
3:00remaining
Predict 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());
AnigeB
BBegin
CBegin Middle
DStart Middle
Attempts:
2 left