Bird
0
0

What will be the output of this Swift code?

medium📝 Predict Output Q13 of 15
Swift - Variables and Constants
What will be the output of this Swift code?
var count = 5
count = 10
print(count)
A5
BError: Cannot assign to 'count' because it is a let constant
C10
Dnil
Step-by-Step Solution
Solution:
  1. Step 1: Analyze variable declaration and assignment

    count is declared with var, so it can be changed. Initially, it is 5, then reassigned to 10.
  2. Step 2: Understand print output

    The print statement outputs the current value of count, which is 10 after reassignment.
  3. Final Answer:

    10 -> Option C
  4. Quick Check:

    var allows reassignment, so output is 10 [OK]
Quick Trick: var allows change, so print latest value [OK]
Common Mistakes:
  • Thinking count stays 5 after reassignment
  • Confusing var with let causing error
  • Expecting nil output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes