StringBuilder is a class in Java used to create mutable (changeable) sequences of characters. It is faster than StringBuffer but not thread-safe.
Click to reveal answer
beginner
What is StringBuffer in Java?
StringBuffer is a class in Java that creates mutable sequences of characters and is thread-safe, meaning it can be safely used by multiple threads at the same time.
Click to reveal answer
intermediate
Why is StringBuilder faster than StringBuffer?
StringBuilder is faster because it is not synchronized, so it does not have the overhead of thread safety mechanisms that StringBuffer has.
Click to reveal answer
beginner
Which class should you use if you need to modify strings in a single-threaded environment?
Use StringBuilder because it is faster and thread safety is not needed in single-threaded environments.
Click to reveal answer
beginner
Give an example of appending text using StringBuilder.
Example: StringBuilder sb = new StringBuilder("Hello"); sb.append(" World"); System.out.println(sb.toString()); // Output: Hello World
Click to reveal answer
Which class is synchronized and thread-safe?
AString
BStringBuilder
CStringBuffer
DStringTokenizer
✗ Incorrect
StringBuffer is synchronized and thread-safe, while StringBuilder is not.
Which class is generally faster for string manipulation in a single-threaded program?
AStringBuilder
BStringBuffer
CString
DStringTokenizer
✗ Incorrect
StringBuilder is faster because it does not have synchronization overhead.
What does the append() method do in StringBuilder?
ADeletes characters
BConverts to String
CReplaces characters
DAdds characters to the end
✗ Incorrect
append() adds characters to the end of the current sequence.
Which of these is immutable in Java?
AString
BStringBuffer
CStringBuilder
DStringWriter
✗ Incorrect
String objects are immutable; their content cannot be changed after creation.
If multiple threads modify the same string buffer, which class should be used?
AStringBuilder
BStringBuffer
CString
DStringTokenizer
✗ Incorrect
StringBuffer is synchronized and safe for use by multiple threads.
Explain the difference between StringBuilder and StringBuffer in Java.
Think about when you need synchronization and when you don't.
You got /3 concepts.
Describe a situation where you would prefer StringBuilder over StringBuffer.
Consider performance in a simple program.
You got /3 concepts.
Practice
(1/5)
1.
Which of the following statements about StringBuilder and StringBuffer is true?
easy
A. StringBuffer is faster and not thread-safe, while StringBuilder is thread-safe but slower.
B. StringBuilder is faster but not thread-safe, while StringBuffer is thread-safe but slower.
C. Both StringBuilder and StringBuffer are thread-safe and have the same speed.
D. StringBuilder and StringBuffer cannot be used to modify strings.
Solution
Step 1: Understand thread safety difference
StringBuffer is synchronized, making it thread-safe but slower.
Step 2: Understand performance difference
StringBuilder is not synchronized, so it is faster but not thread-safe.
Final Answer:
StringBuilder is faster but not thread-safe, while StringBuffer is thread-safe but slower. -> Option B
Quick Check:
Thread safety and speed = A [OK]
Hint: Remember: Buffer is safe but slower; Builder is fast but unsafe [OK]
Common Mistakes:
Confusing which class is thread-safe
Thinking both have same speed
Believing they cannot modify strings
2.
Which of the following is the correct way to create a StringBuilder object with initial content "Hello"?
easy
A. StringBuilder sb = new StringBuffer("Hello");
B. StringBuilder sb = StringBuilder("Hello");
C. StringBuilder sb = new StringBuilder(); sb.append("Hello");
D. StringBuilder sb = new StringBuilder("Hello");
Solution
Step 1: Check constructor syntax
The correct way to create a StringBuilder with initial content is using new StringBuilder("Hello").
Step 2: Verify other options
StringBuilder sb = StringBuilder("Hello"); misses new, StringBuilder sb = new StringBuilder(); sb.append("Hello"); is valid but not direct initialization, StringBuilder sb = new StringBuffer("Hello"); uses wrong class.
Final Answer:
StringBuilder sb = new StringBuilder("Hello"); -> Option D
Quick Check:
Correct constructor syntax = D [OK]
Hint: Use 'new' keyword and constructor with string inside parentheses [OK]
Step 2: Replace characters from index 0 to 4 with "Python"
Characters "Java" (index 0 to 4) are replaced by "Python", resulting in "Python Programming".
Final Answer:
Python Programming -> Option C
Quick Check:
Append + replace result = A [OK]
Hint: Replace changes substring; append adds at end [OK]
Common Mistakes:
Confusing replace indices
Thinking append replaces text
Ignoring toString() output
4.
Identify the error in the following code snippet:
StringBuffer sb = new StringBuffer("Test");
sb.insert(5, "ing");
System.out.println(sb);
medium
A. IndexOutOfBoundsException because insert index is invalid
B. NullPointerException because sb is not initialized
C. Compilation error due to wrong method name
D. No error; output is "Testing"
Solution
Step 1: Check insert index validity
StringBuffer length is 4 ("Test"), but insert index is 5, which is out of bounds.
Step 2: Understand exception thrown
Inserting at index greater than length throws IndexOutOfBoundsException.
Final Answer:
IndexOutOfBoundsException because insert index is invalid -> Option A
Quick Check:
Invalid insert index = B [OK]
Hint: Insert index must be between 0 and length inclusive [OK]
Common Mistakes:
Assuming insert auto-corrects index
Confusing insert with append
Expecting no error on invalid index
5.
You want to build a thread-safe string that appends numbers 1 to 3 separated by commas, resulting in "1,2,3". Which code snippet correctly uses StringBuffer to achieve this?
1. StringBuffer sb = new StringBuffer();
for (int i = 1; i <= 3; i++) {
sb.append(i);
if (i != 3) sb.append(",");
}
System.out.println(sb.toString());
2. StringBuffer sb = new StringBuffer();
for (int i = 1; i <= 3; i++) {
sb.append(i + ",");
}
System.out.println(sb.toString());
3. StringBuffer sb = new StringBuffer();
sb.append("1,2,3,");
System.out.println(sb.toString());
4. StringBuffer sb = new StringBuffer();
for (int i = 1; i < 3; i++) {
sb.append(i);
sb.append(",");
}
System.out.println(sb.toString());
hard
A. Code snippet 1
B. Code snippet 2
C. Code snippet 3
D. Code snippet 4
Solution
Step 1: Analyze snippet 1
Appends numbers 1 to 3 with commas except after last number, producing "1,2,3" correctly.
Step 2: Check other snippets
Snippet 2 adds extra comma at end; snippet 3 hardcodes string but adds extra comma; snippet 4 misses last number 3.
Final Answer:
Code snippet 1 -> Option A
Quick Check:
Correct comma placement in loop = C [OK]
Hint: Add comma only if not last element in loop [OK]
Common Mistakes:
Adding extra comma at end
Missing last number in loop
Hardcoding string without loop
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.