The Char type in Kotlin represents a single character, such as a letter, digit, or symbol. It holds a 16-bit Unicode character.
Click to reveal answer
intermediate
How does Kotlin store characters internally in the Char type?
Kotlin stores Char as a 16-bit value representing a UTF-16 code unit, which can represent most common Unicode characters directly.
Click to reveal answer
intermediate
What happens when a Unicode character requires more than 16 bits in Kotlin's Char type?
Characters outside the Basic Multilingual Plane (BMP) need two Char values called a surrogate pair to represent one Unicode character in Kotlin.
Click to reveal answer
beginner
How can you represent a Unicode character in Kotlin using escape sequences?
You can use the syntax \uXXXX where XXXX is the 4-digit hexadecimal Unicode code unit for the character, e.g., '\u0041' for 'A'.
Click to reveal answer
intermediate
Why might a Kotlin String have a different length than the number of Unicode characters it displays?
Because Kotlin String length counts Char units (UTF-16 code units), some characters (like emojis) use two Char units, so length can be greater than visible characters.
Click to reveal answer
What Kotlin type is used to store a single Unicode character?
AChar
BString
CInt
DByte
✗ Incorrect
The Char type stores a single Unicode character in Kotlin.
How many bits does a Kotlin Char use internally?
A32 bits
B8 bits
C16 bits
D64 bits
✗ Incorrect
Kotlin Char uses 16 bits internally, representing a UTF-16 code unit.
What is a surrogate pair in Kotlin's Unicode handling?
AA pair of integers
BTwo <code>Char</code> values representing one Unicode character outside BMP
CA pair of strings
DA Kotlin keyword
✗ Incorrect
Surrogate pairs are two Char values used to represent one Unicode character outside the Basic Multilingual Plane.
How do you write the Unicode character 'A' using an escape sequence in Kotlin?
A'\u0041'
B'\x41'
C'\u41'
D'\U0041'
✗ Incorrect
The correct Unicode escape sequence in Kotlin is \u0041 for 'A'.
Why might string.length be larger than the number of visible characters?
ABecause of string encoding errors
BBecause Kotlin counts words, not characters
CBecause of trailing spaces
DBecause some characters use two <code>Char</code> units (surrogate pairs)
✗ Incorrect
Some Unicode characters like emojis use two Char units, so length counts code units, not visible characters.
Explain how Kotlin's Char type represents Unicode characters and what happens with characters outside the Basic Multilingual Plane.
Think about how UTF-16 encoding works with Kotlin's Char.
You got /4 concepts.
Describe how to use Unicode escape sequences in Kotlin to represent characters and why string length might not match visible characters.
Consider how Kotlin counts characters internally versus what you see.
You got /4 concepts.
Practice
(1/5)
1. Which of the following correctly declares a Char variable in Kotlin?
easy
A. val letter: Char = `A`
B. val letter: Char = "A"
C. val letter: Char = A
D. val letter: Char = 'A'
Solution
Step 1: Understand Char declaration syntax
In Kotlin, a Char value must be enclosed in single quotes, like 'A'.
Step 2: Check each option
val letter: Char = 'A' uses single quotes correctly. val letter: Char = "A" uses double quotes (String). val letter: Char = A has no quotes (invalid). val letter: Char = `A` uses backticks (invalid for Char).
Final Answer:
val letter: Char = 'A' -> Option D
Quick Check:
Char uses single quotes = A [OK]
Hint: Char values use single quotes, not double quotes [OK]
Common Mistakes:
Using double quotes instead of single quotes
Omitting quotes around the character
Using backticks instead of quotes
2. Which of the following is the correct way to get the Unicode code of a Char in Kotlin?
easy
A. val code = 'A'.code
B. val code = 'A'.unicodeValue()
C. val code = 'A'.toInt()
D. val code = 'A'.unicode
Solution
Step 1: Recall Kotlin Char Unicode property
Kotlin provides the code property to get the Unicode integer value of a Char.
Step 2: Evaluate options
val code = 'A'.code uses code property correctly. val code = 'A'.toInt() uses toInt() which does not exist for Char. Options B and D use non-existent methods/properties.
Final Answer:
val code = 'A'.code -> Option A
Quick Check:
Use .code to get Unicode number [OK]
Hint: Use .code property to get Unicode number from Char [OK]
Common Mistakes:
Using non-existent toInt() method on Char
Trying non-existent methods like unicodeValue()
Accessing properties that don't exist
3. What is the output of the following Kotlin code?
val ch: Char = 'Z'
println(ch.code)
medium
A. 122
B. 90
C. Z
D. SyntaxError
Solution
Step 1: Identify the Char and property used
The variable ch is assigned the character 'Z'. The code prints ch.code, which returns the Unicode integer value.
Step 2: Find Unicode value of 'Z'
The Unicode code for uppercase 'Z' is 90.
Final Answer:
90 -> Option B
Quick Check:
'Z'.code = 90 [OK]
Hint: Remember uppercase 'Z' Unicode is 90 [OK]
Common Mistakes:
Confusing uppercase and lowercase Unicode values
Expecting the character itself instead of code
Thinking code property causes syntax error
4. Identify the error in this Kotlin code snippet:
val ch: Char = "A"
println(ch.code)
medium
A. code property does not exist for Char
B. Missing semicolon after println
C. Using double quotes for Char assignment
D. Variable ch should be Int, not Char
Solution
Step 1: Check Char assignment syntax
Chars must be assigned using single quotes, not double quotes. Double quotes create a String.
Step 2: Verify other parts
Semicolons are optional in Kotlin, so no error there. The code property exists for Char. Variable type Char is correct.
Final Answer:
Using double quotes for Char assignment -> Option C
Quick Check:
Char needs single quotes, not double [OK]
Hint: Chars use single quotes; double quotes mean String [OK]
Common Mistakes:
Thinking semicolons are mandatory
Believing code property is invalid
Confusing Char and Int types
5. Given a list of characters val chars = listOf('a', 'B', 'c', 'D'), which Kotlin expression correctly filters only uppercase letters using Unicode properties?
hard
A. chars.filter { it.isUpperCase() }
B. chars.filter { it.code > 90 }
C. chars.filter { it.code.isUpperCase() }
D. chars.filter { it > 'Z' }
Solution
Step 1: Understand filtering uppercase letters
Kotlin's Char class has a method isUpperCase() that returns true if the character is uppercase.
Step 2: Evaluate each option
chars.filter { it.isUpperCase() } uses isUpperCase() correctly. chars.filter { it.code > 90 } filters by code > 90, which excludes all uppercase letters (A-Z: 65-90). chars.filter { it.code.isUpperCase() } calls isUpperCase() on an Int (code), which does not exist. chars.filter { it > 'Z' } compares chars incorrectly.
Final Answer:
chars.filter { it.isUpperCase() } -> Option A
Quick Check:
Use isUpperCase() to filter uppercase chars [OK]
Hint: Use isUpperCase() method to filter uppercase chars [OK]
Common Mistakes:
Filtering by numeric code incorrectly
Calling isUpperCase() on the Unicode code (Int)
Using wrong comparison operators for chars
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.