Playing as a guest? to earn XP and track your progress.
๐ข Data Handling Quiz
๐ข Data Handling
What does a data type tell Python?
The memory address of a value
What kind of value a variable holds and what operations can be performed on it
How many times a loop runs
The name of a variable
A data type tells Python what kind of value is held and which operations are allowed; type() shows it.
Which are Python's four numeric types?
int, float, list, tuple
int, long, double, float
int, float, str, bool
int, bool, float, complex
Python's numbers are int, bool, float and complex.
Which statement about Python integers is correct?
They have unlimited precision โ as large as memory allows
They must be positive
They are limited to 32 bits
They are stored approximately
Python integers have unlimited precision; for example 2 ** 100 is printed in full.
What is the output of print(True + True)?
Error
TrueTrue
2
True
bool is a sub-type of int where True = 1, so True + True = 2.
What does bool(0), bool(15), bool("") return?
(False, True, True)
(False, True, False)
(0, 15, '')
(True, True, False)
0 and the empty string are falsy; 15 is truthy.
What is the value of the float literal 1.75E1?
1.75
175.0
0.175
17.5
Exponent form means mantissa ร 10^exponent: 1.75 ร 10ยน = 17.5.
Why does 0.1 + 0.2 give 0.30000000000000004 in Python?
Floats are stored with limited precision, so tiny rounding errors appear
0.1 is stored as an integer
The + operator rounds up
It is a bug in Python
Floats are approximate (about 15 significant digits); integers are always exact.
For z = 3 + 4j, what do z.real and z.imag return?
3 and 4
3 and 4j
3.0 and 4.0
'3' and '4'
Both parts of a complex number are returned as floats: 3.0 and 4.0.
Which letter does Python use for the imaginary unit of a complex number?
x
j
k
i
Python writes complex numbers as a + bj, using j (not i).
For a string of length n, what is the range of valid backward indices?
-0 to -n+1
1 to n
0 to n
-1 to -n
Forward indices go from 0 to n-1 and backward indices from -1 to -n.
In the string "SCHOOL", what are the forward and backward indices of the letter 'H'?
2 and -4
3 and -3
1 and -5
2 and -3
S=0, C=1, H=2 forwards; backwards L=-1, O=-2, O=-3, H=-4.
Accessing an index outside a string's valid range raises:
ValueError
TypeError
IndexError
KeyError
An out-of-range index gives an IndexError, for example "abc"[5].
Which statement about lists and tuples is correct?
Tuples use [ ] and are mutable
Lists use [ ] and are mutable; tuples use ( ) and are immutable
Both are immutable
Lists use ( ) and are immutable
Lists (square brackets) can be changed; tuples (round brackets) cannot.
How are the values in a dictionary accessed?
By their position from the end
By slicing only
By index numbers
By their keys
A dictionary stores key:value pairs in { }, and values are looked up by key, e.g. student["name"].
What is the type of the value None?
NoneType
null
int
bool
None represents 'no value' and its type is NoneType.
Which statement about None is correct?
None is the same as 0
None is the same as an empty string
None is a separate value meaning absence of a value, and must be written with a capital N
none and None are the same
None is not 0, False or "". Writing none (lowercase) gives a NameError.
Which group contains only mutable types?
str, list, tuple
list, dict, set
int, str, tuple
float, bool, complex
Only list, dictionary and set are mutable (the 'LDS' trick); int, float, bool, complex, str and tuple are immutable.
Every Python object has which three things?
Key, value, index
Name, size, colour
Address, port, protocol
Identity, type, value
Every object has an identity (id), a type (type()) and a value.
After a = 10 and then a = a + 5, what happens to id(a)?
It changes, because a new object 15 is created
It becomes None
It becomes 15
It stays the same
int is immutable, so Python creates a new object 15 and a now points to it; the id changes.
After marks = [70, 80, 90] and marks[0] = 100, what happens to id(marks)?
An error occurs
It becomes 100
It stays the same, because the list is changed in place
It changes
Lists are mutable, so they are modified in place and keep the same id.
What is the output of print(10 / 2)?
5
5.0
5.00
2
True division / always returns a float, even when the division is exact.
What is the output of print(17 // 5, 17 % 5)?
3 3
3.4 2
4 2
3 2
17 // 5 is the floor quotient, 3; 17 % 5 is the remainder, 2.
What is the output of print(-22 // 5, -22 % 5)?
-5 3
-5 -3
-4 -2
-4 2
-22 / 5 = -4.4, which floors to -5; then -5 ร 5 = -25, and -22 - (-25) = 3.
What is the output of print(17.0 // 5)?
3
4.0
3.0
3.4
Floor division with a float operand gives a float result: 3.0.
What is the output of print(2 ** 3 ** 2)?
64
512
72
36
** is right-associative, so 2 ** 3 ** 2 = 2 ** (3 ** 2) = 2 ** 9 = 512.
What is the output of print(2 ** 2 ** 3)?
64
16
12
256
Right-associative: 2 ** (2 ** 3) = 2 ** 8 = 256.
What is the output of print(-2 ** 2)?
-4
4
0
Error
** has higher precedence than unary minus, so it is -(2 ** 2) = -4.
What is the output of print(3 * "ab" + "c")?
abcabcabc
Error
abababc
ab3c
3 * "ab" gives "ababab", then + "c" gives "abababc".
What does print("Zoo" < "apple") display?
False
True
Zoo
Error
Strings are compared character by character using Unicode values; 'Z' (90) is less than 'a' (97).
The chained comparison 1 < 5 < 10 is equivalent to:
1 < 10
(1 < 5) < 10
1 < 5 or 5 < 10
1 < 5 and 5 < 10
Python evaluates a chained comparison as 1 < 5 and 5 < 10, which is True.
Why is 'if a = 5:' wrong?
It should be 'if a == 5:' โ = assigns, == compares
The colon is not allowed
It should be 'if a := 5'
Nothing is wrong
= is assignment and == is comparison; if a = 5: is a SyntaxError.
For a = [1, 2, 3], b = [1, 2, 3] and c = a, which result is correct?
a is b โ error
a == b โ False
a is c โ True
a is b โ True
a == b is True (same values) but a is b is False (different objects); c points to the same object as a, so a is c is True.
What is the difference between == and is?
is compares values; == compares ids
== compares values; is checks whether both refer to the same object in memory
== works only on numbers
There is no difference
== compares values, while is compares identity (the same id()).
Which of these values is falsy in Python?
[0]
" " (a string containing a space)
-1
{} (an empty dictionary)
Falsy values include 0, 0.0, 0j, "", [], (), {}, None and False; " ", [0] and -1 are truthy.
What is the output of print(0 or 7)?
7
True
False
0
x or y returns y when x is falsy; 0 is falsy, so it returns 7.
What is the output of print(5 or 7)?
7
True
5
12
x or y returns x if x is truthy; 5 is truthy, so it returns 5 without checking 7.
What is the output of print(5 and 7)?
5
7
35
True
x and y returns y when x is truthy, so 5 and 7 gives 7.
What is the output of print(0 and 5, 3 or 0, not 0)?
0 0 False
False True True
5 3 True
0 3 True
0 and 5 returns 0 (falsy first operand); 3 or 0 returns 3; not always returns a Boolean, so not 0 is True.
What is printed after this code?
x = 10 x += 5 x *= 2 x //= 4 print(x)
7
7.5
8
30
x becomes 15, then 30, then 30 // 4 = 7.
What happens when you write x++ in Python?
x becomes None
x is doubled
SyntaxError โ Python has no ++ operator
x increases by 1
Python has no ++ or -- operators; use x += 1.
What does print("a" in "Python") display?
True
False
Error
1
"Python" contains no lowercase 'a', so the result is False (whereas "th" in "Python" is True).
Which operator has the HIGHEST precedence (after parentheses)?
==
and
*
**
The order is ( ) โ ** โ unary +/- โ * / // % โ + - โ relational โ not โ and โ or.
Which logical operator has the LOWEST precedence?
or
==
and
not
or has the lowest precedence of the operators listed.
What is the value of 12 + 3 * 2 ** 2 // 5 - 1?
16
14
13
11
2 ** 2 = 4; 3 * 4 = 12; 12 // 5 = 2; 12 + 2 - 1 = 13.
What is the value of 20 // 3 + 2 ** 3 % 5?
8
9
3
6
2 ** 3 = 8; 20 // 3 = 6; 8 % 5 = 3; 6 + 3 = 9.
What is the value of not 5 > 3 or 4 < 2?
True
Error
None
False
The relational operators come first: not True or False โ False or False โ False.
What is the value of 20 / 4 * 2?
10.0
2.5
10
40
Same-precedence operators are evaluated left to right: (20 / 4) * 2 = 5.0 * 2 = 10.0.
What is the value of 5 + 3 * 2 ** 2 - 8 / 4?
30.0
13.0
15.0
15
2 ** 2 = 4; 3 * 4 = 12; 8 / 4 = 2.0; 5 + 12 - 2.0 = 15.0.
Implicit type conversion (coercion) is performed by:
The print() function
Python automatically, converting the smaller type to the bigger type
The programmer using int(), float()
The operating system
Coercion happens automatically in the order bool โ int โ float โ complex, so no data is lost.
What is the output of print(True + 3.5)?
3.5
Error
True3.5
4.5
True is converted to 1, then 1.0, so the result is 4.5.
What does int(7.9) return?
7
7.9
Error
8
int() cuts off the decimal part โ it truncates rather than rounds.
What does int("3.5") give?
3
3.5
ValueError
4
A string with a decimal point cannot be converted directly to int. Use int(float("3.5")), which gives 3.
What is the output of print(int(7.99) + float("2"))?
9
9.0
10.0
9.99
int(7.99) = 7 and float("2") = 2.0; 7 + 2.0 = 9.0.
Which Python expression correctly represents โ(bยฒ โ 4ac)?
sqrt b^2 - 4ac
(b ** 2 - 4 * a * c) ** 2
math.sqrt(b * 2 - 4ac)
math.sqrt(b ** 2 - 4 * a * c)
Use math.sqrt() with ** for the power and * for each multiplication.
Which type of error is 10 / 0?
Runtime error (ZeroDivisionError)
Logical error
No error
Syntax error
Dividing by zero raises a ZeroDivisionError while the program is running.
Writing avg = a + b / 2 instead of avg = (a + b) / 2 is an example of:
A runtime error
A NameError
A logical error
A syntax error
The program runs but gives the wrong result because of faulty logic.
Which error occurs when a variable is used before it has been defined?
ValueError
NameError
IndexError
TypeError
Using an undefined name, such as print(total) before total is assigned, raises a NameError.
int("hello") raises which error?
TypeError
NameError
SyntaxError
ValueError
The type (str) is acceptable to int(), but the value is invalid, so a ValueError is raised.