Bird
0
0

Consider this Swift code snippet:

hard📝 Application Q9 of 15
iOS Swift - Swift Language Essentials
Consider this Swift code snippet:
var a = 8
let b = a
a = 15
print(b)

What will be printed and why?
A8, because b captures the value of a at assignment
B15, because b reflects the updated value of a
CError: Cannot assign to constant b
Dnil, because b is not initialized properly
Step-by-Step Solution
Solution:
  1. Step 1: Variable and constant declaration

    a is a variable initialized to 8, b is a constant assigned the current value of a.
  2. Step 2: Reassignment of a

    a is changed to 15, but b remains unchanged because it holds a copy of the original value.
  3. Step 3: Print statement

    Printing b outputs 8, the value it was assigned at declaration.
  4. Final Answer:

    8, because b captures the value of a at assignment -> Option A
  5. Quick Check:

    Constants hold the value at assignment, not a reference [OK]
Quick Trick: Constants copy value at assignment, not updated later [OK]
Common Mistakes:
  • Assuming b updates when a changes
  • Confusing reference and value types
  • Expecting runtime error on reassignment

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes