Bird
Raised Fist0
Javaprogramming~20 mins

Why strings are special in Java - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Java String Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Java code involving String comparison?

Consider the following Java code snippet:

public class Test {
    public static void main(String[] args) {
        String a = "hello";
        String b = "hello";
        String c = new String("hello");
        System.out.println(a == b);
        System.out.println(a == c);
    }
}

What will be the output when this code runs?

Java
public class Test {
    public static void main(String[] args) {
        String a = "hello";
        String b = "hello";
        String c = new String("hello");
        System.out.println(a == b);
        System.out.println(a == c);
    }
}
A
false
false
B
true
false
C
true
true
D
false
true
Attempts:
2 left
💡 Hint

Remember that == compares references, not content, and string literals are interned.

🧠 Conceptual
intermediate
1:30remaining
Why are String objects immutable in Java?

Why did Java designers make String objects immutable?

ATo allow strings to be safely shared and reused without changes
BTo make strings faster to modify in place
CTo prevent strings from being used as keys in collections
DTo allow strings to be garbage collected immediately after use
Attempts:
2 left
💡 Hint

Think about safety and sharing in multi-threaded programs.

❓ Predict Output
advanced
2:00remaining
What is the output of this code using StringBuilder and String?

Examine this Java code:

public class Test {
    public static void main(String[] args) {
        String s = "abc";
        StringBuilder sb = new StringBuilder(s);
        sb.append("def");
        System.out.println(s);
        System.out.println(sb.toString());
    }
}

What will be printed?

Java
public class Test {
    public static void main(String[] args) {
        String s = "abc";
        StringBuilder sb = new StringBuilder(s);
        sb.append("def");
        System.out.println(s);
        System.out.println(sb.toString());
    }
}
A
abc
abcdef
B
abcdef
abcdef
C
abc
abc
D
abcdef
abc
Attempts:
2 left
💡 Hint

Remember that String is immutable but StringBuilder can change.

🔧 Debug
advanced
1:30remaining
What error does this code cause?

Look at this Java code snippet:

public class Test {
    public static void main(String[] args) {
        String s = null;
        if (s.equals("test")) {
            System.out.println("Match");
        } else {
            System.out.println("No match");
        }
    }
}

What happens when you run this code?

Java
public class Test {
    public static void main(String[] args) {
        String s = null;
        if (s.equals("test")) {
            System.out.println("Match");
        } else {
            System.out.println("No match");
        }
    }
}
APrints "Match"
BPrints "No match"
CNullPointerException
DCompilation error
Attempts:
2 left
💡 Hint

Think about calling a method on a null reference.

🚀 Application
expert
2:30remaining
How many unique strings are in this array after interning?

Consider this Java code:

public class Test {
    public static void main(String[] args) {
        String[] arr = new String[] {"cat", new String("cat"), "dog", new String("dog")};
        for (int i = 0; i < arr.length; i++) {
            arr[i] = arr[i].intern();
        }
        java.util.Set set = new java.util.HashSet<>();
        for (String s : arr) {
            set.add(s);
        }
        System.out.println(set.size());
    }
}

What number will be printed?

Java
public class Test {
    public static void main(String[] args) {
        String[] arr = new String[] {"cat", new String("cat"), "dog", new String("dog")};
        for (int i = 0; i < arr.length; i++) {
            arr[i] = arr[i].intern();
        }
        java.util.Set<String> set = new java.util.HashSet<>();
        for (String s : arr) {
            set.add(s);
        }
        System.out.println(set.size());
    }
}
A1
B4
C3
D2
Attempts:
2 left
💡 Hint

Remember what intern() does to strings.

Practice

(1/5)
1. Why are strings considered special objects in Java compared to other objects?
easy
A. Because Java stores string literals in a special pool to save memory
B. Because strings can only store numbers
C. Because strings are primitive data types
D. Because strings cannot be compared using methods

Solution

  1. Step 1: Understand string storage in Java

    Java stores string literals in a special memory area called the string pool to reuse them and save memory.
  2. Step 2: Compare with other objects

    Other objects do not have this special pool and are stored separately each time they are created.
  3. Final Answer:

    Because Java stores string literals in a special pool to save memory -> Option A
  4. Quick Check:

    String pool = special memory reuse [OK]
Hint: Remember: string literals share memory in a pool [OK]
Common Mistakes:
  • Thinking strings are primitive types
  • Believing strings only store numbers
  • Assuming strings cannot be compared
2. Which of the following is the correct way to create a string literal in Java?
easy
A. String s = new String('hello');
B. String s = "hello";
C. String s = 'hello';
D. String s = hello;

Solution

  1. Step 1: Identify correct string literal syntax

    In Java, string literals must be enclosed in double quotes, like "hello".
  2. Step 2: Check other options for errors

    String s = new String('hello'); uses single quotes which are for chars, String s = 'hello'; uses single quotes incorrectly, String s = hello; lacks quotes entirely.
  3. Final Answer:

    String s = "hello"; -> Option B
  4. Quick Check:

    Strings use double quotes "" [OK]
Hint: Use double quotes for strings, single for chars [OK]
Common Mistakes:
  • Using single quotes for strings
  • Omitting quotes around text
  • Confusing char and string syntax
3. What will be the output of the following Java code?
String a = "hello";
String b = "hello";
System.out.println(a == b);
medium
A. false
B. Runtime exception
C. Compilation error
D. true

Solution

  1. Step 1: Understand string literal pooling

    Both variables a and b refer to the same string literal "hello" in the string pool, so they point to the same object.
  2. Step 2: Evaluate '==' operator on strings

    The '==' operator compares references, and since both refer to the same object, it returns true.
  3. Final Answer:

    true -> Option D
  4. Quick Check:

    Same literal references = true [OK]
Hint: == true if both strings share the same pool object [OK]
Common Mistakes:
  • Assuming '==' compares string content
  • Expecting false because of separate variables
  • Confusing '==' with equals() method
4. Identify the error in the following code snippet:
String s1 = new String("test");
String s2 = new String("test");
if (s1 == s2) {
System.out.println("Equal");
} else {
System.out.println("Not Equal");
}
medium
A. Using '==' to compare string contents instead of equals()
B. Missing semicolon after string declarations
C. Incorrect string initialization syntax
D. Cannot create strings with new keyword

Solution

  1. Step 1: Understand '==' vs equals() for strings

    '==' compares references, not content. Since s1 and s2 are different objects, '==' returns false.
  2. Step 2: Correct way to compare string content

    Use s1.equals(s2) to compare the actual text inside the strings.
  3. Final Answer:

    Using '==' to compare string contents instead of equals() -> Option A
  4. Quick Check:

    Use equals() for string content comparison [OK]
Hint: Use equals() to compare string text, not '==' [OK]
Common Mistakes:
  • Using '==' to compare string values
  • Thinking new String() creates same object
  • Ignoring string pool behavior
5. Given the code below, what will be the output?
String s1 = "Java";
String s2 = new String("Java");
String s3 = s2.intern();
System.out.println(s1 == s2);
System.out.println(s1 == s3);

Why does this happen?
hard
A. false false - all are different objects
B. true false - new String creates pooled string
C. false true - intern() returns the pooled string reference
D. true true - all strings are pooled automatically

Solution

  1. Step 1: Analyze s1 and s2 references

    s1 is a string literal in the pool, s2 is a new String object outside the pool, so s1 == s2 is false.
  2. Step 2: Understand intern() method effect

    s3 = s2.intern() returns the reference from the string pool for "Java", which is the same as s1, so s1 == s3 is true.
  3. Final Answer:

    false true - intern() returns the pooled string reference -> Option C
  4. Quick Check:

    intern() returns pool reference [OK]
Hint: intern() returns pooled string reference [OK]
Common Mistakes:
  • Assuming new String() returns pooled string
  • Thinking '==' compares string content
  • Ignoring intern() method purpose
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard