Bird
0
0

Identify the error in this Kotlin code snippet:

medium📝 Debug Q14 of 15
Kotlin - Functions
Identify the error in this Kotlin code snippet:
fun calculate() {
    fun add(a: Int, b: Int): Int {
        return a + b
    }
}
fun main() {
    println(add(3, 4))
}
AMissing return type for 'add'
BSyntax error in function declaration
CCannot call local function 'add' from outside 'calculate'
DNo error, prints 7
Step-by-Step Solution
Solution:
  1. Step 1: Understand local function visibility

    The function 'add' is local inside 'calculate' and cannot be accessed outside it.
  2. Step 2: Check where 'add' is called

    'add(3, 4)' is called in 'main', outside 'calculate', causing an error.
  3. Final Answer:

    Cannot call local function 'add' from outside 'calculate' -> Option C
  4. Quick Check:

    Local functions are private to outer function [OK]
Quick Trick: Local functions can't be called outside their outer function [OK]
Common Mistakes:
MISTAKES
  • Trying to call local function from outside
  • Confusing local and top-level functions
  • Ignoring function scope rules

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes