Challenge - 5 Problems
String Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of String Concatenation and Memory
What is the output of the following Java code snippet?
DSA Java
String a = "Hello"; String b = "World"; String c = a + b; System.out.println(c);
Attempts:
2 left
💡 Hint
Remember that + operator concatenates strings without adding spaces automatically.
✗ Incorrect
The + operator joins the two strings exactly as they are, so "Hello" + "World" becomes "HelloWorld" without spaces.
❓ Predict Output
intermediateString Immutability Effect
What will be printed after running this Java code?
DSA Java
String s = "abc";
s.toUpperCase();
System.out.println(s);Attempts:
2 left
💡 Hint
Strings in Java cannot be changed once created.
✗ Incorrect
The toUpperCase() method returns a new string but does not change the original string s.
🔧 Debug
advancedWhy does this String comparison fail?
Consider this Java code snippet. Why does it print "Not equal"?
DSA Java
String s1 = new String("test"); String s2 = new String("test"); if (s1 == s2) { System.out.println("Equal"); } else { System.out.println("Not equal"); }
Attempts:
2 left
💡 Hint
Use .equals() to compare string content in Java.
✗ Incorrect
The == operator checks if both variables point to the same object in memory. Here, s1 and s2 are different objects with the same content.
🧠 Conceptual
advancedString Pool Behavior
Which statement about Java String pool is true?
Attempts:
2 left
💡 Hint
Think about how Java optimizes memory for string literals.
✗ Incorrect
Java stores string literals in a special pool to reuse them and save memory. Strings created with new keyword are not automatically pooled.
❓ Predict Output
expertOutput of String Interning
What is the output of this Java code snippet?
DSA Java
String s1 = new String("hello"); String s2 = s1.intern(); String s3 = "hello"; System.out.println(s1 == s2); System.out.println(s2 == s3);
Attempts:
2 left
💡 Hint
intern() returns a reference from the String pool.
✗ Incorrect
s1 is a new object, so s1 == s2 is false. s2 and s3 both refer to the pooled string literal, so s2 == s3 is true.
