Bird
0
0

What will be the output of this Swift code?

medium📝 Predict Output Q13 of 15
iOS Swift - Swift Language Essentials
What will be the output of this Swift code?
var name = "Alice"
name = "Bob"
print(name)
AError: Cannot assign to let constant
BAlice
CBob
Dnil
Step-by-Step Solution
Solution:
  1. Step 1: Understand variable reassignment

    The variable name is declared with var, so it can be changed after initial assignment.
  2. Step 2: Trace the code execution

    Initially, name is "Alice". Then it is reassigned to "Bob". The print statement outputs the current value, "Bob".
  3. Final Answer:

    Bob -> Option C
  4. Quick Check:

    var allows change, so output is reassigned value [OK]
Quick Trick: var means value can change, so print latest value [OK]
Common Mistakes:
  • Thinking let was used and reassignment causes error
  • Expecting original value to print
  • Confusing variable with constant

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes