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
Q

When to use multi-thread?

A

Use multi-thread for complex programs

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

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

A

constructor, initialization

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

A constructor sets up the
_________ by _________ or
___________.

A

initial state, assigning values, allocating memory,

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

In terms of StringBuilder, a __________is used to ________ a new StringBuilder with a ________ or ___________.

A

StringBuilder, constructor initialize, default, specified value

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

The StringBuilder class provides several________ that allow different ways to create a _________.

A

constructors, StringBuilder instance

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

When using StringBuilder() (Default Constructor), what does it initializes?

A

an empty StringBuilder with a default capacity of 16 characters.

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

When using StringBuilder(int capacity) (Custom Capacity), What does it create?

A

a StringBuilder with a custom capacity (useful when handling large strings to reduce memory reallocation).

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

When using StringBuilder(String str) (Initialize with String), What does it create?

A

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

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

When using StringBuilder(CharSequence seq) (Initialize with CharSequence), What does it create?

A

CharSequence is a general representation of character sequences (like String and StringBuffer).

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

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

A

StringBuilder(CharSequence seq) (Initialize with CharSequence)

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

The _________ class in Java provides various _________ to manipulate ________ efficiently.

A

StringBuilder, methods, string sequences

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

This method 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
37
Q

The _______ method returns the same _________, allowing ____________.

A

append(), StringBuilder instance, method chaining

38
Q

This method Inserts the specified text at the given index (offset).

A

StringBuilder.insert(int offset, data);

39
Q

The index position where the data should be inserted.

40
Q

The value to be inserted (can be a String, char, int, double, etc.).

41
Q

The _______ method modifies the existing ________ data without creating a new one.

A

insert(), StringBuilder

42
Q

When to use StringBuilder.insert(int offset, data); ?

A

Useful when inserting a string at a specific
position.

43
Q

This method replaces the characters between start and end indexes with the specified string.

A

replace(int start, int end, String str)

44
Q

The starting index (inclusive) where replacement

45
Q

The ending index (exclusive) where replacement stops.

46
Q

The new string that will replace the existing
characters in the given range.

47
Q

The _______ method modifies the existing StringBuilder data without creating a new one.

48
Q

When to use replace(int start, int end, String str)?

A

useful when modifying a portion of the string.

49
Q

This method deletes characters between the specified start and end indexes.

A

delete(int start, int end)

50
Q

When to use delete(int start, int end)?

A

when removing unwanted parts of the string.

51
Q

Removes a single character at the
specified index.

A

deleteCharAt(int index)

52
Q

The ________ method ______ the existing __________ instead of creating a new one.

A

deleteCharAt() , modifies, StringBuilder

53
Q

When to use deleteCharAt(int index)?

A

Useful for deleting a single character without affecting the rest of the string.

54
Q

This method reverses the sequence of characters in the StringBuilder.

55
Q

This method modifies the existing StringBuilder in place.

56
Q

When to use reverse()

A

useful when reversing strings for palindromes or algorithmic processing.

57
Q

This method returns the current capacity (storage size) of the StringBuilder.

A

capacity()

58
Q

__________ refers to the number of characters the StringBuilder can hold before it needs to resize.

59
Q

Default capacity is ________ if created with
new StringBuilder().

A

16 characters

60
Q

When the capacity is _________, it _________ using the formula:

A

exceeded, expands automatically

61
Q

What is the formula of Capacity method?

A

New Capacity = (Old Capacity * 2) + 2

62
Q

When to use capacity?

A

When understanding how much space is allocated before resizing occurs.

63
Q

This method does not shrink capacity, only increases it if necessary.

A

ensureCapacity(int minCapacity)

64
Q

This method ensures that the StringBuilder has at least minCapacity of storage, growing if necessary.

A

ensureCapacity(int minCapacity)

65
Q

If minCapacity is greater than the current capacity, the capacity increases using the formula. Which is ?

A

New Capacity = (Old Capacity * 2) + 2

66
Q

If minCapacity is less than or equal to the current capacity, what happens?

A

no change is made

67
Q

When to use ensureCapacity(int minCapacity)?

A

when preventing frequent resizing when adding large amounts of data.

68
Q

This method returns the number of characters in the StringBuilder.

69
Q

What is the syntax of length?

A

int currentLength = StringBuilder.length();

70
Q

What differs lengths() to capacity()?

A

length() → Returns the actual number of
characters in the StringBuilder.
capacity() → Returns the total allocated space for characters.

71
Q

When to use length()?

A

When determining the current size of the content.

72
Q

This method returns the character at the specified index

A

charAt(int index)

73
Q

The position of the character to
retrieve

74
Q

When to use charAt(int index)

A

When retrieving a specific character from the StringBuilder

75
Q

This method changes the character at the specified index to the given char c.

A

setCharAt(int index, char c)

76
Q

When to use setCharAt(int index, char c)?

A

Useful for modifying individual characters without modifying the whole string.

77
Q

This method returns a new String starting from start index to the end.

A

substring(int start)

78
Q

When to use substring(int start)

A

Extracts a portion of the StringBuilder as a String.

79
Q

This method extracts a portion of the string between the given start (inclusive) and end (exclusive) indexes.

A

substring(int start, int end)

80
Q

When to use substring(int start, int end)?

A

Extracts a portion of the StringBuilder as a String.

81
Q

This method converts the StringBuilder data into a String.

A

toString()

82
Q

When to use toString()?

A

Since StringBuilder is mutable, this method is used when we need an immutable String representation.

83
Q

What are the key differences of StringBuilder and StringBuffer?

A

the key difference is that StringBuffer is thread- safe (synchronized) but slower, while StringBuilder is faster but not thread-safe.

84
Q

What is the execution time for StringBuilder and StringBuffer when performing a large number of operations?

A

Appending 100,000 times

85
Q

Give 3 string types in Java

A

String (Immutable)
StringBuffer (Mutable & Thread-Safe)
StringBuilder (Mutable & Fast but Not Thread-Safe)

86
Q

“Since String is immutable, converting it to StringBuffer or StringBuilder allows modifications.”. What type of conversion is this?

A

Convert String to StringBuffer and
StringBuilder

87
Q

“Since StringBuffer and StringBuilder are mutable, converting them to String gives an
immutable version.” What type of conversion is this?

A

Convert StringBuffer and StringBuilder
to String

88
Q

“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?

A

Convert StringBuffer to StringBuilder or
Vice Versa

89
Q

In Java, working with different string types—_____, _____, and _______—is important for handling text efficiently.

A

String, StringBuffer, StringBuilder

90
Q

_________ is immutable, meaning it cannot be changed once created, while ______ and ______ are mutable, allowing _______ without creating ________.

A

String, StringBuffer, StringBuilder, modifications, new instance.

91
Q

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.

A

thread-safe (synchronized), multi-threaded applications, synchronized, single-threaded