Bird
0
0

Why might a Kotlin developer prefer a local function over a lambda expression inside a function?

hard📝 Conceptual Q10 of 15
Kotlin - Functions
Why might a Kotlin developer prefer a local function over a lambda expression inside a function?
ALocal functions cannot access outer variables.
BLocal functions are always faster than lambdas.
CLocal functions must be declared at the top level.
DLocal functions can be recursive and have named parameters.
Step-by-Step Solution
Solution:
  1. Step 1: Compare local functions and lambdas

    Local functions support recursion and have explicit names and parameters, unlike lambdas which are anonymous and harder to recurse.
  2. Step 2: Evaluate options

    Local functions can access outer variables. Performance is not always better. Local functions are declared inside functions.
  3. Final Answer:

    Local functions can be recursive and have named parameters. -> Option D
  4. Quick Check:

    Local function advantage = recursion and naming [OK]
Quick Trick: Local functions allow recursion; lambdas do not easily [OK]
Common Mistakes:
MISTAKES
  • Assuming local functions are always faster
  • Thinking local functions cannot access outer scope
  • Believing local functions must be top-level

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes