NEW
Font size
WorksheetsPython: Built-In Functions and Upto Tuple
Total questions: 50
Worksheet time: 16mins
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]
What will the following code output?
for i in range(3):
↳ for j in range(2):
↳ print(i * j, end=" ")
Note: ↳ indicating that the statement is the part of its above block
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
Note: ↳ indicating that the statement is the part of its above block
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 will the following code do?
x = 0
while x < 3:
↳print(x, end=" ")
↳x += 1
else:
↳print("Done")
Note: ↳ indicating that the statement is the part of its above block
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
print(sum([1, 2, 3, 4], 5))
10
15
20
TypeError
What does the sorted() function return?
A sorted list
A sorted tuple
A sorted set
A new list in reverse order
x = "print(10+5)"
exec(x)
15
'10+5'
Error
None
What does the eval() function do?
Compiles Python code
Executes a string as a Python expression
Converts string to integer
Reads input
What does the zip() function return?
A list
A dictionary
A zip object
A tuple
Which of the following functions returns an enumerator object?
enumerate()
sorted()
zip()
map()
What will be the output of abs(-7.5)?
-7.5
7.5
7
None
What does the all() function return for an empty list?
False
True
None
Error
What does the any() function return for an empty list?
False
True
None
Error
What does the any() function return for an iterable containing only False values?
True
False
None
Error
What will be the output of round(4.675, 2)?
4.67
4.68
4.675
4.66
What will be the output of min("Banana")?
a
n
B
Error
What does ord("MAYANK SAXENA".split()[1][-1:-2:-1]) return?
Error
List can't treat as String
65
78
What does chr(sorted(tuple([65, 70, 78, 97, 122, 90]))[-1]-32) return?
Error
-32 is negative so it will not be acceptable
'z'
'Z'
What does bool([]) return?
True
False
Error
None
What will be the output of pow(2, 3, 5)?
8
3
5
Error
What will be the output of divmod(10, 3)?
(3, 1)
(3, 3)
(3, 0)
Error
What will be the output of bin(10)
'0b10'
'0b1010'
'10'
10
What does map(str, [1, 2, 3]) return?
['1', '2', '3']
('1', '2', '3')
A map object
None
What is the result of bool("0")?
True
False
0
None
What is the result of bool(" ")?
True
False
None
Error
Which function is used to apply a function to each item of an iterable?
filter()
map()
reduce()
zip()
for i, v in enumerate(['apple', 'banana'], start=1):
print(i, v)
0 apple
1 banana
1 apple
2 banana
apple 0
banana 1
apple 1
banana 2
Which of the following best describes the purpose of enumerate()?
It generates indices only.
It returns the original list unchanged.
It pairs each element with an index.
It sorts the list by indices.
What is the output of the following code?
print(list(zip([1, 2, 3], ['a', 'b'])))
[(1, 2, 3), ('a', 'b', 'c')]
[(1, 'a'), (2, 'b')]
[[1, 'a'], [2, 'b'], [3, 'c']]
{1: 'a', 2: 'b', 3: 'c'}
How does zip() handle iterables of different lengths?
Raises an error
Fills missing values with None
Truncates to the length of the shortest iterable
Repeats the shorter iterable
Given the following code snippet, what is the value of variable b?
t = (1, 2, 3, 4, (5, 6, 7))
a, *b, (c, d, e) = t
[1, 2, 3, 4]
[2, 3, 4]
(2, 3, 4)
[(2, 3, 4)]
