What is the simplest way to reverse a string using built-in Java methods?
Use StringBuilder or StringBuffer with the reverse() method. For example: new StringBuilder(str).reverse().toString().
Click to reveal answer
beginner
How can you reverse a string manually using a character array in Java?
Convert the string to a char array, then swap characters from start and end moving towards the center until all are reversed.
Click to reveal answer
beginner
What is the time complexity of reversing a string by swapping characters in place?
The time complexity is O(n), where n is the length of the string, because each character is visited once.
Click to reveal answer
intermediate
Why is using recursion to reverse a string less efficient in Java?
Recursion uses extra memory on the call stack for each character, which can lead to stack overflow for large strings and is less efficient than iterative methods.
Click to reveal answer
intermediate
Explain the difference between StringBuilder and manual reversal using a char array.
StringBuilder.reverse() is a built-in method that reverses the string efficiently and concisely. Manual reversal requires explicit swapping of characters in a char array, giving more control but needing more code.
Click to reveal answer
Which Java class provides a built-in method to reverse strings easily?
AStringBuilder
BString
CArrays
DCharacter
✗ Incorrect
StringBuilder has the reverse() method that reverses the string content.
What is the first step to reverse a string manually in Java?
AUse substring method
BConvert the string to a char array
CCall reverse() on the string
DUse recursion directly on the string
✗ Incorrect
You need to convert the string to a char array to swap characters manually.
What is the time complexity of reversing a string by swapping characters?
AO(1)
BO(n^2)
CO(log n)
DO(n)
✗ Incorrect
Each character is visited once, so the complexity is linear, O(n).
Why might recursion be a poor choice for reversing very long strings?
AIt is faster than iteration
BIt does not work on strings
CIt uses extra stack memory and can cause stack overflow
DIt changes the original string permanently
✗ Incorrect
Recursion uses stack frames for each call, risking stack overflow on large inputs.
Which method is more concise for reversing a string in Java?
AUsing StringBuilder.reverse()
BManually swapping characters in a char array
CUsing recursion
DUsing substring concatenation
✗ Incorrect
StringBuilder.reverse() is a single method call and concise.
Describe two different ways to reverse a string in Java and their pros and cons.
Think about built-in methods vs manual approaches and memory usage.
You got /3 concepts.
Explain why reversing a string by swapping characters has O(n) time complexity.
Consider how many times each character is processed.
You got /3 concepts.
Practice
(1/5)
1. Which of the following methods correctly reverses a string in Java using built-in functions?
easy
A. Using new StringBuilder(str).reverse().toString()
B. Using str.reverse()
C. Using str.toCharArray().reverse()
D. Using String.reverse(str)
Solution
Step 1: Understand Java String reversal methods
Java's String class does not have a reverse() method, but StringBuilder does.
Step 2: Identify correct usage of StringBuilder
Creating a StringBuilder from the string and calling reverse() then toString() returns the reversed string.
Final Answer:
Using new StringBuilder(str).reverse().toString() -> Option A
Quick Check:
StringBuilder.reverse() = correct reversal [OK]
Hint: Use StringBuilder's reverse() method for easy string reversal [OK]
Common Mistakes:
Trying to call reverse() directly on String
Using non-existent String.reverse() method
Assuming toCharArray() has reverse() method
2. Which of the following code snippets correctly reverses a string str using a for loop in Java?
easy
A. for (int i = 0; i < str.length(); i++) reversed += str.charAt(i);
B. for (int i = 1; i <= str.length(); i++) reversed += str.charAt(i);
C. for (int i = str.length() - 1; i >= 0; i--) reversed += str.charAt(i);
D. for (int i = str.length(); i > 0; i--) reversed += str.charAt(i);
Solution
Step 1: Understand string indexing in Java
String indices start at 0 and go to length()-1.
Step 2: Identify correct loop to reverse string
Loop from last index (length()-1) down to 0, appending characters to reversed string.
Final Answer:
for (int i = str.length() - 1; i >= 0; i--) reversed += str.charAt(i); -> Option C
Quick Check:
Loop from end to start = correct reversal [OK]
Hint: Loop backward from length()-1 to 0 to reverse string [OK]
Common Mistakes:
Looping forward instead of backward
Using 1-based indexing instead of 0-based
Accessing charAt with index equal to length()
3. What is the output of the following Java code?
String str = "hello";
String reversed = "";
for (int i = str.length() - 1; i >= 0; i--) {
reversed += str.charAt(i);
}
System.out.println(reversed);
medium
A. hello
B. olleh
C. olle
D. SyntaxError
Solution
Step 1: Trace the for loop from end to start
The loop starts at index 4 ('o') and goes down to 0 ('h'), appending each character to reversed.