0
0
Javaprogramming~15 mins

String immutability in Java - Practice Problems & Coding Challenges

Choose your learning style8 modes available
trophyChallenge - 5 Problems
🎖️
String Immutability Master
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 immutability?
Consider the following Java code snippet. What will be printed when it runs?
Java
public class Test {
    public static void main(String[] args) {
        String s = "hello";
        s.toUpperCase();
        System.out.println(s);
    }
}
AHELLO
BCompilation error
Cnull
Dhello
Attempts:
2 left
💻 code output
intermediate
2:00remaining
What does this code print after String concatenation?
Look at this Java code. What will be printed?
Java
public class Test {
    public static void main(String[] args) {
        String s = "cat";
        s += "dog";
        System.out.println(s);
    }
}
Adog
Bcat
Ccatdog
DCompilation error
Attempts:
2 left
🔧 debug
advanced
2:00remaining
Which option causes a compilation error due to String immutability?
Which of the following Java code snippets will cause a compilation error?
A
String s = "hello";
s.charAt(0) = 'H';
B
String s = "hello";
s = s.toUpperCase();
C
String s = "hello";
System.out.println(s.length());
D
String s = "hello";
String t = s + " world";
Attempts:
2 left
🧠 conceptual
advanced
2:00remaining
How does Java handle String immutability internally?
Which statement best describes how Java implements String immutability?
AJava uses a special String object that allows changes only through reflection.
BJava stores String characters in a final char array that cannot be changed after creation.
CJava copies the String every time it is passed to a method to prevent changes.
DJava uses a mutable char array internally but prevents changes by restricting access.
Attempts:
2 left
💻 code output
expert
2:00remaining
What is the output of this Java code involving String references?
Analyze the code below. What will be printed?
Java
public class Test {
    public static void main(String[] args) {
        String a = "abc";
        String b = a;
        a = a + "d";
        System.out.println(b);
    }
}
Aabc
Babcd
CCompilation error
Dnull
Attempts:
2 left