Recall & Review
beginner
What is a single-expression function in Kotlin?
A single-expression function is a function that returns the result of a single expression directly without using curly braces or the return keyword.
Click to reveal answer
beginner
How do you write a single-expression function that adds two numbers in Kotlin?
fun add(a: Int, b: Int) = a + b
Click to reveal answer
intermediate
Can single-expression functions have explicit return types in Kotlin?
Yes, you can specify the return type explicitly, but Kotlin can often infer it automatically.
Click to reveal answer
beginner
What is the benefit of using single-expression functions?
They make the code shorter, cleaner, and easier to read by removing unnecessary syntax like braces and return statements.
Click to reveal answer
beginner
Is this a valid single-expression function in Kotlin? <br>
fun greet() = println("Hello")Yes, it is valid. The function calls println as a single expression and returns Unit.
Click to reveal answer
Which of the following is a correct single-expression function in Kotlin?
✗ Incorrect
Option D uses the single-expression syntax with '=' and no braces, which is correct.
What is the return type of this function? <br>
fun isEven(n: Int) = n % 2 == 0
✗ Incorrect
The expression 'n % 2 == 0' returns a Boolean value.
Can a single-expression function have multiple statements inside it?
✗ Incorrect
Single-expression functions can only have one expression, no multiple statements.
Which keyword is NOT needed in a single-expression function?
✗ Incorrect
The 'return' keyword is not needed because the expression result is returned automatically.
What does this function return? <br>
fun greet() = println("Hi")✗ Incorrect
println returns Unit, so the function returns Unit.
Explain what a single-expression function is and how it differs from a regular function in Kotlin.
Think about how the function body is written and how the result is returned.
You got /4 concepts.
Write a single-expression function in Kotlin that checks if a number is positive.
Use a comparison expression like 'n > 0'.
You got /4 concepts.