0
0
Javaprogramming~15 mins

Why strings are special in Java - Challenge Your Understanding

Choose your learning style8 modes available
trophyChallenge - 5 Problems
🎖️
Java String Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 code 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
🧠 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
💻 code 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
🔧 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
🚀 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