Python Data Types and Variables 2 — Questions and Answers
Question 1: What is the result of `type(3.0)` in Python?
- <class 'int'>
- <class 'float'> (Correct answer)
- <class 'double'>
- <class 'number'>
Correct answer: <class 'float'>
3.0 is a floating-point literal, so its type is `float`.
Question 2: Which statement correctly creates a complex number in Python?
- z = complex(2, 3) (Correct answer)
- z = 2 + 3i
- z = (2, 3j)
- z = 2.3j
Correct answer: z = complex(2, 3)
`complex(2, 3)` creates the complex number 2+3j; Python uses `j` not `i` in literals.
Question 3: What does `bool` inherit from in Python's type hierarchy?
- object
- int (Correct answer)
- float
- str
Correct answer: int
`bool` is a subclass of `int`, so `True == 1` and `False == 0`.
Question 4: What is the output of `print(type(None))`?
- <class 'null'>
- <class 'undefined'>
- <class 'NoneType'> (Correct answer)
- <class 'void'>
Correct answer: <class 'NoneType'>
Python's `None` is the sole instance of the built-in `NoneType` class.
Question 5: Which of the following variable names is INVALID in Python?
- _count
- count2
- 2count (Correct answer)
- Count
Correct answer: 2count
Variable names cannot begin with a digit; `2count` is a syntax error.
Question 6: What is the value of `int('0b1010', 0)` in Python?
- 0
- 10 (Correct answer)
- 1010
- 8
Correct answer: 10
Passing base `0` tells `int()` to auto-detect the base from the prefix; `0b1010` is binary 10.
Question 7: After `x = 5`, which operation changes `x` to a float WITHOUT reassigning explicitly?
- x = x // 1
- x = x * 1
- x /= 1 (Correct answer)
- x += 0
Correct answer: x /= 1
True division `/=` always returns a float, so `x /= 1` makes x equal 5.0.
What is the result of `type(3.0)` in Python?