Bird
0
0

Which of the following is the correct syntax to declare a local function inside a Kotlin function?

easy📝 Syntax Q3 of 15
Kotlin - Functions
Which of the following is the correct syntax to declare a local function inside a Kotlin function?
Afun outer() { fun inner: () -> Unit = { println("Hi") } inner() }
Bfun outer() { fun inner() { println("Hi") } inner() }
Cfun outer() { val inner = fun() { println("Hi") } inner() }
Dfun outer() { def inner() { println("Hi") } inner() }
Step-by-Step Solution
Solution:
  1. Step 1: Review Kotlin local function syntax

    Local functions are declared with fun keyword inside another function, followed by parentheses and body.
  2. Step 2: Check each option

    fun outer() { fun inner() { println("Hi") } inner() } uses correct syntax: fun inner() { ... } inside outer(). fun outer() { fun inner: () -> Unit = { println("Hi") } inner() } uses lambda syntax incorrectly. fun outer() { val inner = fun() { println("Hi") } inner() } uses anonymous function assigned to val, which is valid but not a local function declaration. fun outer() { def inner() { println("Hi") } inner() } uses def which is invalid in Kotlin.
  3. Final Answer:

    fun outer() { fun inner() { println("Hi") } inner() } -> Option B
  4. Quick Check:

    Local function syntax = fun inside fun [OK]
Quick Trick: Local functions use 'fun' keyword inside another function [OK]
Common Mistakes:
MISTAKES
  • Using def instead of fun
  • Confusing lambdas with local functions
  • Assigning anonymous functions to variables instead of declaring local functions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes