Challenge - 5 Problems
Java String Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 code output
intermediate2: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); } }
Attempts:
2 left
🧠 conceptual
intermediate1:30remaining
Why are String objects immutable in Java?
Why did Java designers make String objects immutable?
Attempts:
2 left
💻 code output
advanced2: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()); } }
Attempts:
2 left
🔧 debug
advanced1: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"); } } }
Attempts:
2 left
🚀 application
expert2: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()); } }
Attempts:
2 left
