Challenge - 5 Problems
Local Functions Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nested local function call
What is the output of this Kotlin code with a local function inside another function?
Kotlin
fun outer() { fun inner() { println("Hello from inner") } inner() println("Hello from outer") } fun main() { outer() }
Attempts:
2 left
💡 Hint
Remember that local functions can be called inside their enclosing function.
✗ Incorrect
The local function 'inner' is called first, printing its message, then the outer function prints its message.
❓ Predict Output
intermediate2:00remaining
Value returned by nested local function
What value does the function 'outer' return when called?
Kotlin
fun outer(): Int { fun inner(x: Int): Int { return x * 2 } return inner(5) + 3 } fun main() { println(outer()) }
Attempts:
2 left
💡 Hint
Calculate inner(5) first, then add 3.
✗ Incorrect
inner(5) returns 10, adding 3 gives 13.
🔧 Debug
advanced2:00remaining
Identify the error in nested function usage
What error does this Kotlin code produce?
Kotlin
fun outer() { inner() fun inner() { println("Inside inner") } } fun main() { outer() }
Attempts:
2 left
💡 Hint
Check the order of function declaration and usage.
✗ Incorrect
The local function 'inner' is called before it is declared, causing an unresolved reference error.
❓ Predict Output
advanced2:00remaining
Effect of local function modifying outer variable
What is the output of this Kotlin code?
Kotlin
fun outer() { var count = 0 fun increment() { count += 1 } increment() increment() println(count) } fun main() { outer() }
Attempts:
2 left
💡 Hint
Local functions can access and modify variables from the outer function.
✗ Incorrect
The local function increments 'count' twice, so the final value is 2.
🧠 Conceptual
expert2:00remaining
Scope and visibility of local functions
Which statement about local functions in Kotlin is TRUE?
Attempts:
2 left
💡 Hint
Think about the visibility and scope rules of local functions.
✗ Incorrect
Local functions are only visible and callable inside the function where they are declared.