0
0
Kotlinprogramming~20 mins

Preconditions (require, check, error) in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Preconditions Mastery
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 code using require?
Consider this Kotlin function that uses require to check input. What will be printed when calling checkAge(15)?
Kotlin
fun checkAge(age: Int) {
    require(age >= 18) { "Age must be at least 18" }
    println("Age is valid: $age")
}

fun main() {
    checkAge(15)
}
AException in thread "main" java.lang.IllegalArgumentException: Age must be at least 18
BNo output, program ends silently
CException in thread "main" java.lang.IllegalStateException: Age must be at least 18
DAge is valid: 15
Attempts:
2 left
💡 Hint
Remember that require throws IllegalArgumentException if the condition is false.
Predict Output
intermediate
2:00remaining
What error does this Kotlin code raise using check?
Look at this Kotlin function using check. What error will be raised when calling validateNumber(-5)?
Kotlin
fun validateNumber(num: Int) {
    check(num > 0) { "Number must be positive" }
    println("Number is valid: $num")
}

fun main() {
    validateNumber(-5)
}
AException in thread "main" java.lang.IllegalArgumentException: Number must be positive
BException in thread "main" java.lang.IllegalStateException: Number must be positive
CNumber is valid: -5
DNo output, program ends silently
Attempts:
2 left
💡 Hint
Check throws IllegalStateException if the condition is false.
🔧 Debug
advanced
2:00remaining
Why does this Kotlin code using error() crash with this message?
This Kotlin function calls error("Invalid state"). What is the type of exception thrown and why?
Kotlin
fun fail() {
    error("Invalid state")
}

fun main() {
    fail()
}
AThrows java.lang.IllegalStateException with message "Invalid state" because error() always throws this exception.
BThrows java.lang.IllegalArgumentException with message "Invalid state" because error() throws this exception.
CThrows java.lang.RuntimeException with message "Invalid state" because error() throws a generic runtime exception.
DNo exception thrown, error() just logs the message.
Attempts:
2 left
💡 Hint
Check the Kotlin documentation for error() function behavior.
📝 Syntax
advanced
2:00remaining
Which Kotlin code snippet correctly uses require with a message lambda?
Select the code that compiles and runs without syntax errors.
Arequire(x > 0) -> { "x must be positive" }
Brequire(x > 0, "x must be positive")
Crequire(x > 0) { "x must be positive" }
Drequire(x > 0) "x must be positive"
Attempts:
2 left
💡 Hint
require can take a lambda that returns the message.
🚀 Application
expert
2:00remaining
How many items are in the list after filtering with require in this Kotlin code?
This Kotlin code filters a list using require inside a map. How many items remain after running processList()?
Kotlin
fun processList(): List<Int> {
    val numbers = listOf(1, 2, 3, 4, 5)
    return numbers.map {
        require(it % 2 == 1) { "Only odd numbers allowed" }
        it * 2
    }
}

fun main() {
    try {
        val result = processList()
        println(result.size)
    } catch (e: IllegalArgumentException) {
        println("Exception caught")
    }
}
A0
B3
C5
DException caught
Attempts:
2 left
💡 Hint
require throws exception on first even number, stopping the map.