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
✗ Incorrect
The equals() function is case-sensitive, so "apple" is not equal to "Apple".
What will "cat".compareTo("car") return?
AA positive number
BZero
CA negative number
DThrows an error
✗ Incorrect
Since 't' comes after 'r' in the alphabet, "cat" is greater than "car", so compareTo returns a positive number.
Which operator in Kotlin calls equals() when comparing strings?
A==
B===
C!=
D+=
✗ Incorrect
The == operator checks structural equality and calls equals() for strings.
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
✗ Incorrect
A zero result means the two strings are exactly equal.
Which method would you use to check if two strings have the same content in Kotlin?
AcompareTo()
BtoString()
ChashCode()
Dequals()
✗ Incorrect
The equals() method checks if two strings have the same content.
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.
Practice
(1/5)
1. Which Kotlin method should you use to check if two strings have exactly the same characters and case? val a = "Hello" val b = "hello"
easy
A. a.equals(b)
B. a.compareTo(b)
C. a === b
D. a.equals(b, ignoreCase = true)
Solution
Step 1: Understand equals() method
The equals() method checks if two strings are exactly the same including case.
Step 2: Compare equals() with other options
compareTo() returns an order number, === checks reference equality (not content), equals(ignoreCase=true) ignores case, so only equals() matches exact case-sensitive check.
Final Answer:
a.equals(b) -> Option A
Quick Check:
Use equals() for exact string match [OK]
Hint: Use equals() for exact case-sensitive string match [OK]
Common Mistakes:
Using compareTo() to check equality
Assuming === checks content equality
Using equals(ignoreCase=true) when case matters
2. Which of the following is the correct syntax to compare two strings str1 and str2 for equality ignoring case in Kotlin?
easy
A. str1.equals(str2, ignoreCase = true)
B. str1.compareTo(str2, ignoreCase = true)
C. str1.equals(str2)
D. str1 == str2
Solution
Step 1: Recall equals() with ignoreCase parameter
In Kotlin, equals() can take ignoreCase=true to compare strings ignoring case differences.
Step 2: Check other options
A: equals() without ignoreCase is case-sensitive, B: compareTo(ignoreCase=true) returns Int not Boolean, D: == is case-sensitive.
Final Answer:
str1.equals(str2, ignoreCase = true) -> Option A
Quick Check:
Use equals(ignoreCase=true) to ignore case [OK]
Hint: Use equals(ignoreCase=true) to compare ignoring case [OK]
Common Mistakes:
Using compareTo(ignoreCase=true) directly (returns Int, not Boolean)
Using == which is case-sensitive
Forgetting to set ignoreCase=true
3. What will be the output of the following Kotlin code?
val s1 = "apple" val s2 = "banana" println(s1.compareTo(s2))
medium
A. A positive number
B. A negative number
C. 0
D. Compilation error
Solution
Step 1: Understand compareTo() behavior
compareTo() returns a negative number if the first string is lexicographically less than the second.
Step 2: Compare "apple" and "banana" lex order
"apple" comes before "banana" alphabetically, so compareTo returns a negative number.
Final Answer:
A negative number -> Option B
Quick Check:
"apple" < "banana" means compareTo() < 0 [OK]
Hint: compareTo() < 0 means first string is before second [OK]
Common Mistakes:
Assuming compareTo() returns boolean
Confusing positive and negative return values
Thinking compareTo() returns 0 for different strings
4. The following Kotlin code is intended to check if two strings are equal ignoring case, but it does not compile. What is the error?
val a = "Test" val b = "test" if (a.compareTo(b, ignoreCase = true)) { println("Equal") } else { println("Not Equal") }
medium
A. Missing parentheses in if condition
B. ignoreCase is not a valid parameter for compareTo()
C. Strings cannot be compared with compareTo()
D. compareTo() returns Int, cannot be used as Boolean
Solution
Step 1: Check compareTo() return type
compareTo() returns an Int, not a Boolean, so it cannot be used directly in if condition.
Step 2: Understand how to check equality with compareTo()
To check equality ignoring case, compareTo() == 0 should be used, not just compareTo() alone.
Final Answer:
compareTo() returns Int, cannot be used as Boolean -> Option D
Quick Check:
compareTo() returns Int, use == 0 to check equality [OK]
Hint: compareTo() returns Int; compare with 0 for equality [OK]
Common Mistakes:
Using compareTo() directly as Boolean
Forgetting to compare result with 0
Assuming ignoreCase is invalid parameter (it is valid)
5. You want to sort a list of strings ignoring case in Kotlin. Which code snippet correctly sorts the list val fruits = listOf("Banana", "apple", "Cherry") ignoring case?
hard
A. fruits.sortedBy { it }
B. fruits.sortedBy(String.CASE_INSENSITIVE_ORDER)
C. fruits.sortedWith(String.CASE_INSENSITIVE_ORDER)
D. fruits.sorted()
Solution
Step 1: Understand sorting ignoring case
To sort strings ignoring case, use a comparator that compares strings case-insensitively.
Step 2: Check options for case-insensitive sorting
A and D are case-sensitive. C: sortedBy expects key selector, not Comparator (compile error). B uses built-in case-insensitive Comparator correctly.
Final Answer:
fruits.sortedWith(String.CASE_INSENSITIVE_ORDER) -> Option C
Quick Check:
Use sortedWith(String.CASE_INSENSITIVE_ORDER) for case-insensitive sort [OK]
Hint: Use sortedWith(String.CASE_INSENSITIVE_ORDER) to ignore case in sort [OK]
Common Mistakes:
Using sorted() which is case-sensitive
Using sortedBy { it } which is case-sensitive
Confusing sortedBy (selector) with sortedWith (Comparator)
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.