Topic 3.2: Using Operators and Decision Constructs - Test equality between Strings and other objects using == and equals () Flashcards

1
Q

Can the == operator be reliably used to compare String objects?

A

No, using the == operator to compare String objects may not always yield the expected results.

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

What is the output of the following code?

String str1 = "Hello";
String str2 = new String("Hello");
boolean isEqual = str1.equals(str2);
System.out.println(isEqual);
A

The output is true.

Explanation: In this example, the equals() method is used to compare the character sequences of str1 and str2. Despite str2 being created using the new keyword, the equals() method checks the actual contents of the Strings, resulting in true since both have the same characters.

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

What does it mean when
two objects have value/content equality?

A

In this situation, it means that their actual contents are the same.

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

In this situation, it means that their actual contents are the same.

A

What does it mean when
two objects have value/content equality?

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

What does the == operator test for?

A

This operator tests for reference equality.

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

Which class is the equals() method inherited from?

A

The equals() method is inherited from the Object class.

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

Instead of this operator the equals() method should be used to compare String objects for content equality.

A

What should be used instead of the == operator to compare String objects?

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

This method tests for value/content equality.

A

What does the
equals() method
test for?

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

What is the output of the following code?

String str1 = "Hello";
String str2 = "Hi";
boolean isEqual = str1.equals(str2);
System.out.println(isEqual);
A

The output is false.

Explanation: In this example, the equals() method is used to compare the content of str1 and str2. Since the content of str1 is “Hello” and the content of str2 is “Hi”, they are different strings, resulting in false when comparing them with equals().

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

What does it mean when two objects refer to the same memory location?

A

When two objects refer to the same memory location, it means that they are the same object.

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

Can the equals() method be overridden by classes?

A

Yes, classes can override the equals() method to provide their own implementation of equality comparison.

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

What should be used instead of the == operator to compare String objects?

A

Instead of this operator the equals() method should be used to compare String objects for content equality.

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

What happens when we concatenate Strings using the + operator?

A

When we concatenate Strings using the + operator, a new String object is created.

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

What is the output of the following code?

String str1 = "Hello";
String str2 = new String("Hello");
boolean isEqual = str1 == str2;
System.out.println(isEqual);
A

The output is false.

Explanation: In this example, the str1 and str2 variables are compared using the == operator. Since str2 is created using the new keyword, it will create a new String object in memory. Therefore, str1 and str2 have different references, resulting in false when comparing them with ==.

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

What is the output of the following code?

String str1 = "Hello";
String str2 = "Hello";
boolean isEqual = str1 == str2;
System.out.println(isEqual);
A

The output is true.

Explanation: In this example, the == operator is used to test if the references of str1 and str2 are the same. Since both Strings are created using the same literal “Hello”, they refer to the same object in the String pool, resulting in true when comparing them with ==.

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

What does the
equals() method
test for?

A

This method tests for value/content equality.

17
Q

This operator tests for reference equality.

A

What does the == operator test for?