0
0
Swiftprogramming~5 mins

Character and String types in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the difference between a Character and a String in Swift?

A Character represents a single letter, number, or symbol, while a String is a collection of Characters that form words or sentences.

Click to reveal answer
beginner
How do you declare a variable of type Character in Swift?
<p>You declare it by specifying the type <code>Character</code> and assigning a single character in double quotes, for example: <code>let letter: Character = "A"</code>.</p>
Click to reveal answer
beginner
Can a Swift String contain multiple characters including spaces and emojis?

Yes, a String can hold multiple characters, spaces, emojis, and special symbols, like "Hello 👋".

Click to reveal answer
intermediate
How do you convert a Character to a String in Swift?
<p>You can convert a <code>Character</code> to a <code>String</code> by using the <code>String()</code> initializer, for example: <code>let str = String(character)</code>.</p>
Click to reveal answer
intermediate
What happens if you try to assign multiple characters to a Character variable in Swift?

Swift will give a compile-time error because Character can only hold exactly one character.

Click to reveal answer
Which Swift type is used to store a single letter or symbol?
ACharacter
BString
CInt
DBool
How do you declare a string variable in Swift?
Avar name: Int = "John"
Blet name: Character = "John"
Cvar name = 123
Dlet name: String = "John"
What will happen if you assign "AB" to a Character variable?
AStores "AB" as one character
BStores only "A"
CCompile-time error
DStores only "B"
Which of these is a valid way to convert a Character to a String?
Acharacter.toString()
BString(character)
Ccharacter as String
Dcharacter.to_str()
Can a Swift String contain emojis?
AYes
BNo
COnly if converted to <code>Character</code>
DOnly in comments
Explain the difference between Character and String types in Swift.
Think about letters vs words.
You got /4 concepts.
    Describe how to convert a Character to a String and why you might need to do this.
    Consider when you want to join characters or print them as text.
    You got /3 concepts.