Bird
0
0

What is wrong with this Swift code snippet?

medium📝 Debug Q6 of 15
Swift - Data Types
What is wrong with this Swift code snippet?
let letter: Character = "Hello"
AThe variable name 'letter' is a reserved keyword.
BA Character can only hold a single character, not a string.
CCharacter type cannot be assigned using double quotes.
DThe code is correct and will compile without errors.
Step-by-Step Solution
Solution:
  1. Step 1: Understand Character type

    In Swift, a Character represents a single extended grapheme cluster, which means it can hold exactly one visible character.
  2. Step 2: Analyze the assignment

    The code attempts to assign "Hello" (a string of multiple characters) to a Character variable, which is invalid.
  3. Final Answer:

    A Character can only hold a single character, not a string. -> Option B
  4. Quick Check:

    Assigning multiple characters to Character type causes a compile-time error. [OK]
Quick Trick: Character holds one character only. [OK]
Common Mistakes:
  • Thinking Character can hold multiple characters like String.
  • Using double quotes incorrectly for Character.
  • Assuming variable names like 'letter' are reserved.

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes