Bird
0
0

Find the error in this Swift code snippet:

medium📝 Debug Q7 of 15
Swift - Data Types
Find the error in this Swift code snippet:
var pair = (first: 1, second: 2)
pair.first = 3
pair.2 = 4
AAccessing pair.2 is invalid; index out of range
BTuple elements cannot be changed after declaration
CTuple must be declared with let, not var
DTuple elements must be accessed by name only
Step-by-Step Solution
Solution:
  1. Step 1: Check tuple mutability and access

    Tuple declared with var can be changed. pair.first = 3 is valid.
  2. Step 2: Check index access

    Tuple has two elements indexed 0 and 1. pair.2 is invalid index, causing error.
  3. Final Answer:

    Accessing pair.2 is invalid; index out of range -> Option A
  4. Quick Check:

    Tuple index out of range = error [OK]
Quick Trick: Tuple indexes start at 0; max index is count-1 [OK]
Common Mistakes:
  • Assuming tuples are immutable
  • Using invalid index
  • Thinking named access forbids index access

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes