0
0
Kotlinprogramming~10 mins

Kotlin test assertions - Interactive Code Practice

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

Complete the code to assert that two values are equal.

Kotlin
import kotlin.test.assertEquals

fun testSum() {
    val result = 2 + 3
    assertEquals([1], result)
}
Drag options to blanks, or click blank then click option'
A6
B4
C7
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using the actual result as the expected value.
Mixing up the order of arguments in assertEquals.
2fill in blank
medium

Complete the code to assert that a condition is true.

Kotlin
import kotlin.test.assertTrue

fun testPositive() {
    val number = 10
    assertTrue([1])
}
Drag options to blanks, or click blank then click option'
Anumber > 0
Bnumber <= 0
Cnumber == 0
Dnumber < 0
Attempts:
3 left
💡 Hint
Common Mistakes
Using a condition that is false.
Confusing greater than and less than signs.
3fill in blank
hard

Fix the error in the assertion that checks if a list is empty.

Kotlin
import kotlin.test.assertTrue

fun testEmptyList() {
    val list = listOf<Int>()
    assertTrue([1])
}
Drag options to blanks, or click blank then click option'
Alist.size > 0
Blist.isNotEmpty()
Clist.isEmpty()
Dlist.size == 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using isNotEmpty() which checks for non-empty lists.
Checking size incorrectly.
4fill in blank
hard

Fill both blanks to assert that a string contains a substring and is not empty.

Kotlin
import kotlin.test.assertTrue

fun testString() {
    val text = "Hello Kotlin"
    assertTrue(text.[1]("Kotlin") && text.[2]())
}
Drag options to blanks, or click blank then click option'
Acontains
BisEmpty
CisNotEmpty
DstartsWith
Attempts:
3 left
💡 Hint
Common Mistakes
Using isEmpty() instead of isNotEmpty().
Using startsWith() which only checks the start of the string.
5fill in blank
hard

Fill all three blanks to assert that a map has a key, the value is not null, and the value equals 42.

Kotlin
import kotlin.test.assertTrue

fun testMap() {
    val map = mapOf("answer" to 42)
    assertTrue(map.[1]("answer") && map["answer"] [2] null && map["answer"] [3] 42)
}
Drag options to blanks, or click blank then click option'
AcontainsKey
B!=
C==
DgetOrDefault
Attempts:
3 left
💡 Hint
Common Mistakes
Using getOrDefault instead of containsKey.
Using == null instead of != null.
Mixing up equality operators.