Bird
0
0

What is wrong with this Kotlin code?

medium📝 Debug Q7 of 15
Kotlin - Functions
What is wrong with this Kotlin code?
fun outer() {
    fun inner() {
        println(message)
    }
}
val message = "Hi"
outer()
Ainner() must be called inside outer() to print message.
Bmessage must be declared inside outer() to be accessible.
Cinner() cannot access message because it is declared outside outer().
Dmessage must be a var, not val.
Step-by-Step Solution
Solution:
  1. Step 1: Check if inner() is called

    The inner() function is declared but never called inside outer(), so println is never executed.
  2. Step 2: Analyze variable access

    message is declared outside outer(), but inner() can access it because it is in scope. The problem is the missing call to inner().
  3. Final Answer:

    inner() must be called inside outer() to print message. -> Option A
  4. Quick Check:

    Local function must be called to run [OK]
Quick Trick: Declare and call local functions to execute their code [OK]
Common Mistakes:
MISTAKES
  • Assuming variable scope is the problem
  • Forgetting to call the local function
  • Thinking val vs var affects access here

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes