JAVA STRING Flashcards

1
Q

In Java, the ____ class is used to
represent a ___________.

A

String, group of characters

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

Java String or String

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

When a new ____ is created,
________ is allocated for it.

A

string, memory

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

It is created using double quotes and it can store in the String Pool

A

String Literals

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

This can be created using the new keyword or concatenation.

A

Dynamically Constructed Strings

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

What is String Pool

A

a special memory area.

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

What is Heap

A

general memory area for objects.

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

The _____ is like a __________ where Java stores data

A

Heap, large warehouse

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

What are the two key features of Heap Memory?

A

Dynamic and Garbage Collection

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

Java automatically removes unused data to free up memory.

A

Garbage Collection

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

A special area within the Heap for storing strings.

A

String Pool (String Constant Pool)

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

What is the Purpose of String Pool?

A

Saves memory by reusing identical strings and it Improves performance by avoiding duplicate data.

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

Heap is a general memory for _____, ______, and managed by _______.

A

data, dynamic, garbage collection

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

String Pool is a ________ 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
15
Q

Strings in Java are _________, meaning their values __________ after creation.

A

immutable, cannot be changed

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

This behavior can lead to __________, especially when performing frequent __________.

A

higher memory usage, string
manipulations

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

Java’s __________ efficiently manages memory by cleaning up _________.

A

garbage collector, unused string data

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

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

A

immutability, predictable, safe behavior

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

_________ ensures that multiple threads can access shared _____ without causing _____ or _______.

A

Thread safety, data, errors, corruption

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

What is race condition?

A

two threads updating the same data simultaneously.

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

What is data inconsistency?

A

threads seeing incorrect or partial data.

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

What are the use of Single-Thread?

A

Perform one task at a time and Slower for multiple tasks.

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

What are the use of Multi-Threaded Programs?

A

Perform multiple tasks simultaneously. Faster and more efficient.

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

When to use Single-Thread?

A

Use single-thread for simple programs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
When to use multi-thread?
Use multi-thread for complex programs
26
In Java, a ________ is a special method used for __________.
constructor, initialization
27
A constructor sets up the _________ by _________ or ___________.
initial state, assigning values, allocating memory,
28
In terms of StringBuilder, a __________is used to ________ a new StringBuilder with a ________ or ___________.
StringBuilder, constructor initialize, default, specified value
29
The StringBuilder class provides several________ that allow different ways to create a _________.
constructors, StringBuilder instance
30
When using StringBuilder() (Default Constructor), what does it initializes?
an empty StringBuilder with a default capacity of 16 characters.
31
When using StringBuilder(int capacity) (Custom Capacity), What does it create?
a StringBuilder with a custom capacity (useful when handling large strings to reduce memory reallocation).
32
When using StringBuilder(String str) (Initialize with String), What does it create?
a StringBuilder initialized with the given string value with the initial capacity of 16+length of the string.
33
When using StringBuilder(CharSequence seq) (Initialize with CharSequence), What does it create?
CharSequence is a general representation of character sequences (like String and StringBuffer).
34
This constructor allows initializing a StringBuilder using a StringBuffer or another CharSequence.
StringBuilder(CharSequence seq) (Initialize with CharSequence)
35
The _________ class in Java provides various _________ to manipulate ________ efficiently.
StringBuilder, methods, string sequences
36
This method adds the specified text at the end of the existing StringBuilder content.
append(String s)
37
The _______ method returns the same _________, allowing ____________.
append(), StringBuilder instance, method chaining
38
This method Inserts the specified text at the given index (offset).
StringBuilder.insert(int offset, data);
39
The index position where the data should be inserted.
offset
40
The value to be inserted (can be a String, char, int, double, etc.).
data
41
The _______ method modifies the existing ________ data without creating a new one.
insert(), StringBuilder
42
When to use StringBuilder.insert(int offset, data); ?
Useful when inserting a string at a specific position.
43
This method replaces the characters between start and end indexes with the specified string.
replace(int start, int end, String str)
44
The starting index (inclusive) where replacement
start
45
The ending index (exclusive) where replacement stops.
end
46
The new string that will replace the existing characters in the given range.
str
47
The _______ method modifies the existing StringBuilder data without creating a new one.
replace()
48
When to use replace(int start, int end, String str)?
useful when modifying a portion of the string.
49
This method deletes characters between the specified start and end indexes.
delete(int start, int end)
50
When to use delete(int start, int end)?
when removing unwanted parts of the string.
51
Removes a single character at the specified index.
deleteCharAt(int index)
52
The ________ method ______ the existing __________ instead of creating a new one.
deleteCharAt() , modifies, StringBuilder
53
When to use deleteCharAt(int index)?
Useful for deleting a single character without affecting the rest of the string.
54
This method reverses the sequence of characters in the StringBuilder.
reverse()
55
This method modifies the existing StringBuilder in place.
reverse()
56
When to use reverse()
useful when reversing strings for palindromes or algorithmic processing.
57
This method returns the current capacity (storage size) of the StringBuilder.
capacity()
58
__________ refers to the number of characters the StringBuilder can hold before it needs to resize.
Capacity
59
Default capacity is ________ if created with new StringBuilder().
16 characters
60
When the capacity is _________, it _________ using the formula:
exceeded, expands automatically
61
What is the formula of Capacity method?
New Capacity = (Old Capacity * 2) + 2
62
When to use capacity?
When understanding how much space is allocated before resizing occurs.
63
This method does not shrink capacity, only increases it if necessary.
ensureCapacity(int minCapacity)
64
This method ensures that the StringBuilder has at least minCapacity of storage, growing if necessary.
ensureCapacity(int minCapacity)
65
If minCapacity is greater than the current capacity, the capacity increases using the formula. Which is ?
New Capacity = (Old Capacity * 2) + 2
66
If minCapacity is less than or equal to the current capacity, what happens?
no change is made
67
When to use ensureCapacity(int minCapacity)?
when preventing frequent resizing when adding large amounts of data.
68
This method returns the number of characters in the StringBuilder.
length()
69
What is the syntax of length?
int currentLength = StringBuilder.length();
70
What differs lengths() to capacity()?
length() → Returns the actual number of characters in the StringBuilder. capacity() → Returns the total allocated space for characters.
71
When to use length()?
When determining the current size of the content.
72
This method returns the character at the specified index
charAt(int index)
73
The position of the character to retrieve
index
74
When to use charAt(int index)
When retrieving a specific character from the StringBuilder
75
This method changes the character at the specified index to the given char c.
setCharAt(int index, char c)
76
When to use setCharAt(int index, char c)?
Useful for modifying individual characters without modifying the whole string.
77
This method returns a new String starting from start index to the end.
substring(int start)
78
When to use substring(int start)
Extracts a portion of the StringBuilder as a String.
79
This method extracts a portion of the string between the given start (inclusive) and end (exclusive) indexes.
substring(int start, int end)
80
When to use substring(int start, int end)?
Extracts a portion of the StringBuilder as a String.
81
This method converts the StringBuilder data into a String.
toString()
82
When to use toString()?
Since StringBuilder is mutable, this method is used when we need an immutable String representation.
83
What are the key differences of StringBuilder and StringBuffer?
the key difference is that StringBuffer is thread- safe (synchronized) but slower, while StringBuilder is faster but not thread-safe.
84
What is the execution time for StringBuilder and StringBuffer when performing a large number of operations?
Appending 100,000 times
85
Give 3 string types in Java
String (Immutable) StringBuffer (Mutable & Thread-Safe) StringBuilder (Mutable & Fast but Not Thread-Safe)
86
"Since String is immutable, converting it to StringBuffer or StringBuilder allows modifications.". What type of conversion is this?
Convert String to StringBuffer and StringBuilder
87
"Since StringBuffer and StringBuilder are mutable, converting them to String gives an immutable version." What type of conversion is this?
Convert StringBuffer and StringBuilder to String
88
"Since both are mutable, Java doesn’t provide a direct method to convert between them. We first convert to String, then create a new instance of the other type." What type of conversion is this?
Convert StringBuffer to StringBuilder or Vice Versa
89
In Java, working with different string types—_____, _____, and _______—is important for handling text efficiently.
String, StringBuffer, StringBuilder
90
_________ is immutable, meaning it cannot be changed once created, while ______ and ______ are mutable, allowing _______ without creating ________.
String, StringBuffer, StringBuilder, modifications, new instance.
91
The key difference between StringBuffer and StringBuilder is that StringBuffer is _________, making it suitable for _________, whereas StringBuilder is faster but not _________, making it ideal for ____________ environments.
thread-safe (synchronized), multi-threaded applications, synchronized, single-threaded