Bird
Raised Fist0
Swiftprogramming~5 mins

Numeric literal formats in Swift - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is a numeric literal in Swift?
A numeric literal is a fixed number value written directly in code, like 42 or 3.14.
Click to reveal answer
beginner
How do you write a binary literal in Swift?
Use the prefix 0b followed by 0s and 1s, for example: 0b1010 equals 10 in decimal.
Click to reveal answer
beginner
What prefix is used for hexadecimal literals in Swift?
Hexadecimal literals start with 0x, like 0x1F which equals 31 in decimal.
Click to reveal answer
beginner
How can you write a number with underscores in Swift and why?
You can add underscores to separate digits for readability, like 1_000_000. It helps you read large numbers easily but does not affect the value.
Click to reveal answer
intermediate
How do you write a floating-point literal with an exponent in Swift?
Use e or E to indicate the power of 10, for example: 1.25e2 means 1.25 × 10² = 125.
Click to reveal answer
Which prefix is used for octal literals in Swift?
A0o
B0x
C0b
DNo prefix, just digits
What does the numeric literal 0b1101 represent in decimal?
A15
B13
C14
D11
How would you write one million in Swift for better readability?
A1000000
B10_00_00
C1_000_000
D1,000,000
What is the value of 3.5e1 in Swift?
A35
B3.5
C350
D0.35
Which of these is NOT a valid numeric literal format in Swift?
A0xFF
B123_456
C0b1010
D0d123
Explain the different numeric literal formats available in Swift and give an example of each.
Think about prefixes like 0b, 0o, 0x and how floating-point numbers use e for exponents.
You got /6 concepts.
    How do underscores improve numeric literals in Swift? Provide an example.
    Consider readability and whether underscores change the number.
    You got /3 concepts.

      Practice

      (1/5)
      1. Which of the following is a valid binary numeric literal in Swift?
      easy
      A. 0b1010
      B. 0x1010
      C. 1010b
      D. 0o1010

      Solution

      1. Step 1: Understand binary literal format in Swift

        Binary literals start with 0b followed by 0s and 1s.
      2. Step 2: Check each option

        0b1010 uses 0b prefix correctly. 0x1010 is hexadecimal, C is invalid, A is octal.
      3. Final Answer:

        0b1010 -> Option A
      4. Quick Check:

        Binary literals start with 0b = D [OK]
      Hint: Binary literals always start with 0b prefix [OK]
      Common Mistakes:
      • Confusing binary (0b) with hexadecimal (0x)
      • Placing the b suffix after the number
      • Using octal prefix 0o for binary
      2. Which of the following is the correct way to write the number one million using underscores in Swift?
      easy
      A. 1_00_0000
      B. 1000_000
      C. 10_00_000
      D. 1_000_000

      Solution

      1. Step 1: Understand underscore usage in numeric literals

        Underscores can be placed anywhere between digits to improve readability.
      2. Step 2: Check each option for correct grouping

        1_000_000 groups digits in thousands correctly. Others have irregular grouping which is allowed but uncommon and confusing.
      3. Final Answer:

        1_000_000 -> Option D
      4. Quick Check:

        Use underscores to separate thousands = A [OK]
      Hint: Use underscores every three digits for clarity [OK]
      Common Mistakes:
      • Placing underscores at the start or end of number
      • Using inconsistent grouping that confuses readers
      • Thinking underscores change the value
      3. What is the output of the following Swift code?
      let number = 1.5e2
      print(number)
      medium
      A. 150.0
      B. 1.5e2
      C. 15.0
      D. SyntaxError

      Solution

      1. Step 1: Understand scientific notation in Swift

        1.5e2 means 1.5 x 10², which equals 150.0.
      2. Step 2: Evaluate the print output

        Printing the number shows the decimal value 150.0, not the literal string.
      3. Final Answer:

        150.0 -> Option A
      4. Quick Check:

        1.5e2 = 150.0 [OK]
      Hint: e2 means multiply by 10 squared [OK]
      Common Mistakes:
      • Thinking print shows the literal string '1.5e2'
      • Confusing e2 with e-2 (which divides)
      • Assuming integer output instead of floating-point
      4. Identify the error in this Swift numeric literal:
      let value = 0b1021
      medium
      A. Should use 0x prefix for binary
      B. Binary literals cannot contain digits other than 0 or 1
      C. Missing underscore for readability
      D. Floating-point literals cannot use binary format

      Solution

      1. Step 1: Check digits allowed in binary literals

        Binary literals only allow digits 0 and 1 after the 0b prefix.
      2. Step 2: Analyze the given literal

        The literal contains digit 2, which is invalid for binary (only 0s and 1s allowed).
      3. Final Answer:

        Binary literals cannot contain digits other than 0 or 1 -> Option B
      4. Quick Check:

        Binary digits only 0 or 1 = C [OK]
      Hint: Binary digits must be only 0 or 1 [OK]
      Common Mistakes:
      • Thinking underscores are mandatory
      • Confusing binary prefix 0b with hexadecimal 0x
      • Assuming floating-point literals can be binary
      5. You want to represent the decimal number 255 in Swift using hexadecimal and octal literals. Which pair of literals correctly represents 255?
      hard
      A. 0xFF and 0o255
      B. 0x255 and 0o255
      C. 0xFF and 0o377
      D. 0x377 and 0oFF

      Solution

      1. Step 1: Convert decimal 255 to hexadecimal

        255 decimal equals FF in hexadecimal, so 0xFF is correct.
      2. Step 2: Convert decimal 255 to octal

        255 decimal equals 377 in octal, so 0o377 is correct.
      3. Final Answer:

        0xFF and 0o377 -> Option C
      4. Quick Check:

        255 decimal = 0xFF hex & 0o377 oct [OK]
      Hint: Hex FF = 255 decimal; Oct 377 = 255 decimal [OK]
      Common Mistakes:
      • Confusing octal digits with decimal digits
      • Using decimal digits after 0x prefix
      • Mixing up hexadecimal and octal prefixes
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard