Bird
Raised Fist0
Kotlinprogramming~20 mins

Why testing matters in Kotlin - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Testing Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Kotlin test function?

Consider this Kotlin function that tests if a number is even. What will be printed when this test runs?

Kotlin
fun isEven(n: Int) = n % 2 == 0

fun testIsEven() {
    val testNumber = 4
    if (isEven(testNumber)) {
        println("Test passed")
    } else {
        println("Test failed")
    }
}

testIsEven()
ATest passed
BTest failed
CCompilation error
DNo output
Attempts:
2 left
💡 Hint

Think about what the function isEven returns for 4.

🧠 Conceptual
intermediate
1:30remaining
Why do we write tests in programming?

Which of the following is the main reason programmers write tests for their code?

ATo make the program look more complex
BTo make the code run faster
CTo check if the code works as expected and catch errors early
DTo avoid writing comments
Attempts:
2 left
💡 Hint

Think about what testing helps prevent before the program is used.

❓ Predict Output
advanced
2:00remaining
What error does this Kotlin test code raise?

Look at this Kotlin test code. What error will it cause when run?

Kotlin
fun divide(a: Int, b: Int): Int = a / b

fun testDivide() {
    val result = divide(10, 0)
    println("Result is $result")
}

testDivide()
ANo error, prints 'Result is 0'
BArithmeticException: / by zero
CCompilation error
DNullPointerException
Attempts:
2 left
💡 Hint

What happens if you divide a number by zero in Kotlin?

🚀 Application
advanced
1:30remaining
How many tests are run in this Kotlin test suite?

Given this Kotlin test suite, how many tests will be executed?

Kotlin
fun testAdd() { println("Add test") }
fun testSubtract() { println("Subtract test") }
fun testMultiply() { println("Multiply test") }

fun runTests() {
    testAdd()
    testSubtract()
}

runTests()
A2
B3
C1
D0
Attempts:
2 left
💡 Hint

Look at which test functions are called inside runTests().

🔧 Debug
expert
2:30remaining
What is the value of 'passed' after running this Kotlin test code?

Consider this Kotlin code that tests if numbers are positive. What is the value of the variable passed after running runTests()?

Kotlin
var passed = 0

fun isPositive(n: Int) = n > 0

fun testPositive(n: Int) {
    if (isPositive(n)) {
        passed += 1
    }
}

fun runTests() {
    val numbers = listOf(1, -1, 0, 5)
    for (num in numbers) {
        testPositive(num)
    }
}

runTests()
A1
B3
C4
D2
Attempts:
2 left
💡 Hint

Count how many numbers in the list are greater than zero.

Practice

(1/5)
1. Why is testing important when writing Kotlin programs?
easy
A. It makes the program run faster automatically.
B. It allows the program to work without any user input.
C. It removes the need to write comments in code.
D. It helps find mistakes early and keeps the program working well.

Solution

  1. Step 1: Understand the purpose of testing

    Testing is used to find mistakes and ensure the program behaves as expected.
  2. Step 2: Compare options with testing goals

    Only It helps find mistakes early and keeps the program working well. correctly states that testing finds mistakes early and keeps the program working well.
  3. Final Answer:

    It helps find mistakes early and keeps the program working well. -> Option D
  4. Quick Check:

    Testing finds mistakes early = B [OK]
Hint: Testing finds bugs early and keeps code reliable [OK]
Common Mistakes:
  • Thinking testing makes code faster
  • Believing testing replaces comments
  • Assuming testing removes user input
2. Which Kotlin syntax correctly defines a simple test function named testSum?
easy
A. fun testSum() { /* test code here */ }
B. def testSum() { /* test code here */ }
C. function testSum() { /* test code here */ }
D. testSum fun() { /* test code here */ }

Solution

  1. Step 1: Recall Kotlin function syntax

    Kotlin functions start with the keyword fun, followed by the function name and parentheses.
  2. Step 2: Match options to Kotlin syntax

    Only fun testSum() { /* test code here */ } uses the correct Kotlin syntax: fun testSum() { ... }.
  3. Final Answer:

    fun testSum() { /* test code here */ } -> Option A
  4. Quick Check:

    Kotlin functions start with fun = A [OK]
Hint: Kotlin functions always start with 'fun' keyword [OK]
Common Mistakes:
  • Using 'def' like in Python
  • Using 'function' like in JavaScript
  • Placing 'fun' after the function name
3. What will be the output of this Kotlin test function?
fun testMultiply() {
    val result = 3 * 4
    if (result == 12) {
        println("Test passed")
    } else {
        println("Test failed")
    }
}
testMultiply()
medium
A. Test passed
B. No output
C. Test failed
D. Compilation error

Solution

  1. Step 1: Calculate the multiplication

    The variable result is assigned 3 * 4, which equals 12.
  2. Step 2: Check the if condition

    The condition result == 12 is true, so it prints "Test passed".
  3. Final Answer:

    Test passed -> Option A
  4. Quick Check:

    3 * 4 = 12, prints 'Test passed' = C [OK]
Hint: Calculate expressions before checking conditions [OK]
Common Mistakes:
  • Assuming multiplication is wrong
  • Ignoring the if condition
  • Thinking no output occurs
4. This Kotlin test function is supposed to check if a number is even. What is wrong?
fun testIsEven() {
    val number = 5
    if (number % 2 = 0) {
        println("Even")
    } else {
        println("Odd")
    }
}
testIsEven()
medium
A. The variable 'number' should be a String
B. Missing parentheses around the if condition
C. Using single '=' instead of '==' in the if condition
D. The function name should start with 'is'

Solution

  1. Step 1: Identify the if condition syntax error

    The condition uses a single '=' which is assignment, not comparison.
  2. Step 2: Correct the comparison operator

    It should use '==' to compare if number % 2 equals 0.
  3. Final Answer:

    Using single '=' instead of '==' in the if condition -> Option C
  4. Quick Check:

    Comparison needs '==' not '=' = A [OK]
Hint: Use '==' for comparison, '=' is assignment [OK]
Common Mistakes:
  • Confusing '=' with '=='
  • Thinking parentheses are missing
  • Believing variable type is wrong
5. You want to write a Kotlin test that checks if a function calculateDiscount returns the correct discount for a price. Which approach best shows why testing matters?
hard
A. Write a test that changes the price variable but does not check the output.
B. Write a test that calls calculateDiscount(100) and checks if the result is 10.
C. Write a test that always returns true without calling the function.
D. Write a test that prints the price without checking the result.

Solution

  1. Step 1: Understand the goal of testing functions

    Tests should call the function and verify if the output matches expected results.
  2. Step 2: Evaluate each option

    Only Write a test that calls calculateDiscount(100) and checks if the result is 10. calls the function and checks the result, which helps find mistakes and trust the code.
  3. Final Answer:

    Write a test that calls calculateDiscount(100) and checks if the result is 10. -> Option B
  4. Quick Check:

    Test calls function and checks output = D [OK]
Hint: Tests must check function output against expected values [OK]
Common Mistakes:
  • Writing tests that don't check results
  • Returning true without testing
  • Changing variables without verification
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard