Bird
0
0

Find the problem in this Swift code snippet:

medium📝 Debug Q7 of 15
Swift - Collections
Find the problem in this Swift code snippet:
var colors = ["Red", "Green"]
let colors = ["Blue"]
colors.append("Yellow")
ANo problem, code runs and appends Yellow
BSyntax error in array initialization
CCannot redeclare colors with let after var
DAppend on let array is allowed
Step-by-Step Solution
Solution:
  1. Step 1: Analyze variable declarations

    First, colors is declared as a mutable variable with var.
  2. Step 2: Redeclaring with let causes error

    Redeclaring colors with let in the same scope is not allowed and causes a compile-time error.
  3. Final Answer:

    Cannot redeclare colors with let after var -> Option C
  4. Quick Check:

    Variable redeclaration = Compile-time error [OK]
Quick Trick: Do not redeclare variables in the same scope [OK]
Common Mistakes:
  • Assuming redeclaration is allowed
  • Thinking append works on let array here
  • Ignoring scope rules

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes