Strings

0.0(0)
Studied by 0 people
call kaiCall Kai
Locked
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/136

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 8:03 PM on 9/21/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

137 Terms

1
New cards

String

An object that represents a sequence of characters.

2
New cards

Are Strings primitive types or objects?

Objects.

3
New cards

String immutability

Once a String object is created, its contents cannot be changed.

4
New cards

What happens when a String seems to be modified?

A new String object is created instead of changing the original.

5
New cards

How can you create a String using new?

String s = new String("Ricky");

6
New cards

How can you create a String using a literal?

String s = "Ricky";

7
New cards

String literal

Text enclosed in double quotes, such as "Hello".

8
New cards

How do you call a method on an object?

objectReference.methodName(parameterList)

9
New cards

What does length() return?

The number of characters in a String.

10
New cards

If String s = "Ricky", what does s.length() return?

5.

11
New cards

void method

A method that does not return a value.

12
New cards

Concatenation

Combining Strings by appending characters to the end of another String.

13
New cards

What method can concatenate Strings?

concat().

14
New cards

What operator is commonly used for String concatenation?

+.

15
New cards

What does "Hello" + "World!" produce?

"HelloWorld!"

16
New cards

What does "Hello " + "World!" produce?

"Hello World!"

17
New cards

What does s1.concat(s2) do?

Returns a new String containing s1 followed by s2.

18
New cards

Does concat() modify the original String?

No. Strings are immutable, so it returns a new String.

19
New cards

String pool

Special JVM memory area used to store and reuse String literals.

20
New cards

String interning

Reusing an existing String literal from the String pool instead of creating another identical object.

21
New cards

What happens when Java encounters a String literal already in the String pool?

It gives the variable a reference to the existing String object.

22
New cards

Where are String literals generally stored?

The String pool.

23
New cards

Where are Strings created with new stored?

The heap.

24
New cards

Are Strings created using new automatically interned?

No.

25
New cards

Heap

Memory area where objects created using new are stored.

26
New cards

What does new String("Hello") do?

Creates a new String object on the heap.

27
New cards

charAt(int index)

Returns the character at the specified index of a String.

28
New cards

What index does the first character of a String have?

0.

29
New cards

If String s = "Hello", what is s.charAt(0)?

'H'.

30
New cards

If String s = "Hello", what is s.charAt(4)?

'o'.

31
New cards

What happens if charAt() uses an index outside the String?

An index-out-of-bounds exception occurs.

32
New cards

compareTo(String)

Compares two Strings lexicographically and returns 0 when their contents are equal.

33
New cards

What does compareTo() return when two Strings are equal?

0.

34
New cards

equals(Object)

Compares the contents of two Strings.

35
New cards

What does equals() return when two Strings contain the same sequence of characters?

true.

36
New cards

Is equals() case-sensitive?

Yes.

37
New cards

equalsIgnoreCase()

Compares String contents while ignoring uppercase and lowercase differences.

38
New cards

What does "hello".equals("Hello") return?

false.

39
New cards

What does "hello".equalsIgnoreCase("Hello") return?

true.

40
New cards

isEmpty()

Returns true when a String contains zero characters.

41
New cards

What does "".isEmpty() return?

true.

42
New cards

What does " ".isEmpty() return?

false.

43
New cards

Why is " ".isEmpty() false?

The String contains one space character.

44
New cards

System

A Java class.

45
New cards

System.out

An object representing standard output, usually the console.

46
New cards

println()

Prints output and then moves to a new line.

47
New cards

print()

Prints output without automatically moving to a new line.

48
New cards

What is the difference between print() and println()?

println() adds a newline; print() does not.

49
New cards

What does System.out.print("Hi"); do?

Prints Hi without automatically starting a new line.

50
New cards

What does System.out.println("Hi"); do?

Prints Hi and then starts a new line.

51
New cards

Escape sequence

A special character sequence beginning with a backslash that controls or represents special output.

52
New cards

What character begins a Java escape sequence?

Backslash \.

53
New cards

What does \n represent?

A newline.

54
New cards

What does \t represent?

A tab.

55
New cards

What does \" represent?

A double quotation mark inside a String.

56
New cards

What does \\ represent?

A single backslash character.

57
New cards

How do you include quotation marks inside a String?

Escape them with \", such as "He said \"Hi\"".

58
New cards

What does System.out.println("Hello\nWorld"); print?

Hello and World on separate lines.

59
New cards

What does System.out.print("A\tB"); do?

Prints A and B separated by a tab.

60
New cards

printf()

Prints formatted output using format specifiers.

61
New cards

Format specifier

A placeholder in printf that is replaced by a supplied value.

62
New cards

Why use printf instead of print or println?

To control how values are formatted in output.

63
New cards

What format specifier is commonly used for an int?

%d.

64
New cards

What format specifier is commonly used for a floating-point value?

%f.

65
New cards

What format specifier is commonly used for a String?

%s.

66
New cards

What does %.2f mean in printf?

Display a floating-point value with 2 digits after the decimal.

67
New cards

Does printf automatically add a newline?

No.

68
New cards

How can printf include a newline?

Use %n or \n in the format String.

69
New cards

What does System.out.printf("%d", 5); print?

5.

70
New cards

What does System.out.printf("%.2f", 3.14159); print?

3.14.

71
New cards

What does System.out.printf("%s", "Hello"); print?

Hello.

72
New cards

Variable aliasing

When multiple reference variables refer to the same object.

73
New cards

What do object variables store?

References to objects.

74
New cards

What do primitive variables store?

Their actual primitive values.

75
New cards

If String a = "Suzy"; String b = "Mark"; is a == b true?

false.

76
New cards

After a = b, what object does a reference?

The same String object referenced by b.

77
New cards

After a = b, what does a == b return?

true.

78
New cards

Why does a == b become true after a = b?

Both variables now contain the same object reference.

79
New cards

What does == compare for String objects?

Whether the two references point to the same object.

80
New cards

What does equals() compare for Strings?

The characters or contents of the Strings.

81
New cards

Main difference between == and equals() for Strings

== compares references; equals() compares contents.

82
New cards

Should == normally be used to compare String contents?

No. Use equals().

83
New cards

String c = new String("Mary"); String d = new String("Mary"); What is c == d?

false.

84
New cards

Why is c == d false when both contain "Mary"?

new creates two separate objects with different references.

85
New cards

String c = new String("Mary"); String d = new String("Mary"); What is c.equals(d)?

true.

86
New cards

Why is c.equals(d) true?

Both Strings contain the same sequence of characters.

87
New cards

Is equals() case-sensitive?

Yes.

88
New cards

What method compares Strings while ignoring case?

equalsIgnoreCase().

89
New cards

If String str1 = "Hello"; String str2 = "Hello"; what is str1 == str2?

true.

90
New cards

Why can two identical String literals have == return true?

Java may intern both literals so they reference the same String pool object.

91
New cards

If str1 = "Hello" and str3 = new String("Hello"), what is str1 == str3?

false.

92
New cards

If str1 = "Hello" and str3 = new String("Hello"), what is str1.equals(str3)?

true.

93
New cards

String str1 = "Hello"; String str2 = "Hello"; What is str1.equals(str2)?

true.

94
New cards

String str4 = "Hello".toLowerCase(); String str5 = "hello"; Is str4 == str5 guaranteed true?

No.

95
New cards

Why should you not use == to compare str4 and str5?

Their contents may match even though their references may differ.

96
New cards

How should str4 and str5 be compared for matching contents?

str4.equals(str5).

97
New cards

What does toLowerCase() do?

Returns a new String with letters converted to lowercase.

98
New cards

Does toLowerCase() modify the original String?

No. Strings are immutable.

99
New cards

If String s = "HELLO"; s.toLowerCase(); what is s afterward?

"HELLO".

100
New cards

Why does s remain "HELLO" after s.toLowerCase();?

The returned new String was not assigned anywhere.