Java Strings & String Methods Quiz

Java
0 Passed
0% acceptance

35 in-depth questions covering String immutability, object creation, comparison behavior, common methods, StringBuilder/StringBuffer usage, and performance implications in real-world Java code.

35 Questions
~70 minutes
1

Question 1

How many String objects are created by this single line?

java
String s = "Java";
A
One object in the string constant pool
B
Two objects (one in pool, one on heap)
C
Zero objects
D
Depends on JVM version
2

Question 2

What is the primary reason String is immutable in Java?

A
Security, thread-safety, and ability to cache hash codes
B
Performance only
C
To reduce memory usage
D
Historical accident
3

Question 3

What is the output?

java
String a = "hello";
String b = "hello";
System.out.println(a == b);
A
true
B
false
C
Compilation error
D
Runtime exception
4

Question 4

What does s.intern() do when called on a String?

A
Returns a canonical representation from the string pool
B
Creates a new copy on the heap
C
Modifies the original String
D
Has no effect since Java 7
5

Question 5

What is printed?

java
String x = new String("test");
String y = new String("test");
System.out.println(x == y);
A
false
B
true
C
Compilation error
D
Undefined behavior
6

Question 6

Which method should be used to compare String contents?

A
equals()
B
==
C
compareTo()
D
contentEquals()
7

Question 7

What is the result?

java
String s = "Java";
s = s.concat("17");
System.out.println(s);
A
Java17
B
Java
C
null
D
Compilation error
8

Question 8

Why can String be used safely as a key in HashMap?

A
Because it is immutable and its hashCode is cached
B
Because it overrides equals() only
C
Because it is final
D
Because it implements Serializable
9

Question 9

What is the output?

java
StringBuilder sb = new StringBuilder("abc");
sb.append("def").reverse().delete(1, 3);
System.out.println(sb);
A
fedcba
B
fecba
C
fedba
D
abc
10

Question 10

What happens when you do this in a loop a million times?

java
String result = "";
for (int i = 0; i < 1_000_000; i++) {
    result += i;
}
A
Very poor performance due to creating millions of temporary String objects
B
Fast because the compiler optimizes it
C
OutOfMemoryError immediately
D
Same speed as StringBuilder
11

Question 11

What is the output?

java
String s1 = "hello";
String s2 = s1.substring(1, 4);
System.out.println(s2);
A
ell
B
ello
C
hel
D
llo
12

Question 12

What is the main difference between StringBuilder and StringBuffer?

A
StringBuffer is synchronized, StringBuilder is not
B
StringBuilder is immutable
C
StringBuffer is faster
D
No difference since Java 5
13

Question 13

What is printed?

java
String s = "Java Duke";
int len = s.length();
char first = s.charAt(0);
char last = s.charAt(len - 1);
System.out.println(first + " " + last);
A
J e
B
74 101
C
J Duke
D
Exception
14

Question 14

What is the result?

java
String a = "abc";
String b = a.toUpperCase();
String c = b.toLowerCase();
System.out.println(a == c);
A
true
B
false
C
Compilation error
D
Depends on locale
15

Question 15

What is the output?

java
StringBuilder sb1 = new StringBuilder("hello");
StringBuilder sb2 = new StringBuilder("hello");
System.out.println(sb1.equals(sb2));
A
false
B
true
C
Compilation error
D
Runtime exception
16

Question 16

Which of these methods can change the length of a String object?

A
None of them
B
trim()
C
replace()
D
substring()
17

Question 17

What is the output?

java
String s = null;
System.out.println(s + "world");
A
nullworld
B
world
C
NullPointerException
D
Compilation error
18

Question 18

What is the result of this optimization attempt?

java
String result = "";
for (int i = 0; i < 1000; i++) {
    result = result.concat(String.valueOf(i));
}
A
Still poor performance because concat creates new objects each iteration
B
Fast because concat is optimized
C
Better than += but still not ideal
D
Same as StringBuilder
19

Question 19

What is printed?

java
String s = "  Java  ";
System.out.println("'" + s.strip() + "'");
A
'Java'
B
' Java '
C
'Java '
D
Compilation error (pre-Java 11)
20

Question 20

Why does this code compile and run without error?

java
String s = "hello";
s = null;
s.length();
A
It does NOT compile
B
It compiles but throws NullPointerException at runtime
C
null strings have length 0
D
The JVM handles null automatically
21

Question 21

What is the output?

java
String lang = "Java";
lang += lang.length();
System.out.println(lang);
A
Java4
B
JavaJava
C
44
D
Compilation error
22

Question 22

When should you prefer StringBuffer over StringBuilder?

A
When the instance is shared between multiple threads
B
When maximum performance is needed
C
When working with very large strings
D
Never — StringBuffer is deprecated
23

Question 23

What is the result?

java
String s = "abc";
String t = s;
s = s.toUpperCase();
System.out.println(t);
A
abc
B
ABC
C
null
D
Runtime exception
24

Question 24

What is printed?

java
StringBuilder sb = new StringBuilder(100);
System.out.println(sb.capacity() + " " + sb.length());
A
100 0
B
16 0
C
100 100
D
0 0
25

Question 25

Which statement about the string constant pool is correct?

A
It is part of the heap and stores canonical String instances
B
It is outside the heap
C
It was removed in Java 7
D
It only stores compile-time constants
26

Question 26

What is the output?

java
String s = "Hello World";
System.out.println(s.substring(6));
A
World
B
World
C
Hello
D
Exception
27

Question 27

What happens when this code runs?

java
StringBuilder sb = new StringBuilder("abc");
sb.setLength(10);
System.out.println("'" + sb + "'");
A
'abc'
B
'abc '
C
'abcdefghij'
D
Exception
28

Question 28

Why does "hello".equals("hello") return true even if the objects are different?

A
Because equals() in String compares character content, not references
B
Because they are the same pooled object
C
Because == is used internally
D
Because of compiler optimization
29

Question 29

What is the output?

java
String s = String.join(", ", "Java", "Kotlin", "Scala");
System.out.println(s);
A
Java, Kotlin, Scala
B
JavaKotlinScala
C
Java , Kotlin , Scala
D
Compilation error
30

Question 30

What is printed?

java
String s = "abc";
s = s.replace('b', 'x');
System.out.println(s);
A
axc
B
abc
C
axcaxc
D
Exception
31

Question 31

Which of these is the most efficient way to concatenate 100 strings in a loop?

A
Use StringBuilder with explicit capacity
B
Use String concat()
C
Use String += operator
D
Use String.format()
32

Question 32

What is the result?

java
String s = "Java";
String t = "JaVa";
System.out.println(s.equalsIgnoreCase(t));
A
true
B
false
C
Compilation error
D
Runtime exception
33

Question 33

What is printed?

java
StringBuilder sb = new StringBuilder("Java");
sb.insert(4, "17");
System.out.println(sb);
A
Java17
B
Jav17a
C
Java17a
D
Exception
34

Question 34

Why is this considered bad practice?

java
public String getName() {
    return "John " + getFirst() + " " + getLast();
}
A
It creates multiple temporary String objects
B
It is slower than direct concatenation
C
It may cause NullPointerException
D
It is not thread-safe
35

Question 35

After completing this quiz, what is the single most important lesson about String handling in Java?

A
String is immutable — every apparent modification creates a new object
B
Always use new String()
C
StringBuilder is deprecated
D
== is best for String comparison

QUIZZES IN Java