Stringbuilder and Stringbuffer Flashcards

1
Q

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

A

String

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

When a new string is created,
_________ is allocated for it.

A

memory

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

In Java, immutability ensures __________
and ___________, especially when multiple
parts of a program use the same text.

A

predictable and safe behavior

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

__________ ensures that multiple threads can
access shared data without causing errors or
corruption.

A

Thread safety

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

______-Threaded Programs:
1. Perform one task at a time.
2. Slower for multiple tasks.

A

Single

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

_____-Threaded Programs
1. Perform multiple tasks simultaneously.
2. Faster and more efficient.

A

Multi

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

Use _____________ for simple programs (e.g.,
basic calculations).

A

single-thread

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

Use _____________ for complex programs (e.g.,
video streaming, chat apps, web servers).

A

multi-thread

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

In Java, a __________ is a special method used
for initialization.

A

constructor

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

A constructor sets up the
initial state by assigning ________ or
allocating __________.

A

A constructor sets up the
initial state by assigning VALUES or
allocating MEMORY.

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

Visualize what the constructor look like in Java:

Creates an empty StringBuilder with an initialize capacity of 16 characters.

A

StringBuilder()

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

Visualize what the constructor look like in Java:

Creates an empty StringBuilder with an specified capacity (number of characters it can hold before resizing).

A

StringBuilder(int capacity)

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

Visualize what the constructor look like in Java:

Creates a StringBuilder initialized with the given string value.
The initial capacity is 16 + length of the string.

A

StringBuilder(String str)

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

Visualize what the constructor look like in Java:

This constructor allows initializing a StringBuilder using a
StringBuffer or another CharSequence.

A

StringBuilder(CharSequence seq)

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

Adds the specified text at the end of
the existing StringBuilder content.

A

append(String s)

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

Description: Replaces the characters between start and end
indexes with the specified string.

A

replace(int start, int end, String str)

17
Q

Description: Deletes characters between the specified
start and end indexes.

A

delete(int start, int end)

18
Q

Description: Removes a single character at the
specified index.

A

deleteCharAt(int index)

19
Q

The delete() method __________ the existing StringBuilder
without creating a new one.

20
Q

Description: Reverses the sequence of characters in
the StringBuilder.

21
Q

Description: Returns the current capacity (storage size) of
the StringBuilder.

A

capacity()

22
Q

Unlike length(), which gives the actual number of
characters stored, ___________ tells how much
space is allocated.

A

capacity()

23
Q

Description: Ensures that the StringBuilder has at least
minCapacity of storage, growing if necessary.

A

ensureCapacity(int minCapacity)

24
Q

Description: Returns the number of characters in the
StringBuilder.

25
Q

Description: Returns the character at the specified
index.

A

charAt(int index)

26
Q

Description: Changes the character at the specified index to
the given char c.

A

setCharAt(int index, char c)

27
Q

Description: Returns a new String starting from start
index to the end.

A

substring(int start)

28
Q

Description: Extracts a portion of the string between the
given start (inclusive) and end (exclusive) indexes.

A

substring(int start, int end)

29
Q

Description: Converts the StringBuilder data into a
String.

A

toString()

30
Q

Use _____________ if multiple people
(threads) will be working on the same
text at the same time.

A

StringBuffer

31
Q

Use _____________ if only one person
(thread) is working on it for speed.

A

StringBuilder

32
Q

Since StringBuffer and StringBuilder are
mutable, converting them to String gives an
____________ version.