Bird
0
0

Which of the following Kotlin number literals is syntactically correct?

easy📝 Syntax Q3 of 15
Kotlin - Data Types
Which of the following Kotlin number literals is syntactically correct?
Aval num = 1__000
Bval num = 0b1010_1010
Cval num = 0x_1A
Dval num = 0b2_010
Step-by-Step Solution
Solution:
  1. Step 1: Understand binary literal rules in Kotlin

    Binary literals start with 0b and contain only 0 or 1 digits, underscores allowed between digits.
  2. Step 2: Check each option for syntax correctness

    val num = 0b1010_1010 is valid binary with underscores correctly placed. val num = 0b2_010 contains '2' which is invalid in binary. val num = 0x_1A has underscore immediately after 0x which is invalid. val num = 1__000 has consecutive underscores which is invalid.
  3. Final Answer:

    val num = 0b1010_1010 -> Option B
  4. Quick Check:

    Valid binary literal syntax = D [OK]
Quick Trick: Binary digits only 0 or 1; underscores between digits only [OK]
Common Mistakes:
MISTAKES
  • Using digits other than 0 or 1 in binary literals
  • Placing underscore right after prefix 0b or 0x
  • Using multiple underscores consecutively

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes