0
0
Kotlinprogramming~5 mins

Number literal formats (underscore, hex, binary) in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of using underscores in Kotlin number literals?
Underscores in Kotlin number literals make large numbers easier to read by visually separating digits, like commas in regular numbers. They do not affect the actual value.
Click to reveal answer
beginner
How do you write a hexadecimal number literal in Kotlin?
In Kotlin, a hexadecimal number literal starts with 0x or 0X followed by digits 0-9 and letters A-F (case insensitive). For example, 0x1A3F.
Click to reveal answer
beginner
How do you write a binary number literal in Kotlin?
A binary number literal in Kotlin starts with 0b or 0B followed by only 0s and 1s. For example, 0b1010_1101.
Click to reveal answer
intermediate
Can underscores be placed anywhere in Kotlin number literals?
Underscores can be placed between digits in Kotlin number literals to improve readability, but not at the start or end of the number, nor next to the decimal point or prefixes like 0x or 0b.
Click to reveal answer
intermediate
Example: What is the decimal value of 0xFF_EC_DE_5E in Kotlin?
The hexadecimal literal 0xFF_EC_DE_5E equals 4293713502 in decimal. The underscores are ignored when computing the value.
Click to reveal answer
Which of the following is a valid Kotlin binary literal?
A0b1021
B0x1010_1101
C1010_1101b
D0b1010_1101
What does the underscore do in Kotlin number literals?
AMarks the number as hexadecimal
BMultiplies the number by 1000
CSeparates digits for readability without changing the value
DIndicates a negative number
Which prefix is used for hexadecimal literals in Kotlin?
A0x
B#
C0b
D0h
Is this Kotlin number literal valid? 0x_1A3F
ANo, underscore cannot be right after the prefix
BYes, underscores can be anywhere
CYes, but only in binary literals
DNo, underscores are not allowed in hex literals
What is the decimal value of 0b1111_0000?
A15
B240
C11110000
D128
Explain how to write and use underscores in Kotlin number literals.
Think about how commas help read big numbers.
You got /4 concepts.
    Describe how to write hexadecimal and binary number literals in Kotlin with examples.
    Remember the prefixes and allowed digits.
    You got /4 concepts.