Bird
0
0

What will be the output of this Swift code?

medium📝 Predict Output Q5 of 15
Swift - Variables and Constants
What will be the output of this Swift code?
var score = 5
score = 10
let score = 15
print(score)
A5
BCompilation error
C15
D10
Step-by-Step Solution
Solution:
  1. Step 1: Review variable shadowing rules

    Declaring let score after var score in the same scope is not allowed.
  2. Step 2: Check for compile errors

    Swift does not allow redeclaration of variables with the same name in the same scope, causing a compile error.
  3. Final Answer:

    Compilation error -> Option B
  4. Quick Check:

    Redeclaring variable in same scope = compile error [OK]
Quick Trick: Redeclaring variables in same scope causes compile errors [OK]
Common Mistakes:
  • Expecting last declaration to override silently
  • Confusing variable shadowing with redeclaration
  • Assuming let can redeclare var in same scope

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes