0
0
Kotlinprogramming~20 mins

Single-expression functions in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Single-expression Kotlin Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
Output of a single-expression function with string interpolation
What is the output of this Kotlin code?
Kotlin
fun greet(name: String) = "Hello, $name!"

fun main() {
    println(greet("Anna"))
}
AHello, Anna!
BHello, name!
Cgreet(Anna)
DError: Missing return type
Attempts:
2 left
💡 Hint
Look at how the function returns a string using $name inside quotes.
Predict Output
intermediate
1:30remaining
Return type inference in single-expression functions
What will this Kotlin program print?
Kotlin
fun square(x: Int) = x * x

fun main() {
    println(square(5))
}
A0
BError: Return type required
C25
D5
Attempts:
2 left
💡 Hint
The function returns the product of x times x.
🔧 Debug
advanced
2:00remaining
Identify the error in this single-expression function
What error does this Kotlin code produce?
Kotlin
fun add(a: Int, b: Int) = return a + b

fun main() {
    println(add(3, 4))
}
AOutput: 7
BSyntaxError: Unexpected 'return' in single-expression function
CTypeError: Missing return type
DRuntimeException: Null pointer
Attempts:
2 left
💡 Hint
Single-expression functions do not use the 'return' keyword explicitly.
Predict Output
advanced
1:30remaining
Output of a single-expression function with conditional expression
What is the output of this Kotlin code?
Kotlin
fun max(a: Int, b: Int) = if (a > b) a else b

fun main() {
    println(max(10, 20))
}
A10
BError: Missing braces in if expression
Ctrue
D20
Attempts:
2 left
💡 Hint
The function returns the greater of two numbers using if-else.
🧠 Conceptual
expert
2:00remaining
Understanding single-expression function behavior with side effects
Consider this Kotlin code. What is the output when main() runs?
Kotlin
var counter = 0
fun increment() = counter++

fun main() {
    println(increment())
    println(increment())
}
A
0
1
B
1
2
C
0
0
DError: Variable counter must be immutable
Attempts:
2 left
💡 Hint
The post-increment operator returns the value before incrementing.