Topic 9.2: Working with Selected classes from the Java API - Create and manipulate Strings Flashcards

1
Q

What is string interning and how does it optimize memory usage?

A

This is the process of storing string literals in a string pool to optimize memory usage.

When encountering a string literal, the JVM checks if it already exists in the pool. If it does, a reference to the existing string is returned. If not, a new string object is created and added to the pool.

This ensures that only one copy of each unique string exists in memory, reducing memory footprint and enhancing performance.

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

The equalsIgnoreCase() method can be used for this

A

What method can be used for case-insensitive string comparison in Java?

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

To achieve this you can use the substring() method

String text = "Hello, World!";
String substring = text.substring(7);
A

How can you extract a substring from a string in Java?

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

To achieve this, you can use the startsWith() method.

String text = "Hello, World!";
boolean startsWithHello = text.startsWith("Hello");
A

How can you check if a string starts with a specific prefix?

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

To achieve this you can use the toUpperCase() method

String text = "Hello, World!";
String uppercase = text.toUpperCase();
A

How can you convert a string to uppercase in Java?

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

How can you convert a string to uppercase in Java?

A

To achieve this you can use the toUpperCase() method

String text = "Hello, World!";
String uppercase = text.toUpperCase();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can you create strings in Java using string literals or the new keyword?

A

String literals involve enclosing characters within double quotation marks, such as “Hello, world!”. The new keyword can be used with the String class constructor, as in new String(“John”).

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

To achieve this you can use the trim() method

String text = "   Hello, World!   ";
String trimmed = text.trim();
A

How can you remove leading and trailing whitespace from a string in Java?

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

How can you check if a string starts with a specific prefix?

A

To achieve this, you can use the startsWith() method.

String text = "Hello, World!";
boolean startsWithHello = text.startsWith("Hello");
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How can you search for the first occurrence of a substring within a string in Java?

A

To achieve this you can use the indexOf() method, which takes the substring as a parameter and returns the index of the first occurrence. If the substring is not found, it returns -1.

For example, given the string “Hello, World!”, calling indexOf(“o”) would return 4.

String text = "Hello, World!";
int index1 = text.indexOf("o");
int index2 = text.indexOf("XYZ");
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

To achieve this you can use the replace() method with a single character as the target and the replacement value

For example, given the string “Hello, World!”, calling replace(‘o’, ‘e’) would return “Helle, Werld!”.

String text = "Hello, World!";
String replaced = text.replace('o', 'e');
A

How can you replace a character within a string in Java?

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

How can you find the index of the last occurrence of a specific substring within a string?

A

To achieve this, you can use the lastIndexOf() method.

String text = "Hello, World!";
int lastIndex = text.lastIndexOf("o");
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Behaviour of this:

The replacement proceeds from the beginning of the string to the end. For example, replacing “aa” with “b” in the string “aaa” will result in “ba” rather than “ab”.

A

What is the behavior of the replace() method in Java?

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

To acheive this you can use the split() method, which takes a delimiter as a parameter and returns an array of strings.

For example, given the string “Hello, World!”, calling split(“, “) would return an array containing [“Hello”, “World!”].

String text = "Hello, World!";
String[] words = text.split(", ");
A

How can you split a string into an array of substrings in Java?

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

How can you extract a substring from a string in Java?

A

To achieve this you can use the substring() method

String text = "Hello, World!";
String substring = text.substring(7);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How can you convert other data types to strings?

A

This can be achieved using the valueOf() or toString() methods.

For example, String.valueOf(10) or Integer.toString(5).

17
Q

How can you remove leading and trailing whitespace from a string in Java?

A

To achieve this you can use the trim() method

String text = "   Hello, World!   ";
String trimmed = text.trim();
18
Q

To achieve this you can use the indexOf() method, which takes the substring as a parameter and returns the index of the first occurrence. If the substring is not found, it returns -1.

For example, given the string “Hello, World!”, calling indexOf(“o”) would return 4.

String text = "Hello, World!";
int index1 = text.indexOf("o");
int index2 = text.indexOf("XYZ");
A

How can you search for the first occurrence of a substring within a string in Java?

19
Q

To achieve this, you can use the endsWith() method.

String text = "Hello, World!";
boolean endsWithWorld = text.endsWith("World!");
A

How can you check if a string ends with a specific suffix?

20
Q

String literals involve enclosing characters within double quotation marks, such as “Hello, world!”. The new keyword can be used with the String class constructor, as in new String(“John”).

A

How can you create strings in Java using string literals or the new keyword?

21
Q

To achieve this you can use the toLowerCase() method

String text = "Hello, World!";
String lowercase = text.toLowerCase();
A

How can you convert a string to lowercase in Java?

22
Q

This can be acieved using methods like parseInt(), parseDouble(), etc.

For example, Integer.parseInt("10") or Double.parseDouble("3.14").

A

How can you parse strings into other data types?

23
Q

How can you parse strings into other data types?

A

This can be acieved using methods like parseInt(), parseDouble(), etc.

For example, Integer.parseInt("10") or Double.parseDouble("3.14").

24
Q

The format() method can be used to format strings based on patterns.

String name = "Alice";
int age = 25;
String formatted = String.format("My name is %s and I am %d years old.", name, age);
A

What method can be used to format strings based on patterns?

25
Q

How can you split a string into an array of substrings in Java?

A

To acheive this you can use the split() method, which takes a delimiter as a parameter and returns an array of strings.

For example, given the string “Hello, World!”, calling split(“, “) would return an array containing [“Hello”, “World!”].

String text = "Hello, World!";
String[] words = text.split(", ");
26
Q

How can you replace substrings within a string in Java?

A

To achieve this you can use the replace() method with a substring as the target and the replacement value.

For example, given the string “Hello, World!”, calling replace(“Hello”, “Hi”) would return “Hi, World!”.

String text = "Hello, World!";
String replaced = text.replace("Hello", "Hi");
27
Q

How can you convert a string to lowercase in Java?

A

To achieve this you can use the toLowerCase() method

String text = "Hello, World!";
String lowercase = text.toLowerCase();
28
Q

To achieve this you can use the replace() method with a substring as the target and the replacement value.

For example, given the string “Hello, World!”, calling replace(“Hello”, “Hi”) would return “Hi, World!”.

String text = "Hello, World!";
String replaced = text.replace("Hello", "Hi");
A

How can you replace substrings within a string in Java?

29
Q

What method can be used to format strings based on patterns?

A

The format() method can be used to format strings based on patterns.

String name = "Alice";
int age = 25;
String formatted = String.format("My name is %s and I am %d years old.", name, age);
30
Q

What is the behavior of the replace() method in Java?

A

Behaviour of this:

The replacement proceeds from the beginning of the string to the end. For example, replacing “aa” with “b” in the string “aaa” will result in “ba” rather than “ab”.

31
Q

This is the process of storing string literals in a string pool to optimize memory usage.

When encountering a string literal, the JVM checks if it already exists in the pool. If it does, a reference to the existing string is returned. If not, a new string object is created and added to the pool.

This ensures that only one copy of each unique string exists in memory, reducing memory footprint and enhancing performance.

A

What is string interning and how does it optimize memory usage?

32
Q

This can be achieved using the valueOf() or toString() methods.

For example, String.valueOf(10) or Integer.toString(5).

A

How can you convert other data types to strings?

33
Q

How can you check if a string ends with a specific suffix?

A

To achieve this, you can use the endsWith() method.

String text = "Hello, World!";
boolean endsWithWorld = text.endsWith("World!");
34
Q

How can you replace a character within a string in Java?

A

To achieve this you can use the replace() method with a single character as the target and the replacement value

For example, given the string “Hello, World!”, calling replace(‘o’, ‘e’) would return “Helle, Werld!”.

String text = "Hello, World!";
String replaced = text.replace('o', 'e');
35
Q

What method can be used for case-insensitive string comparison in Java?

A

The equalsIgnoreCase() method can be used for this

36
Q

To achieve this, you can use the lastIndexOf() method.

String text = "Hello, World!";
int lastIndex = text.lastIndexOf("o");
A

How can you find the index of the last occurrence of a specific substring within a string?