Bird
0
0

What will this Swift code print?

medium📝 Predict Output Q5 of 15
Swift - Operators and Expressions
What will this Swift code print?
let number = 0
let result = number != 0 ? "Non-zero" : "Zero"
print(result)
ANon-zero
Bnumber != 0
Cnil
DZero
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate the condition number != 0

    Since number is 0, the condition number != 0 is false.
  2. Step 2: Apply ternary operator

    Because the condition is false, the value "Zero" is assigned to result and printed.
  3. Final Answer:

    Zero -> Option D
  4. Quick Check:

    False condition picks second value after colon [OK]
Quick Trick: False condition picks value after colon [OK]
Common Mistakes:
  • Choosing true value when condition is false
  • Printing condition expression
  • Confusing != with ==

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes