StringBuilder and Stringbuffer Flashcards

(140 cards)

1
Q

In Java, the _________ is used to
represent a group of characters.

A

String class

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

It is one of the most frequently
used classes in Java programming.

A

String

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

When a new string is created,
memory is ________ for it.

A

allocated

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

String Literals
Created using __________
Stored in the ____

A

double quotes
String Pool

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

If the same value already exists in the pool, Java
_______ it instead of creating a new one.

A

reuses

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

_________: Memory is allocated and
freed as the program runs.

A

Dynamic

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

________________________
Created using the new keyword or
__________.
Stored in the Heap (general memory area for
objects).
Each new keyword creates a new instance, even
if the value is the same.

A

Dynamically Constructed Strings
concatenation

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

The _______ is like a large warehouse
where Java stores data(e.g., Strings,
Cars, Dogs).

A

Heap

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

The Heap is like a ________
where Java stores data(e.g., Strings,
Cars, Dogs).

A

large warehouse

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

_________: Java
automatically ____________ to
free up memory.

A

Garbage Collection
removes unused data

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

_________ (String Constant Pool)
A special area within the _______ for __________

A

String Pool
Heap
storing strings

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

_________by reusing identical
strings.

A

Saves memory

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

__________ by avoiding
duplicate data.

A

Improves performance

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

__________ by avoiding
duplicate data.

A

Improves performance

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

Saves memory by reusing ______________.

A

identical
strings

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

Improves performance by avoiding
__________.

A

duplicate data

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

String Pool (String Constant Pool)
How It Works:
When a string literal is created (e.g.,
“Hello “), Java checks ___________ in the pool.
If it exists, Java returns a _________ to
the existing string.
If it doesn’t exist, Java creates a ____________ and returns its
reference.

A

if it already exists
reference
new
string in the pool

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

____________: General memory for ____, _________, and managed by _______.

A

Heap
data
dynamic
garbage collection

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

___________: Special memory for strings, optimized for
reuse and efficiency.

A

String Pool

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

Use _______ (“text”) for memory efficiency and
new for unique instances.

A

string literals

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

Use string literals (“text”) for ___________ and
new for _____________.

A

memory efficiency
unique instances

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

String Pool: __________ for strings, optimized for
________ and ________.

A

Special memory
reuse
efficiency

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

Strings in Java are ____________, meaning their
values cannot be changed after creation.

A

immutable

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

Any operation that seems to modify a string (e.g.,
concatenation, replacement) actually creates a
_________.

A

new string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
This behavior can lead to_______________, especially when performing frequent ______________.
higher memory usage string manipulations
26
However, _____________ efficiently manages memory by cleaning up _____________.
Java’s garbage collector unused string data
27
Immutability means
You cannot change the content of the box directly
28
________ is like giving a copy of the toy to your friend.
Immutability
29
In Java, immutability ensures predictable and ________________, especially when multiple parts of a program use the same text.
safe behavior
30
Why It’s Important: Prevents _____________ (two threads updating the same data simultaneously). Avoids _________________ (threads seeing incorrect or partial data). Prevents ______________ or _________.
race conditions data inconsistency unexpected crashes unpredictable behavior
31
______________ensures that multiple threads can access shared data without causing errors or corruption.
Thread safety
32
_______________________ Perform one task at a time. Slower for multiple tasks.
Single-Threaded Programs
33
_______________ Perform multiple tasks simultaneously. Faster and more efficient.
Multi-Threaded Programs
34
Use multi-thread for complex programs (e.g., video streaming, chat apps, web servers).
complex programs
35
In Java, a __________________ is a special method used for initialization. A _____________ sets up the initial state by assigning values or allocating memory.
constructor
36
In Java, a constructor is a special method used for ______________. A constructor sets up the ___________ by ____________ or _________
initialization initial state assigning values allocating memory
37
In terms of ___________, a constructor is used to initialize a __________ with a default or specified value. The _______ class provides several constructors that allow different ways to create a _________ instance.
StringBuilder new StringBuilder StringBuilder StringBuilder
38
In terms of StringBuilder, a ____________ is used to _____ a new StringBuilder with a ______ or __________. The StringBuilder class provides several constructors that allow different ways to create a StringBuilder instance.
constructor initialize default specified value
39
Initializes an ___________ with a default capacity of _________.
empty StringBuilder 16 characters
40
Creates a StringBuilder with a _________ (useful when handling large strings to reduce memory reallocation).
custom capacity
41
Creates a StringBuilder initialized with the given _________.
string value
42
The initial capacity is ________ of the string.
16 + length
43
A _____________ is a general representation of character sequences (like String and StringBuffer).
CharSequence
44
A CharSequence is a ____________ of character sequences (like String and StringBuffer).
general representation
45
A CharSequence is a general representation of character sequences (like _________________).
String and StringBuffer
46
This constructor allows initializing a ______ using a __________ or another___________.
StringBuilder StringBuffer CharSequence
47
The ____________ in Java provides various methods to manipulate string sequences efficiently.
StringBuilder class
48
The StringBuilder class in Java provides various methods to ___________________________.
manipulate string sequences efficiently
49
Adds the specified text at the end of the existing StringBuilder content.
append(String s)
50
The__________ method returns the same StringBuilder instance, allowing method chaining.
append()
51
The append() method returns the same StringBuilder instance, allowing__________.
method chaining
52
append() is _______ – modifies the existing StringBuilder instead of creating a new data.
efficient
53
___________ – You can chain multiple append() calls.
Method chaining
54
_____________ – Works well with Scanner for dynamically building text.
Ideal for user input processing
55
Inserts the specified text at the given index (offset).
StringBuilder.insert(int offset, data);
56
____ → The index position where the data should be inserted.
offset
57
_____ → The value to be inserted (can be a String, char, int, double, etc.).
data
58
The _______ method modifies the existing StringBuilder data without creating a new one.
insert()
59
insert() is _____ – allows adding new data at any position without replacing existing content.
useful
60
___________– unlike replace(), it does not overwrite characters.
Preserves existing text
61
__________– works well with Scanner for building dynamic text.
Great for user input manipulation
62
Replaces the characters between start and end indexes with the specified string.
replace(int start, int end, String str)
63
______→ The starting index (inclusive) where replacement begins.
start
64
______→ The ending index (exclusive) where replacement stops.
end
65
____ → The new string that will replace the existing characters in the given range.
str
66
The ________ method modifies the existing StringBuilder data without creating a new one
replace()
67
Initializes an ___________ with a default capacity of 16 characters.
empty StringBuilder
68
Creates a StringBuilder with a ______________(useful when handling large strings to reduce memory reallocation).
custom capacity
69
Creates a StringBuilder initialized with the__________.
given string value
70
The initial capacity is _______ of the string.
16 + length
71
The StringBuilder class in Java provides various ____________ to manipulate string sequences efficiently.
methods
72
replace() is ______ – modifies the existing StringBuilder without creating a_______.
efficient new data
73
It replaces only a portion of the text – unlike __________, which changes a single character.
setCharAt()
74
Uses index positions – you must specify __ and ______for replacement.
start end indexes
75
Deletes characters between the specified start and end indexes.
delete(int start, int end)
76
Description: Deletes characters between the specified start and end indexes. ______→ The starting index (inclusive) where deletion begins. ______→ The ending index (exclusive) where deletion stops. ______method modifies the existing StringBuilder without creating a new one.
start end The delete()
77
delete(int start, int end) Key Takeaways ✔ delete() _____________– modifies the existing StringBuilder without creating a new Stringbuilder. ✔ It ________________ – unlike setLength(0), which clears the entire content. ✔ Uses___________ – you must specify start and end indexes for deletion. ✔ Works well for__________ – great for removing ______ from user input.
removes text efficiently removes only a portion of the text index positions string manipulation unnecessary words
78
______________ Description: Removes a single character at the specified index. ________ → The position of the character to be removed. The ___________method modifies the existing StringBuilder instead of creating a new one. Usage: Useful for______________ without affecting the rest of the string.
deleteCharAt(int index) index deleteCharAt() deleting a single character
79
5. deleteCharAt(int index) Key Takeaways ✔ deleteCharAt() removes a____ efficiently – modifies the StringBuilder directly. ✔_______ – unlike delete(), which removes a range of characters. ✔ Useful for ______ – removing spaces, typos, or unwanted characters. ✔ ________ is important – always check if the index is valid before using deleteCharAt().
single character Requires an index correcting user input Error handling
80
Useful for correcting user input – ___________, _____, or ____________.
removing spaces typos unwanted characters
81
Error handling is important – always check if the index is ______ before using deleteCharAt().
valid
82
_____ Description: Reverses the sequence of characters in the StringBuilder. This method modifies the existing StringBuilder in place. The __________ is transformed into its reversed version Usage: Helps in reversing strings for ___________- or _____________.
reverse() original string palindromes algorithmic processing
83
______ is useful for reversing words and sentences – modifies the StringBuilder directly. ✔ _______ – does not create a new instance of the variable, works in-place. ✔ Can be used for ____________ – reversing a string and comparing it with the original. ✔ Works on any________ – words, sentences, numbers, etc.
reverse() Efficient palindrome checking text format
84
✔ Works on any text format – ______, _______, ________, etc.
words sentences numbers
85
________ Description: Returns the current capacity (________) of the StringBuilder. ___________ refers to the __________ the StringBuilder can hold before it needs to resize.
capacity() storage size Capacity number of characters
86
_____________ = StringBuilder.capacity();
int currentCapacity
87
int currentCapacity =________________
StringBuilder.capacity();
88
____________ is 16 characters if created with new StringBuilder().
Default capacity
89
When the capacity is exceeded, it expands automatically using the formula: _______________________________
New Capacity = (Old Capacity * 2) + 2
90
Default capacity is ___________ if created with new StringBuilder().
16 characters
91
Unlike _______, which gives the actual number of characters stored, capacity() tells how much space is allocated.
length()
92
Unlike length(), which gives the _______________________, capacity() tells how much space is allocated.
actual number of characters stored
93
Unlike length(), which gives the actual number of characters stored, ___________ tells how much space is allocated.
capacity()
94
Unlike length(), which gives the actual number of characters stored, capacity() tells how much ___________________.
space is allocated
95
capacity() Helps understand how much ______________________.
space is allocated before resizing occurs
96
____________shows allocated storage space, not the actual string length.
capacity()
97
capacity() shows__________________, not the __________________.
allocated storage space actual string length
98
______________→ Number of characters currently stored.
length()
99
Ensures that the StringBuilder has at least minCapacity of storage, growing if necessary.
ensureCapacity(int minCapacity)
100
If __________ is greater than the _________________, the capacity increases using the formula: New Capacity = (Old Capacity * 2) + 2
minCapacity current capacity
101
If minCapacity is greater than the current capacity, the capacity increases using the formula: ____________________________
New Capacity = (Old Capacity * 2) + 2
102
If minCapacity is ________________ to the current capacity, no change is made.
less than or equal
103
______________ → The minimum required capacity.
minCapacity
104
ensureCapacity() This method does not ____________, only increases it if necessary.
shrink capacity
105
ensureCapacity() only ____________________if necessary – it does not decrease or shrink storage.
increases capacity
106
_____________ → Expands the storage if needed.
ensureCapacity()
107
Default capacity is 16, but it ___________ when exceeded.
doubles plus 2
108
ensureCapacity(): Using a__________ can___________ when handling______________.
larger initial capacity improve performance large text data
109
Returns the number of characters in the StringBuilder.
length()
110
_________________= StringBuilder.length();
int currentLength
111
int currentLength = _______________
StringBuilder.length();
112
_______→ Returns the actual number of characters in the StringBuilder.
length()
113
The length changes dynamically as characters are _______ or ________.
added removed
114
Important for checking string size before performing ______________ like_______________.
operations substring()
115
Returns the character at the specified index
charAt(int index)
116
__________→ The position of the character to retrieve.
index
117
Throws _____________ if index is negative or greater than or equal to length().
StringIndexOutOfBoundsException
118
charAt(int index): Helps_____________ from the StringBuilder
retrieve a specific character
119
charAt(int index) Key Takeaways ✔ charAt(int index) retrieves a specific character from StringBuilder. ✔ _________________ – should be between 0 and length() - 1. ✔ Throws____________________if the index is out of range. ✔ Useful for ______________ – extracting characters, checking initials, etc.
Validates the index StringIndexOutOfBoundsException processing strings
120
charAt(int index): Useful for processing strings –______________, ________________, etc.
extracting characters checking initials
121
Changes the character at the specified index to the given char c.
setCharAt(int index, char c)
122
setCharAt(int index, char c) Description: Changes the character at the specified index to the given char c. StringBuilder.setCharAt(int index, char c); _______→ The position of the character to be replaced. ___→ The new character that will replace the existing one at the_____. This method modifies the _______ directly (inplace). Throws StringIndexOutOfBoundsException if index is negative or greater than or equal to length(). Usage: Useful for modifying ______________without modifying the whole string.
index c given index StringBuilder individual characters
123
______________ modifies a specific character directly in StringBuilder. ✔ _____________– must be between 0 and length() - 1. __________________ – capitalizing letters, modifying punctuation, fixing typos, etc.
setCharAt(int index, char c) Validates the index Useful for text formatting
124
Returns a new String starting from start index to the end.
substring(int start)
125
substring(int start) Description: Returns a new String starting from start index to the end. ______________ = StringBuilder.substring(int start); ______ → The index from which the substring begins (inclusive). ________ (does not modify StringBuilder). Throws StringIndexOutOfBoundsException if start is negative or greater than length().
String subStr start Returns a new String
126
___________ extracts part of the string starting from start.
substring(int start)
127
substring(int start, int end) Throws StringIndexOutOfBoundsException if: start is _______. end is _______________. start is______________.
negative greater than length() greater than end
128
substring: ✔ Useful for extracting _____, _____, _____, ______ ______, etc.
names dates words file extensions
129
Converts the StringBuilder data into a String.
toString()
130
toString() Description: Converts the StringBuilder data into a String. String str = ____________________ Returns a new String containing the same sequence of characters as in StringBuilder. Unlike StringBuilder, which is _______, String is __________. Useful when you need an immutable version of the string, such as when returning the final result. Usage: Since StringBuilder is mutable, this method is used when we need an immutable String representation.
StringBuilder.toString(); mutable immutable
131
___________ converts StringBuilder into an immutable String. ✔ Useful when the ____________ should not be modified. ✔ __________ (e.g., storing data, printing results) often require String instead of StringBuilder. ✔ Does not modify the StringBuilder, just returns a ______.
toString() final result String operations new String
132
____________ and ___________ have the same methods.
StringBuilder StringBuffer
133
the keydifference is that StringBuffer is _____________ (synchronized) but ____________, while StringBuilder is ___________ but not___________
threadsafe slower faster threadsafe.
134
______________ does not use ___________, making it more efficient in______________.
StringBuilder synchronization single-threaded environments
135
____________ uses _______ methods, making it ____________.
StringBuffer synchronized thread-safe but slower
136
Since String is immutable, converting it to StringBuffer or StringBuilder allows __________.
modifications
137
Since StringBuffer and StringBuilder are mutable, converting them to String gives an _______________.
immutable version
138
Use StringBuffer and StringBuilder when ____________ are needed.
modifications
139
Use ________ to make strings immutable before storing them.
toString()
140
In Java, working with different string types—String, StringBuffer, and StringBuilder—is important for handling ______________.
text efficiently