Bird
0
0

Which of the following is the correct way to declare and copy a string in Swift?

easy📝 Syntax Q12 of 15
Swift - Data Types
Which of the following is the correct way to declare and copy a string in Swift?
Avar original = "Hello"; var copy = original.clone()
Bvar original = "Hello"; var copy = &original
Cvar original = "Hello"; var copy = original.copy()
Dvar original = "Hello"; var copy = original
Step-by-Step Solution
Solution:
  1. Step 1: Review Swift string assignment syntax

    Assigning one string to another copies the value directly using var copy = original.
  2. Step 2: Check other options for correctness

    Options A, B, and D use invalid syntax or methods not available for Swift strings.
  3. Final Answer:

    var original = "Hello"; var copy = original -> Option D
  4. Quick Check:

    Simple assignment copies string value [OK]
Quick Trick: Copy strings by simple assignment, no special methods needed [OK]
Common Mistakes:
  • Using & operator which is for pointers, not strings
  • Trying to call clone() or copy() which don't exist for String
  • Confusing Swift with other languages' string methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes