0
0
Kotlinprogramming~20 mins

Number literal formats (underscore, hex, binary) in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Number Literal Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
Output of Kotlin number literals with underscores
What is the output of this Kotlin code snippet?
Kotlin
fun main() {
    val number = 1_000_000
    println(number)
}
A1000000
B1000_000
C1_000_000
D100000
Attempts:
2 left
💡 Hint
Underscores in number literals are ignored by the compiler and only improve readability.
Predict Output
intermediate
1:30remaining
Output of Kotlin hexadecimal literal
What will this Kotlin code print?
Kotlin
fun main() {
    val hexNumber = 0x1A
    println(hexNumber)
}
A0x1A
B1A
C26
D31
Attempts:
2 left
💡 Hint
Hexadecimal literals start with 0x and represent base 16 numbers.
Predict Output
advanced
2:00remaining
Output of Kotlin binary literal with underscores
What is the output of this Kotlin program?
Kotlin
fun main() {
    val binaryNumber = 0b1010_1101
    println(binaryNumber)
}
A173
B10101101
C0b10101101
D165
Attempts:
2 left
💡 Hint
Binary literals start with 0b and represent base 2 numbers. Underscores are ignored.
Predict Output
advanced
2:00remaining
Sum of mixed number literals in Kotlin
What is the output of this Kotlin code?
Kotlin
fun main() {
    val a = 0xF
    val b = 0b10_10
    val c = 10_4
    println(a + b + c)
}
A135
B129
C140
D150
Attempts:
2 left
💡 Hint
Convert each literal to decimal before adding: 0xF = 15, 0b10_10 = 10, 10_4 = 104.
Predict Output
expert
2:00remaining
Output of Kotlin code with invalid underscore placement
What error does this Kotlin code produce?
Kotlin
fun main() {
    val invalidNumber = 0x_1F
    println(invalidNumber)
}
ANo error, prints 31
BNumberFormatException
CIllegalArgumentException
DSyntaxError
Attempts:
2 left
💡 Hint
Underscores cannot be placed right after the 0x prefix in hexadecimal literals.