Bird
0
0

What will be printed by this Swift code?

medium📝 Predict Output Q4 of 15
Swift - Variables and Constants
What will be printed by this Swift code?
var value = 3
value = 9
print(value)
A9
B3
C0
DError
Step-by-Step Solution
Solution:
  1. Step 1: Initial assignment

    value is initially set to 3.
  2. Step 2: Reassignment

    value is then changed to 9, which is valid since var allows mutation.
  3. Step 3: Print statement

    The print outputs the current value of value, which is 9.
  4. Final Answer:

    9 -> Option A
  5. Quick Check:

    Mutable variables reflect last assigned value [OK]
Quick Trick: var allows reassignment, so print last value [OK]
Common Mistakes:
  • Assuming print shows initial value
  • Confusing var with let
  • Expecting error on reassignment

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes