0
0
Kotlinprogramming~5 mins

String comparison (equals, compareTo) in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the equals() function do when comparing two strings in Kotlin?
The equals() function checks if two strings have exactly the same characters in the same order. It returns true if they are equal, otherwise false.
Click to reveal answer
beginner
How does compareTo() work when comparing two strings?
The compareTo() function compares two strings lexicographically (like dictionary order). It returns a negative number if the first string is less, zero if they are equal, and a positive number if the first string is greater.
Click to reveal answer
intermediate
What is the difference between == and equals() when comparing strings in Kotlin?
In Kotlin, == checks structural equality and calls equals() under the hood for strings. So, == and equals() behave the same for string content comparison.
Click to reveal answer
beginner
What does a compareTo() result of zero mean?
A compareTo() result of zero means the two strings are exactly equal in content and order.
Click to reveal answer
beginner
If str1.compareTo(str2) returns a positive number, what does it tell you about str1 and str2?
It means str1 comes after str2 in dictionary order. So, str1 is 'greater' than str2.
Click to reveal answer
What does "apple".equals("Apple") return in Kotlin?
Atrue
Bfalse
Cnull
DThrows an error
What will "cat".compareTo("car") return?
AA positive number
BZero
CA negative number
DThrows an error
Which operator in Kotlin calls equals() when comparing strings?
A==
B===
C!=
D+=
If str1.compareTo(str2) returns 0, what can you say about str1 and str2?
Astr1 is lexicographically less than str2
Bstr1 is lexicographically greater than str2
Cstr1 and str2 are equal
Dstr1 and str2 are different types
Which method would you use to check if two strings have the same content in Kotlin?
AcompareTo()
BtoString()
ChashCode()
Dequals()
Explain how equals() and compareTo() differ when comparing strings in Kotlin.
Think about equality vs order.
You got /4 concepts.
    Describe what the result of compareTo() tells you about two strings.
    Imagine comparing words in a dictionary.
    You got /4 concepts.