0
0
Kotlinprogramming~20 mins

Why Kotlin over Java - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kotlin Mastery: Why Kotlin over Java
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 null safety?
Consider this Kotlin code snippet that uses null safety features. What will it print?
Kotlin
fun main() {
    val name: String? = null
    val length = name?.length ?: -1
    println(length)
}
A0
B-1
CThrows NullPointerException
Dnull
Attempts:
2 left
💡 Hint
Look at the Elvis operator ?: and how it handles null values.
🧠 Conceptual
intermediate
2:00remaining
Why does Kotlin reduce boilerplate compared to Java?
Which Kotlin feature helps reduce the amount of boilerplate code compared to Java?
AKotlin forces manual memory management
BKotlin requires explicit getters and setters for all properties
CKotlin does not support lambdas
DData classes automatically generate common methods like equals and toString
Attempts:
2 left
💡 Hint
Think about how Kotlin handles simple classes with properties.
🔧 Debug
advanced
2:00remaining
Identify the error in this Kotlin code using coroutines
What error will this Kotlin coroutine code produce when run?
Kotlin
import kotlinx.coroutines.*

fun main() {
    GlobalScope.launch {
        delay(1000L)
        println("World!")
    }
    println("Hello,")
}
APrints "Hello," only because main exits before coroutine runs
BPrints "Hello," then "World!" after 1 second
CCompilation error due to missing runBlocking
DRuntime exception: CoroutineScope not initialized
Attempts:
2 left
💡 Hint
Consider what happens to the main thread after launching a coroutine.
📝 Syntax
advanced
2:00remaining
Which Kotlin syntax correctly declares a read-only list?
Choose the correct Kotlin syntax to declare a read-only list of integers containing 1, 2, and 3.
Aval list = listOf(1, 2, 3)
Bval list = mutableListOf(1, 2, 3)
Cval list = arrayListOf(1, 2, 3)
Dval list = List(1, 2, 3)
Attempts:
2 left
💡 Hint
Read-only lists use a specific Kotlin function.
🚀 Application
expert
3:00remaining
What is the output of this Kotlin code using sealed classes?
Given the sealed class hierarchy below, what will be printed when calling describeShape with a Circle(5.0)?
Kotlin
sealed class Shape
class Circle(val radius: Double) : Shape()
class Rectangle(val width: Double, val height: Double) : Shape()

fun describeShape(shape: Shape): String = when(shape) {
    is Circle -> "Circle with radius ${shape.radius}"
    is Rectangle -> "Rectangle with width ${shape.width} and height ${shape.height}"
}

fun main() {
    println(describeShape(Circle(5.0)))
}
ARuntime error: Unhandled type
BRectangle with width 5.0 and height 5.0
CCircle with radius 5.0
DCompilation error: when expression is not exhaustive
Attempts:
2 left
💡 Hint
Sealed classes allow exhaustive when expressions without else.