Java Strings & String Methods Quiz
35 in-depth questions covering String immutability, object creation, comparison behavior, common methods, StringBuilder/StringBuffer usage, and performance implications in real-world Java code.
Question 1
How many String objects are created by this single line?
String s = "Java";Question 2
What is the primary reason String is immutable in Java?
Question 3
What is the output?
String a = "hello";
String b = "hello";
System.out.println(a == b);Question 4
What does s.intern() do when called on a String?
Question 5
What is printed?
String x = new String("test");
String y = new String("test");
System.out.println(x == y);Question 6
Which method should be used to compare String contents?
Question 7
What is the result?
String s = "Java";
s = s.concat("17");
System.out.println(s);Question 8
Why can String be used safely as a key in HashMap?
Question 9
What is the output?
StringBuilder sb = new StringBuilder("abc");
sb.append("def").reverse().delete(1, 3);
System.out.println(sb);Question 10
What happens when you do this in a loop a million times?
String result = "";
for (int i = 0; i < 1_000_000; i++) {
result += i;
}Question 11
What is the output?
String s1 = "hello";
String s2 = s1.substring(1, 4);
System.out.println(s2);Question 12
What is the main difference between StringBuilder and StringBuffer?
Question 13
What is printed?
String s = "Java Duke";
int len = s.length();
char first = s.charAt(0);
char last = s.charAt(len - 1);
System.out.println(first + " " + last);Question 14
What is the result?
String a = "abc";
String b = a.toUpperCase();
String c = b.toLowerCase();
System.out.println(a == c);Question 15
What is the output?
StringBuilder sb1 = new StringBuilder("hello");
StringBuilder sb2 = new StringBuilder("hello");
System.out.println(sb1.equals(sb2));Question 16
Which of these methods can change the length of a String object?
Question 17
What is the output?
String s = null;
System.out.println(s + "world");Question 18
What is the result of this optimization attempt?
String result = "";
for (int i = 0; i < 1000; i++) {
result = result.concat(String.valueOf(i));
}Question 19
What is printed?
String s = " Java ";
System.out.println("'" + s.strip() + "'");Question 20
Why does this code compile and run without error?
String s = "hello";
s = null;
s.length();Question 21
What is the output?
String lang = "Java";
lang += lang.length();
System.out.println(lang);Question 22
When should you prefer StringBuffer over StringBuilder?
Question 23
What is the result?
String s = "abc";
String t = s;
s = s.toUpperCase();
System.out.println(t);Question 24
What is printed?
StringBuilder sb = new StringBuilder(100);
System.out.println(sb.capacity() + " " + sb.length());Question 25
Which statement about the string constant pool is correct?
Question 26
What is the output?
String s = "Hello World";
System.out.println(s.substring(6));Question 27
What happens when this code runs?
StringBuilder sb = new StringBuilder("abc");
sb.setLength(10);
System.out.println("'" + sb + "'");Question 28
Why does "hello".equals("hello") return true even if the objects are different?
Question 29
What is the output?
String s = String.join(", ", "Java", "Kotlin", "Scala");
System.out.println(s);Question 30
What is printed?
String s = "abc";
s = s.replace('b', 'x');
System.out.println(s);Question 31
Which of these is the most efficient way to concatenate 100 strings in a loop?
Question 32
What is the result?
String s = "Java";
String t = "JaVa";
System.out.println(s.equalsIgnoreCase(t));Question 33
What is printed?
StringBuilder sb = new StringBuilder("Java");
sb.insert(4, "17");
System.out.println(sb);Question 34
Why is this considered bad practice?
public String getName() {
return "John " + getFirst() + " " + getLast();
}Question 35
After completing this quiz, what is the single most important lesson about String handling in Java?
