String Exercises

Test Your Understanding of Strings in Java

1
String Immutability
Immutability
What is the output of this code?
String s = "Hello"; s.toUpperCase(); System.out.println(s);
A HELLO
B Hello
C hello
D Compilation error
Explanation

toUpperCase() returns a new String but doesn't modify the original. Since we don't assign the result to anything, the original String s remains unchanged. This demonstrates String immutability.

2
String Pool
Interning
What is the output?
String s1 = "Java"; String s2 = "Java"; System.out.println(s1 == s2);
A true
B false
C Compilation error
D Runtime error
Explanation

String literals are automatically interned in the String Pool. Both s1 and s2 point to the same object in the pool, so == returns true.

3
new String()
Interning
What is the output?
String s1 = "Java"; String s2 = new String("Java"); System.out.println(s1 == s2);
A true
B false
C Compilation error
D Runtime error
Explanation

new String() always creates a new object in the heap, not in the String Pool. So s1 and s2 are different objects with different addresses, making == return false.

4
equals() Method
Comparison
What is the output?
String s1 = "Java"; String s2 = new String("Java"); System.out.println(s1.equals(s2));
A false
B Compilation error
C true
D Runtime error
Explanation

.equals() compares the content of the Strings, not the addresses. Both contain "Java", so it returns true. Always use .equals() for String content comparison!

5
substring()
Methods
What is the output?
String s = "Hello World"; System.out.println(s.substring(0, 5));
A Hello (with space)
B Hello
C ello
D World
Explanation

substring(0, 5) returns characters from index 0 to index 4 (5 is exclusive). That's "Hello" (indices 0,1,2,3,4). The space is at index 5 and is not included.

6
indexOf()
Methods
What is the output?
String s = "programming"; System.out.println(s.indexOf('g'));
A 3
B 10
C 4
D -1
Explanation

indexOf() returns the index of the first occurrence. In "programming": p(0), r(1), o(2), g(3)... The first 'g' is at index 3.

7
StringBuilder vs String
StringBuilder
Which class should you use for frequent String modifications in a single-threaded application?
A String
B StringBuffer
C StringBuilder
D CharSequence
Explanation

StringBuilder is the best choice for frequent modifications in a single-threaded application. It's mutable (no new objects) and faster than StringBuffer (no synchronization overhead).

8
Thread Safety
StringBuffer
Which statement is TRUE about StringBuffer?
A StringBuffer is immutable
B StringBuffer is thread-safe (synchronized)
C StringBuffer is faster than StringBuilder
D StringBuffer stores strings in the String Pool
Explanation

StringBuffer is thread-safe because its methods are synchronized. This means only one thread can access it at a time, preventing data corruption. However, this makes it slightly slower than StringBuilder.

9
StringBuilder Methods
StringBuilder
What is the output?
StringBuilder sb = new StringBuilder("Hello"); sb.append(" World"); sb.reverse(); System.out.println(sb);
A Hello World
B olleH
C World Hello
D dlroW olleH
Explanation

First, append(" World") makes it "Hello World". Then reverse() reverses the entire string to "dlroW olleH". StringBuilder modifies in place!

10
Loop Concatenation
Performance
Why is this code inefficient?
String result = ""; for (int i = 0; i < 1000; i++) { result = result + i; }
A The loop runs too many times
B Integer addition is slow
C Each concatenation creates a new String object
D The initial String is empty
Explanation

Because String is immutable, each + operation creates a new String object. With 1000 iterations, that's ~1000 new objects created and discarded! Use StringBuilder instead for much better performance.