Bird
0
0

Consider this Swift code:

hard📝 Application Q15 of 15
iOS Swift - Swift Language Essentials
Consider this Swift code:
var x = 5
let y = x
x = 10
print(y)

What will be printed and why?
A10, because y tracks x's value
Bnil, because y is not initialized
CError, cannot assign var to let
D5, because y is a constant copy of x's initial value
Step-by-Step Solution
Solution:
  1. Step 1: Understand value assignment

    Variable x is 5 initially. y is a constant assigned the current value of x, which is 5.
  2. Step 2: Reassign x and check y

    Changing x to 10 does not affect y, because y holds its own copy of the value 5.
  3. Final Answer:

    5, because y is a constant copy of x's initial value -> Option D
  4. Quick Check:

    let copies value; later changes to var don't affect it [OK]
Quick Trick: let copies value at assignment; later var changes don't affect it [OK]
Common Mistakes:
  • Thinking y updates when x changes
  • Expecting runtime error on assignment
  • Confusing reference and value types

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes