Bird
Raised Fist0
Kotlinprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is a key feature of a single-expression function in Kotlin?
easy
A. It returns the value of the expression after the equals sign automatically.
B. It requires a block body with explicit return statements.
C. It cannot have parameters.
D. It must be declared inside a class.

Solution

  1. Step 1: Understand single-expression function syntax

    Single-expression functions use an equals sign followed by an expression, without braces.
  2. Step 2: Identify the return behavior

    They automatically return the value of that expression without needing an explicit return keyword.
  3. Final Answer:

    It returns the value of the expression after the equals sign automatically. -> Option A
  4. Quick Check:

    Single-expression function = auto return [OK]
Hint: Look for '=' and no braces for single-expression functions [OK]
Common Mistakes:
  • Thinking explicit return is needed
  • Assuming parameters are not allowed
  • Believing it must be inside a class
2. Which of the following is the correct syntax for a single-expression function in Kotlin that adds two integers?
easy
A. fun add(a: Int, b: Int) { return a + b }
B. fun add(a: Int, b: Int) = a + b
C. fun add(a: Int, b: Int): Int { a + b }
D. fun add(a: Int, b: Int): Int => a + b

Solution

  1. Step 1: Identify single-expression function syntax

    It uses '=' followed by the expression without braces or explicit return.
  2. Step 2: Check each option

    fun add(a: Int, b: Int) = a + b matches this syntax exactly: fun add(a: Int, b: Int) = a + b.
  3. Final Answer:

    fun add(a: Int, b: Int) = a + b -> Option B
  4. Quick Check:

    Equals sign and expression = correct syntax [OK]
Hint: Single-expression functions use '=' not braces [OK]
Common Mistakes:
  • Using braces with return in single-expression functions
  • Using '=>' instead of '='
  • Omitting '=' sign
3. What will be the output of this Kotlin code?
fun square(x: Int) = x * x
println(square(4))
medium
A. 16
B. 4
C. x * x
D. Compilation error

Solution

  1. Step 1: Understand the function behavior

    The function square returns the product of x times x.
  2. Step 2: Calculate the output for input 4

    4 * 4 = 16, so square(4) returns 16, which is printed.
  3. Final Answer:

    16 -> Option A
  4. Quick Check:

    4 squared = 16 [OK]
Hint: Multiply input by itself for square function [OK]
Common Mistakes:
  • Expecting the expression as a string output
  • Confusing input with output
  • Thinking it causes a compile error
4. Identify the error in this Kotlin single-expression function:
fun greet(name: String) = {
    "Hello, $name!"
}
medium
A. Missing return type declaration.
B. String interpolation is invalid.
C. Using braces {} after equals sign is incorrect.
D. Function must have no parameters.

Solution

  1. Step 1: Check single-expression function syntax

    Single-expression functions use '=' followed by an expression without braces.
  2. Step 2: Identify the error

    The braces after '=' create a lambda block, which is not allowed here; it should be just the expression.
  3. Final Answer:

    Using braces {} after equals sign is incorrect. -> Option C
  4. Quick Check:

    No braces allowed after '=' in single-expression functions [OK]
Hint: No braces after '=' in single-expression functions [OK]
Common Mistakes:
  • Adding braces after '='
  • Thinking return type is mandatory
  • Misusing string interpolation syntax
5. You want to write a Kotlin single-expression function that returns the length of a string only if it is not empty; otherwise, it returns zero. Which of these is correct?
hard
A. fun lengthOrZero(s: String) = s.length ?: 0
B. fun lengthOrZero(s: String) { if (s.isNotEmpty()) return s.length else return 0 }
C. fun lengthOrZero(s: String): Int = { if (s.isNotEmpty()) s.length else 0 }
D. fun lengthOrZero(s: String) = if (s.isNotEmpty()) s.length else 0

Solution

  1. Step 1: Understand the requirement

    The function must return length if string is not empty, else zero, in a single expression.
  2. Step 2: Check each option

    fun lengthOrZero(s: String) = if (s.isNotEmpty()) s.length else 0 uses a single-expression function with an if-else expression correctly. fun lengthOrZero(s: String) { if (s.isNotEmpty()) return s.length else return 0 } uses braces and explicit returns, not single-expression. fun lengthOrZero(s: String): Int = { if (s.isNotEmpty()) s.length else 0 } wrongly uses braces after '='. fun lengthOrZero(s: String) = s.length ?: 0 misuses the Elvis operator on Int.
  3. Final Answer:

    fun lengthOrZero(s: String) = if (s.isNotEmpty()) s.length else 0 -> Option D
  4. Quick Check:

    If-else expression after '=' = correct single-expression function [OK]
Hint: Use if-else expression directly after '=' for conditions [OK]
Common Mistakes:
  • Using braces after '='
  • Trying to use return inside single-expression function
  • Misusing Elvis operator on non-nullable Int
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