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
✗ Incorrect
Character holds a single letter or symbol, while String holds multiple characters.
How do you declare a string variable in Swift?
Avar name: Int = "John"
Blet name: Character = "John"
Cvar name = 123
Dlet name: String = "John"
✗ Incorrect
Use String type and assign text in double quotes.
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"
✗ Incorrect
Character must hold exactly one character, so multiple characters cause an error.
Which of these is a valid way to convert a Character to a String?
Acharacter.toString()
BString(character)
Ccharacter as String
Dcharacter.to_str()
✗ Incorrect
Use the String() initializer to convert a Character to a String.
Can a Swift String contain emojis?
AYes
BNo
COnly if converted to <code>Character</code>
DOnly in comments
✗ Incorrect
String supports emojis and other Unicode characters.
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.
Practice
(1/5)
1. Which Swift type is used to store a single letter or symbol?
easy
A. Bool
B. String
C. Character
D. Int
Solution
Step 1: Understand the types
Character stores exactly one letter or symbol, while String stores many characters.
Step 2: Match the description
Since the question asks for a single letter or symbol, Character is the correct type.
Final Answer:
Character -> Option C
Quick Check:
Single letter = Character [OK]
Hint: Single letter? Use Character type [OK]
Common Mistakes:
Confusing Character with String
Choosing Int or Bool which are not text types
2. Which of the following is the correct way to declare a String variable in Swift?
easy
A. var name: String = "John"
B. var name = 'John'
C. var name = John
D. var name: Character = "John"
Solution
Step 1: Check syntax for String declaration
In Swift, String literals must be in double quotes, and type can be declared explicitly.
Step 2: Analyze options
var name: String = "John" uses var with type String and double quotes correctly. var name = 'John' uses single quotes which is invalid. var name = John misses quotes. var name: Character = "John" uses Character type but assigns multiple characters.
Final Answer:
var name: String = "John" -> Option A
Quick Check:
String uses double quotes and correct type [OK]
Hint: Use double quotes for String, single char needs Character [OK]
Common Mistakes:
Using single quotes for String
Assigning multiple characters to Character
Missing quotes around text
3. What will be the output of this Swift code?
let letter: Character = "A"
let word: String = "Apple"
print(letter)
print(word)
medium
A. Error: Cannot print Character
B. Apple\nA
C. letter\nword
D. A\nApple
Solution
Step 1: Understand variable types and values
letter is a Character with value "A"; word is a String with value "Apple".
Step 2: Analyze print statements
print(letter) outputs "A" and print(word) outputs "Apple" on separate lines.
Final Answer:
A\nApple -> Option D
Quick Check:
Print Character then String = A\nApple [OK]
Hint: Print shows Character or String as text directly [OK]
Common Mistakes:
Confusing order of print statements
Thinking Character cannot be printed
Expecting variable names printed
4. Identify the error in this Swift code:
let symbol: Character = "AB"
medium
A. Missing semicolon at end
B. Character cannot hold more than one character
C. String type should be used instead of Character
D. Quotes should be single quotes
Solution
Step 1: Check Character assignment rules
Character type can only hold exactly one character or symbol.
Step 2: Analyze the code
"AB" has two characters, so assigning it to Character causes an error.
Final Answer:
Character cannot hold more than one character -> Option B
Quick Check:
Character = single char only [OK]
Hint: Character holds one char; multiple chars need String [OK]
Common Mistakes:
Using multiple characters for Character type
Thinking semicolon is required
Using single quotes for strings
5. You want to create a Swift function that counts how many vowels are in a given String. Which approach correctly uses Character and String types?
hard
A. Iterate over the String's characters and check if each Character is a vowel
B. Convert the String to Int and count vowels
C. Use Character type to store the whole String and count vowels
D. Use Bool type to store vowels and count them
Solution
Step 1: Understand the problem
We need to check each letter in a String to see if it is a vowel.
Step 2: Choose correct types and method
Iterating over a String gives Characters one by one, which can be checked against vowels.
Step 3: Analyze options
Iterate over the String's characters and check if each Character is a vowel correctly describes iterating over Characters in a String. Options A, B, and D misuse types or concepts.
Final Answer:
Iterate over the String's characters and check if each Character is a vowel -> Option A
Quick Check:
Check each Character in String for vowels [OK]
Hint: Loop over String characters to check each letter [OK]
Common Mistakes:
Trying to convert String to Int for counting
Using Character to hold whole String
Using Bool incorrectly to count vowels
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.