String Flashcards

(165 cards)

1
Q

What does immutable mean in the context of String in Java?

A

Immutable means once a String object is created, its value cannot be changed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Is String immutable in Java?

A

Yes. String objects cannot be modified after creation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What happens when you modify a String?

A

A new String object is created instead of changing the original.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Example of String immutability?

A

String s = “A”; s = s + “B”; creates a new object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why was String made immutable?

A

For security, performance, and thread safety.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How does immutability improve security?

A

It prevents modification of sensitive data like file paths or class loaders.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How does immutability improve thread safety?

A

Immutable objects can be safely shared between threads.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How does immutability help String pool?

A

It allows reuse of String literals safely.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Does String provide methods like replace() or concat()?

A

Yes, but they return new String objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Can you change characters inside a String?

A

No. Its internal value cannot be altered.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Which class is mutable alternative to String?

A

StringBuilder and StringBuffer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Does immutability reduce memory usage?

A

It allows String pooling and reuse of identical literals.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is String pool?

A

A memory area where identical String literals are reused.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Can immutable objects be subclassed?

A

No. String is final to preserve immutability.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Key rule to remember?

A

String is immutable — modification creates a new object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Why is a String object considered immutable in Java?

A

Because once a String is created, its internal value cannot be changed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What happens when you modify a String variable?

A

A new String object is created instead of modifying the existing one.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Why is immutability important for String literals?

A

Multiple references can safely share the same literal in the String pool.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What would happen if String were mutable?

A

Changing one reference would affect all other references pointing to the same object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How does String pooling relate to immutability?

A

It allows identical literals to reuse the same memory safely.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Example of shared literal?

A

String a = “Test”; String b = “Test”; both point to same object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

If a is reassigned, does b change?

A

No. A new object is created and b still references the original.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Why does immutability improve memory efficiency?

A

Because shared literals do not require duplicate storage.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

How does immutability improve security?

A

It prevents modification of sensitive data like file paths or credentials.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
How does immutability support thread safety?
Immutable objects can be safely shared across threads.
26
Is String class final?
Yes. It is final to preserve immutability.
27
Can internal value of String be changed using normal methods?
No. All modifying methods return new objects.
28
Which classes provide mutable string alternatives?
StringBuilder and StringBuffer.
29
Does immutability prevent reassignment of reference?
No. It prevents modification of the object, not the reference.
30
Key rule to remember?
String is immutable so shared literals remain safe and consistent.
31
How many objects are created in this code: String s1 = "HelloWorld"; String s2 = "HelloWorld"; String s3 = "HelloWorld"?
Only one String object is created in the String pool.
32
Why is only one object created?
Because all variables reference the same String literal stored in the String pool.
33
What is the String pool?
A special memory area in the heap where identical String literals are stored once.
34
Do identical String literals create multiple objects?
No. They reuse the same pooled object.
35
Where is the pooled String stored?
In the heap’s String constant pool area.
36
What happens when multiple variables use the same literal?
They point to the same memory reference.
37
Does this behavior improve performance?
Yes. It reduces memory usage and avoids duplicate objects.
38
What if one variable changes its value?
A new String object is created and assigned to that variable.
39
Does reassignment modify the pooled object?
No. The original pooled object remains unchanged.
40
What if new String("HelloWorld") is used?
A new object is created in heap, even if literal exists in pool.
41
How many objects in this case?
String s = new String("HelloWorld"); → Two objects (one in pool, one in heap).
42
Is String pooling possible only because String is immutable?
Yes. Immutability makes sharing safe.
43
Do leading/trailing spaces change pooling behavior?
Yes. "HelloWorld" and " HelloWorld " are different literals and create separate objects.
44
What if literals differ slightly?
Each unique literal creates one pooled object.
45
Key rule to remember?
Identical String literals create one pooled object shared by all references.
46
How many ways can you create a String object in Java?
Two main ways: using a String literal or using the new keyword.
47
What is creating a String using a literal?
String s = "Hello";
48
Where is a String literal stored?
In the String constant pool.
49
What happens if the same literal already exists?
The existing pooled object is reused.
50
What is creating a String using new keyword?
String s = new String("Hello");
51
Where is a String created with new stored?
A new object is created in the heap.
52
Does using new always create a new object?
Yes, even if the same literal exists in the pool.
53
How many objects can be created with new String("Hello")?
Two: one in pool (if not already present) and one in heap.
54
Which approach is memory efficient?
String literals, because of pooling.
55
Which approach gives explicit new object every time?
Using the new keyword.
56
Can both approaches produce equal content?
Yes, but references may differ.
57
Does equals() compare content or reference?
Content.
58
Does == compare content or reference?
Reference.
59
Which is recommended for most cases?
String literals.
60
Key rule to remember?
Literal → pooled object; new → heap object.
61
How many objects are created by String s = new String("HelloWorld");?
Two objects are created.
62
Where is the first object created?
In the String constant pool for the literal "HelloWorld" (if not already present).
63
Where is the second object created?
In the heap memory outside the String pool.
64
Why does this happen?
Because the literal is stored in the pool, and the new keyword explicitly creates a new heap object.
65
If "HelloWorld" already exists in the pool, how many new objects are created?
Only one new object in the heap.
66
Does the variable s reference the pooled object?
No. It references the new heap object.
67
How can you make s reference the pooled object?
By calling s = s.intern();
68
What does intern() do?
It returns the reference from the String pool.
69
Is using new String() memory efficient?
No. It creates unnecessary extra objects.
70
Which approach is recommended in most cases?
Using String literals.
71
Does equals() return true between pooled and heap String?
Yes, because content is same.
72
Does == return true between pooled and heap String?
No, because references are different.
73
Why is String immutable important here?
It allows safe sharing in the pool.
74
Key rule to remember?
new String("X") → pooled literal + separate heap object.
75
What is String interning in Java?
String interning is the process of storing only one copy of each distinct String value in the String pool.
76
Where are interned Strings stored?
In the String constant pool inside the heap.
77
Why does Java use String interning?
To save memory and allow efficient reuse of identical String values.
78
How does interning improve memory usage?
Multiple references share the same String object instead of creating duplicates.
79
How does interning improve performance?
Reference comparison using == becomes possible for pooled literals.
80
What method is used to intern a String manually?
intern().
81
What does String.intern() do?
It returns the pooled reference if present, otherwise adds the String to the pool.
82
Example of interning?
String s1 = new String("Hello"); String s2 = s1.intern();
83
Does String literal automatically use interning?
Yes. All String literals are automatically interned.
84
Is interning free in terms of performance?
No. It adds lookup overhead during creation.
85
Can interning increase performance overall?
Yes, especially when many identical Strings exist.
86
Does interning work only because String is immutable?
Yes. Immutability ensures shared objects remain safe.
87
What happens if two identical literals are declared?
They point to the same pooled object.
88
Is the String pool garbage collected?
Yes, since Java 7 it is part of the heap.
89
Key rule to remember?
String interning = one shared immutable instance per distinct value.
90
Why does Java use the String literal concept?
To improve memory efficiency by reusing identical String values through the String pool.
91
What happens when a String literal is created?
It is stored in the String constant pool.
92
What if the same literal already exists in the pool?
The existing object is reused instead of creating a new one.
93
How does this improve memory usage?
It avoids duplicate objects for identical String values.
94
Does String pooling improve performance?
Yes. It reduces object creation and enables faster reference comparisons.
95
Why is pooling safe for Strings?
Because Strings are immutable.
96
What would happen if Strings were mutable?
Sharing pooled Strings would cause unintended modifications.
97
Where is the String pool located?
In the heap memory.
98
Does using new String() benefit from pooling?
No. It creates a separate heap object.
99
Which approach is recommended for normal usage?
String literals.
100
Does String pooling reduce garbage collection pressure?
Yes. Fewer duplicate objects means less memory churn.
101
Can String literals improve startup performance?
Yes. Frequently used literals are reused.
102
Is String pooling automatic?
Yes. The JVM handles it internally.
103
Does every String go into the pool automatically?
Only literals and Strings explicitly interned.
104
Key rule to remember?
String literals enable pooling, which saves memory and improves efficiency.
105
What is the basic difference between String and StringBuffer in Java?
String is immutable, while StringBuffer is mutable.
106
What does immutable mean for String?
Its value cannot be changed after creation.
107
What does mutable mean for StringBuffer?
Its content can be modified without creating a new object.
108
What happens when you modify a String?
A new String object is created.
109
What happens when you modify a StringBuffer?
The same object is updated.
110
Is StringBuffer thread-safe?
Yes. Its methods are synchronized.
111
Is String thread-safe?
Yes, because it is immutable.
112
Which is better for frequent string modifications?
StringBuffer.
113
Which consumes more memory for repeated changes?
String, because it creates multiple objects.
114
How do you append text to StringBuffer?
Using append() method.
115
Example of String modification?
String s = "A"; s = s + "B"; creates new object.
116
Example of StringBuffer modification?
StringBuffer sb = new StringBuffer("A"); sb.append("B");
117
Is StringBuffer faster than String for concatenation?
Yes, in loops or heavy modifications.
118
Is StringBuffer used in modern code often?
Less common; StringBuilder is preferred for non-thread-safe scenarios.
119
Key rule to remember?
String = immutable; StringBuffer = mutable and synchronized.
120
How do you create an immutable class in Java?
Make the class final, keep fields private and final, initialize them once, and avoid exposing mutable state.
121
Why should the class be declared final?
To prevent subclassing that could alter immutability.
122
Why must fields be private?
To prevent direct modification from outside the class.
123
Why must fields be final?
To ensure they are assigned only once.
124
Should immutable classes provide setters?
No. Setters allow modification of state.
125
How should fields be initialized?
Through constructor only.
126
Why use deep copy in constructor?
To prevent external references from modifying internal mutable objects.
127
Should getter methods return direct references to mutable objects?
No. They should return defensive copies.
128
What is defensive copying?
Creating a new copy of mutable objects when returning or assigning.
129
Can immutable classes contain mutable objects?
Yes, but only with proper defensive copying.
130
Should clone() return original object?
No. It should return a new copy if implemented.
131
Is overriding clone() required for immutability?
Not required, but if implemented, it must preserve immutability.
132
Example of immutable class pattern?
final class Person { private final String name; Person(String n){ this.name = n; } public String getName(){ return name; } }
133
Why are immutable classes thread-safe?
Because their state cannot change after construction.
134
Example of immutable class in Java API?
String, Integer, LocalDate.
135
Key rule to remember?
Immutable class = final class + private final fields + no setters + defensive copying.
136
What is the purpose of toString() method in Java?
toString() returns a String representation of an object.
137
Which class defines toString()?
java.lang.Object.
138
What happens when you print an object using System.out.println()?
Java automatically calls the object's toString() method.
139
What is the default implementation of toString()?
It returns ClassName@hashcode.
140
Example default output?
MyClass@1a2b3c4d.
141
Why override toString()?
To provide meaningful, readable object information.
142
Can toString() improve debugging?
Yes. It makes logs and console output more informative.
143
Does overriding toString() change object behavior?
No. It only changes its textual representation.
144
Is toString() automatically called in string concatenation?
Yes. When concatenating object with String.
145
Example of automatic call?
System.out.println("Object: " + obj);
146
Should toString() modify object state?
No. It should only return a representation.
147
Can toString() be final?
Yes, but usually it is overridden.
148
Is toString() required to override?
No, but it is strongly recommended.
149
Do collections use toString()?
Yes. They call toString() of contained objects.
150
Key rule to remember?
toString() provides human-readable representation of an object.
151
Arrange String, StringBuffer, and StringBuilder in order of efficiency for string processing.
StringBuilder > StringBuffer > String.
152
Which is the most efficient for string manipulation?
StringBuilder.
153
Why is StringBuilder the fastest?
It is mutable and not synchronized, so it has no locking overhead.
154
Why is StringBuffer slower than StringBuilder?
Because its methods are synchronized for thread safety.
155
Why is String the slowest for modifications?
Because it is immutable and creates a new object for each change.
156
When should StringBuilder be used?
In single-threaded environments with frequent modifications.
157
When should StringBuffer be used?
In multi-threaded environments where thread safety is required.
158
Is String thread-safe?
Yes, because it is immutable.
159
Is StringBuilder thread-safe?
No.
160
Does immutability affect performance?
Yes. It causes additional object creation during concatenation.
161
Which is best for heavy concatenation in loops?
StringBuilder.
162
Which is best when thread safety is mandatory?
StringBuffer.
163
Which is best for constant or rarely modified text?
String.
164
Does synchronization impact performance?
Yes. It adds overhead.
165
Key rule to remember?
Efficiency order: StringBuilder > StringBuffer > String.