0
0
Javaprogramming~15 mins

String comparison in Java - Cheat Sheet & Quick Revision

Choose your learning style8 modes available
overviewRecall & Review
beginner
What does the == operator check when used with strings in Java?
The == operator checks if two string variables point to the exact same object in memory, not if their text content is the same.
touch_appClick to reveal answer
beginner
How do you correctly compare the content of two strings in Java?
Use the equals() method, like string1.equals(string2), to check if two strings have the same characters in the same order.
touch_appClick to reveal answer
intermediate
What is the difference between equals() and equalsIgnoreCase() methods in Java strings?
equals() compares strings considering uppercase and lowercase letters, while equalsIgnoreCase() ignores case differences when comparing.
touch_appClick to reveal answer
intermediate
What does the compareTo() method return when comparing two strings?
It returns an integer: zero if strings are equal, a negative number if the first string is lexicographically less, and a positive number if it is greater.
touch_appClick to reveal answer
beginner
Why should you avoid using == for string content comparison in Java?
Because == checks if two references point to the same object, not if their text is the same. This can lead to wrong results when strings have the same content but are different objects.
touch_appClick to reveal answer
Which method correctly compares the text content of two strings in Java?
Astring1.equalsIgnoreCase(string2) only
Bstring1 == string2
Cstring1.compareTo(string2) == 1
Dstring1.equals(string2)
What does string1 == string2 check in Java?
AIf both strings have the same characters
BIf both strings are the same object in memory
CIf both strings have the same length
DIf both strings are lexicographically equal
What will "Hello".equalsIgnoreCase("hello") return?
Atrue
Bfalse
Cnull
DThrows an error
If string1.compareTo(string2) returns a negative number, what does it mean?
Astring1 is lexicographically less than string2
Bstring1 is lexicographically greater than string2
Cstring1 and string2 are equal
Dstring1 is null
Which is the best way to compare two strings ignoring case?
Astring1 == string2
Bstring1.equals(string2)
Cstring1.equalsIgnoreCase(string2)
Dstring1.compareTo(string2) == 0
Explain how to compare two strings in Java to check if they have the same text.
Describe what the compareTo() method does when comparing two strings.