0
0
Kotlinprogramming~10 mins

String comparison (equals, compareTo) in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if two strings are equal using the correct method.

Kotlin
val result = str1.[1](str2)
println(result)
Drag options to blanks, or click blank then click option'
AcompareTo
Bequals
C==
DequalsIgnoreCase
Attempts:
3 left
💡 Hint
Common Mistakes
Using compareTo instead of equals causes confusion because it returns an integer.
Using == which compares content in Kotlin, but is an operator not a method call.
2fill in blank
medium

Complete the code to compare two strings lexicographically and print the result.

Kotlin
val comparison = str1.[1](str2)
println(comparison)
Drag options to blanks, or click blank then click option'
AcompareTo
Bequals
C==
DequalsIgnoreCase
Attempts:
3 left
💡 Hint
Common Mistakes
Using equals instead of compareTo when needing order comparison.
Expecting compareTo to return a boolean.
3fill in blank
hard

Fix the error in the code to correctly check if two strings are equal ignoring case.

Kotlin
val isEqual = str1.[1](str2)
println(isEqual)
Drag options to blanks, or click blank then click option'
A==
BcompareTo
Cequals
DequalsIgnoreCase
Attempts:
3 left
💡 Hint
Common Mistakes
Using equals which is case sensitive.
Using compareTo which returns an integer, not boolean.
4fill in blank
hard

Fill both blanks to create a map of words to their lengths only if the length is greater than 3.

Kotlin
val lengths = buildMap<String, Int> {
  for ([1] in words) {
    if ([2] > 3) {
      this[[1]] = [2]
    }
  }
}
Drag options to blanks, or click blank then click option'
Aword
Blen(word)
Cword.length
Dword.size
Attempts:
3 left
💡 Hint
Common Mistakes
Using len(word) which is Python syntax, not Kotlin.
Using word.size which is not a valid property for String length in Kotlin.
5fill in blank
hard

Fill all three blanks to create a map of uppercase words to their lengths only if length is greater than 4.

Kotlin
val result = buildMap<String, Int> {
  for ([3] in words) {
    if ([2] > 4) {
      this[[1]] = [2]
    }
  }
}
Drag options to blanks, or click blank then click option'
Aword.uppercase()
Bword.length
Cword
Dword.lowercase()
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase() instead of uppercase() for the key.
Using incorrect loop variable names.