Bird
0
0

What will be the output of the following Swift code?

medium📝 Predict Output Q13 of 15
Swift - Functions
What will be the output of the following Swift code?
func greet() {
    func sayHello() {
        print("Hello")
    }
    sayHello()
}
greet()
AHello
BNo output
CError: sayHello not found
DHelloHello
Step-by-Step Solution
Solution:
  1. Step 1: Understand function calls inside nested functions

    The outer function 'greet' defines 'sayHello' inside it and calls it once.
  2. Step 2: Trace the execution

    Calling 'greet()' runs 'sayHello()', which prints "Hello" once.
  3. Final Answer:

    Hello -> Option A
  4. Quick Check:

    Nested call prints once = Hello [OK]
Quick Trick: Inner function called once inside outer function [OK]
Common Mistakes:
  • Thinking inner function runs twice
  • Assuming inner function is global
  • Expecting no output because inner function is nested

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes