Thursday, September 10, 2026

Number Bases & int() Conversion

 1. What is the output?

python
a = int('101', 2)
print(a)

A) 101 B) 3 C) 5 ✅ D) Error
Explanation: Binary '101' = 1×2² + 0×2¹ + 1×2⁰ = 4 + 0 + 1 = 5


2. What is the output?

python
b = int('20', 8)
print(b)

A) 20 B) 16 ✅ C) 8 D) 2
Explanation: Octal '20' = 2×8¹ + 0×8⁰ = 16 + 0 = 16


3. What is the output?

python
c = int('1A', 16)
print(c)

A) 1 B) 10 C) 26 ✅ D) Error, letters aren't allowed
Explanation: Hex '1A' = 1×16¹ + A(10)×16⁰ = 16 + 10 = 26


4. What happens here?

python
d = int('12', 8)
print(d)

A) 12 B) 10 ✅ C) Error, invalid octal digits D) 9
Explanation: Octal '12' = 1×8¹ + 2×8⁰ = 8 + 2 = 10 (both digits 1 and 2 are valid in base 8, since octal allows 0–7)


5. What happens here?

python
e = int('19', 8)
print(e)

A) 17 B) 9 C) ValueError ✅ D) 19
Explanation: Octal only allows digits 0–7. Since '9' isn't a valid octal digit, Python raises ValueError: invalid literal for int() with base 8.

No comments: