Bird
0
0

Identify the error in this Swift code:

medium📝 Debug Q14 of 15
Swift - Variables and Constants
Identify the error in this Swift code:
let pi = 3.14
pi = 3.14159
print(pi)
ANo error, prints 3.14159
BCompile-time error: cannot assign to value: 'pi' is a 'let' constant
CRuntime error: variable not initialized
DSyntax error: missing semicolon
Step-by-Step Solution
Solution:
  1. Step 1: Check reassignment of constant

    The code tries to assign a new value to pi, which was declared with let and is immutable.
  2. Step 2: Understand Swift's error

    Swift does not allow changing a constant's value, so this causes a compile-time error.
  3. Final Answer:

    Compile-time error: cannot assign to value: 'pi' is a 'let' constant -> Option B
  4. Quick Check:

    Reassigning let constant causes compile error = D [OK]
Quick Trick: Constants cannot be changed after assignment [OK]
Common Mistakes:
  • Trying to reassign let constants
  • Expecting runtime error instead of compile error
  • Ignoring error messages

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes