Bird
0
0

What is the output of this Swift code?

medium📝 Predict Output Q4 of 15
Swift - Operators and Expressions
What is the output of this Swift code?
let temperature = 30
let weather = temperature > 25 ? "Hot" : "Cold"
print(weather)
AHot
BCold
Ctemperature > 25
DError
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate the condition temperature > 25

    Since temperature is 30, which is greater than 25, the condition is true.
  2. Step 2: Apply ternary operator

    Because the condition is true, the value "Hot" is assigned to weather and printed.
  3. Final Answer:

    Hot -> Option A
  4. Quick Check:

    Condition true = first value chosen [OK]
Quick Trick: True condition picks first value before colon [OK]
Common Mistakes:
  • Choosing false value by mistake
  • Printing condition instead of result
  • Syntax errors in ternary usage

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes