Bird
0
0

What is the type of the value stored in the variable char in the following Swift code?

easy📝 Conceptual Q2 of 15
Swift - Data Types
What is the type of the value stored in the variable char in the following Swift code?
let char = "Z"
ABool
BCharacter
CString
DInt
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the declaration without explicit type

    The variable char is assigned a string literal with double quotes, which defaults to type String in Swift.
  2. Step 2: Understand difference between Character and String

    Single characters can be stored as Character type using single quotes or explicit type annotation, but here no annotation is given, so it is a String.
  3. Final Answer:

    The variable char is of type String. -> Option C
  4. Quick Check:

    Unannotated single quotes = String [OK]
Quick Trick: Double quotes create String, not Character, unless typed [OK]
Common Mistakes:
  • Assuming single character literals are Character by default
  • Confusing single quotes with double quotes in Swift
  • Not knowing Swift requires explicit Character type

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes