Python Data Types and Variables 4 — Questions and Answers
Question 1: What is the result of `0.1 + 0.2 == 0.3` in Python?
- True
- False (Correct answer)
- TypeError
- None
Correct answer: False
Floating-point representation errors mean 0.1 + 0.2 is not exactly 0.3 in IEEE 754.
Question 2: Which module provides the `Decimal` type for exact decimal arithmetic?
- math
- fractions
- decimal (Correct answer)
- numbers
Correct answer: decimal
The `decimal` module provides the `Decimal` class for arbitrary-precision decimal arithmetic.
Question 3: What is the output of `print(10 // 3)`?
- 3.33
- 3 (Correct answer)
- 4
- 3.0
Correct answer: 3
Floor division `//` divides and rounds down to the nearest integer, giving 3.
Question 4: Which built-in function converts a string of binary digits to an integer?
- int(x, 2) (Correct answer)
- bin(x)
- hex(x)
- ord(x)
Correct answer: int(x, 2)
`int(x, 2)` interprets the string `x` as a base-2 (binary) number.
Question 5: What type does `x` have after `x = []`?
- tuple
- set
- dict
- list (Correct answer)
Correct answer: list
Square brackets `[]` create an empty list object.
Question 6: What is the result of `bool('')`?
- True
- False (Correct answer)
- None
- TypeError
Correct answer: False
An empty string is falsy in Python, so `bool('')` returns `False`.
Question 7: Which of the following creates a bytes literal in Python?
- b'hello' (Correct answer)
- B(hello)
- bytes'hello'
- 'hello'.encode
Correct answer: b'hello'
Prefixing a string literal with `b` (e.g., `b'hello'`) creates a bytes object.
What is the result of `0.1 + 0.2 == 0.3` in Python?