Bird
0
0

What will be the output of this Swift code?

medium📝 Predict Output Q5 of 15
iOS Swift - Swift Language Essentials
What will be the output of this Swift code?
let isSunny: Bool = false
let message: String = isSunny ? "Go outside" : "Stay inside"
print(message)
A"Go outside"
Bfalse
C"Stay inside"
DError: Invalid ternary operation
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate the Boolean variable

    isSunny is false, so the ternary condition chooses the false branch.
  2. Step 2: Determine the message value

    False branch is "Stay inside", so message is set to that string.
  3. Final Answer:

    "Stay inside" -> Option C
  4. Quick Check:

    Ternary with false = "Stay inside" [OK]
Quick Trick: Ternary chooses false branch if condition is false [OK]
Common Mistakes:
  • Confusing true/false branches
  • Expecting Boolean output
  • Syntax errors in ternary

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes