WorksheetsPython Quiz
Total questions: 40
Worksheet time: 15mins
What will print([1, 2, 3] + [4, 5]) output?
[1, 2, 3, 4, 5]
[5, 4, 3, 2, 1]
Error
[1, 2, 3, [4, 5]]
Which of the following will create a shallow copy of a list a?
b = a[:]
b = a.copy()
b = a[:]
and
b = a.copy()
both options correct
None of the above
What will print(len([[1, 2], [3, 4]])) return?
2
4
8
Error
Which of the following will raise an error for tuple a?
a = (1, 2)
a[0] = 3
len((1, 2, 3))
(1, 2) + (3,)
What is the output of print((1, 2) * 2)?
(1, 2, 1, 2)
(1, 2, 2)
Error
[1, 2, 1, 2]
Which method is used to get the value associated with a key in a dictionary?
value()
get()
key()
items()
What will print(len({1: "a", 2: "b"})) return?
1
2
3
4
What will print({}.get("key", "default")) output?
None
default
key
Error
Which of the following will raise an error?
{1: "a", 2: "b"}[1]
{1: "a", 2: "b"}["c"]
{1: "a", 2: "b"}.get(3)
{}
What will print({1, 2, 3} | {3, 4, 5}) output?
Error
{1, 2, 4, 5}
{3}
{1, 2, 3, 4, 5}
Which method removes an element from a set without raising an error if the element is absent?
remove()
pop()
discard()
clear()
What will print({1, 2, 3} & {2, 3, 4}) output?
{1, 2, 3, 4}
{2, 3}
{1, 4}
Error
What is the result of print(len({1, 2, 2, 3}))?
3
4
Error
2
What happens when print({1, 2, [3, 4]}) is executed?
{1, 2, [3, 4]}
{1, 2}
Error
None of the above
Evaluate the code:
a = {1, 2, 3}
b = a.pop(1)
Sets do not support pop()
pop() does not accept arguments
Sets are immutable
None of the above
Evaluate the following statement in Python and correct the error:
my_dict = {[1, 2]: "value"}
Replace [1, 2] with (1, 2)
Replace [1, 2] with 1, 2
Use dict() instead
None of the above
What will print({x**2 for x in range(3)}) output?
{1, 4, 0}
{4, 0, 1}
{0, 1, 4}
Error
What is the output of print(2 in {1, 2, 3})?
True
False
None
Error
What will the following code output?
for i in range(3):
for j in range(2):
print(i * j, end=" ")
0 0 0 1 0 2
0 0 0 0 0 1
0 0 0 1
0 0 0 0 1 2
Error Correction: Fix the code to break the loop:
i = 0
while True:
print(i)
if i == 5:
break
Add i += 1 before break
Use for instead of while
Add i += 1 after print(i)
Replace break with continue
What will be the output of the following code?
s = "hello"
print(s[-1:-5:-1])
olleh
hello
olle
Error
What is the result of
''.join(sorted("cabac"))?
cabac
aabcc
abacc
Error
Which of the following results in a valid deep copy of a list?
b = a.copy()
b = copy.deepcopy(a)
b = a[:]
b = list(a)
What is the output of print((1, 2) * 0)?
()
(1, 2)
[]
Error
What will be the result of the following code?
t = (1, [2, 3], 4)
t[1][0] = 0
print(t)
(1, [2, 3], 4)
(1, [0, 3], 4)
Error
(1, 2, 3, 4)
What does the following code output?
d = {"a": 1, "b": 2}
print(d.get("c", d["a"]))
None
1
KeyError
2
What will the following code output?
s1 = {1, 2, 3}
s2 = {3, 4, 5}
print(s1 - s2)
{1, 2}
{4, 5}
{1, 2, 3, 4, 5}
Error
Which of the following will raise an error?
{1, 2}.add(3)
{1, 2}.update({3, 4})
{1, 2}.remove(3)
{1, 2}.discard(3)
What will happen in the following code?
def func(a=[]):
a.append(1)
return a
print(func(), end=", ")
print(func())
[1], [1]
[1], [1, 1]
Error
[1], []
What will the following code do?
x = 0
while x < 3:
print(x, end=" ")
x += 1
else:
print("Done")
0 1 2
0 1 2 Done
Done
Error
What will the following code output?
result = [x**2 for x in range(5) if x % 2 == 0]
print(result)
[0, 1, 4, 9, 16]
[0, 4, 16]
[1, 9]
Error
What does the following code produce?
result = [x + y for x in "abc" for y in "12"]
print(result)
['a1', 'a2', 'b1', 'b2', 'c1', 'c2']
['a1b2', 'c1']
['a1', 'b2', 'c1', 'c2']
Error
Which of the following comprehensions creates a list of tuples (x, y) where x is odd and y is even for x, y ∈ range(4)? Consider 0 in Even.
A. [(x, y) for x in range(4) for y in range(4) if x % 2 == 1 and y % 2 == 0]
B. [(x, y) for x in range(4) if x % 2 == 1 for y in range(4) if y % 2 == 0]
C. [(x, y) for y in range(4) if y % 2 == 0 for x in range(4) if x % 2 == 1]
A
B
C
All of the above
What will print([x for x in range(10) if x < 5 if x % 2 == 0]) output?
[0, 1, 2, 3, 4]
[0, 2, 4]
[0, 4]
Error
What will print([x if x % 2 == 0 else -x for x in range(5)]) output?
[0, 1, 2, 3, 4]
[0, -1, 2, -3, 4]
[0, -1, -2, -3, -4]
Error
What does this code output?
result = [[x * y for x in range(3)] for y in range(3)]
print(result)
[[0, 0, 0], [0, 1, 2], [0, 2, 4]]
[[0, 0, 0], [1, 2, 3], [4, 5, 6]]
[[0, 1, 2], [1, 2, 3], [2, 3, 4]]
Error
What will be the output of the following code?
result = [x for x in range(10) if x in [y for y in range(5, 15)]]
print(result)
[5, 6, 7, 8, 9]
[0, 1, 2, 3, 4]
[10]
Error
What does this comprehension produce?
result = [x + y for x, y in zip(range(3), range(3, 6))]
print(result)
[3, 5, 7]
[3, 4, 5]
[4, 5, 6]
Error
What does the del keyword do in Python?
Deletes variables and objects from memory
Deletes keys from dictionaries
Deletes elements from a list
All of the above
Which of the following statements about Python keywords is true?
Python keywords can be used as variable names
The is keyword compares identity, not equality
The def keyword is used for declaring classes
The break keyword is used to skip iterations in loops
