WorksheetsPython Lists MCQs Quiz
Total questions: 48
Worksheet time: 24mins
Which of the following is the correct syntax for list comprehension in Python?
[expression for item in iterable]
[for item in iterable expression]
(expression item for iterable)
{expression for iterable in item}
What will be the output of: [x**2 for x in range(4)]
[1, 4, 9, 16]
[0, 1, 4, 9]
[0, 1, 2, 3]
Error
Which of these comprehensions filters only even numbers? nums = [1, 2, 3, 4, 5, 6]
[x for x in nums if x%2]
[x for x in nums if x%2==0]
[x if x%2==0 for x in nums]
[if x%2==0 x for x in nums]
What is the output of: [word.upper() for word in ["a", "b", "c"]]
['a', 'b', 'c']
['A', 'B', 'C']
['A B C']
Error
map() function returns?
A list
An iterator
A tuple
A dictionary
Which of these doubles each element? nums = [1, 2, 3]
list(map(lambda x: x*2, nums))
map(nums, lambda x: x*2)
[nums*2 for x in nums]
map(lambda x: x**2, nums)
filter() is used for?
Modifying data
Filtering data by condition
Reducing sequence
Iterating indexes
Which keeps words longer than 3 letters? words = ["hi", "hello", "sun"]
filter(lambda w: len(w)<3, words)
filter(lambda w: len(w)>3, words)
map(lambda w: len(w), words)
reduce(lambda w: w, words)
reduce() is part of which module?
itertools
functools
operator
collections
reduce(lambda x,y:x+y,[1,2,3,4]) =
24
10
[10]
Error
What will be output? reduce(lambda x,y: x*y, [1,2,3,4])
10
24
1
Error
Which finds max element using reduce? nums=[3,7,2,9]
reduce(lambda x,y: x if x>y else y, nums)
reduce(lambda x,y: max(nums))
reduce(max,nums)
Both a and c
Which of these list comprehensions is invalid?
[x for x in range(5)]
[x if x%2==0 for x in range(5)]
[x for x in range(5) if x%2==0]
[if x%2==0: x for x in range(5)]
What will be output? [[i*j for j in range(3)] for i in range(3)]
[[1,2,3],[2,4,6],[3,6,9]]
[[0,0,0],[0,1,2],[0,2,4]]
[[0,1,2],[0,2,4],[0,3,6]]
Error
How to create 2D list 3x3 zeros?
[[0]*3]*3
[[0 for j in range(3)] for i in range(3)]
Both
None
Problem with [[0]*3]*3?
It works fine
Rows are references to same object
Syntax error
Creates jagged list
Jagged list means?
2D with equal length rows
2D with variable length rows
Flat list
Error in memory
Example of jagged list?
[[1,2,3],[4,5,6]]
[[1,2],[3,4,5,6]]
[[],[],[]]
[[1],[2],[3]]
Accessing element at row 2 col 3 in 2D list a?
a[2,3]
a[2][3]
a[3][2]
a[2:3]
Which comprehension flattens 2D list? matrix=[[1,2],[3,4]]
[x for row in matrix for x in row]
[x for x in row for row in matrix]
flatten(matrix)
Both a and b
What will be output? list(map(str.upper,["ab","cd"]))
['AB','CD']
['ab','cd']
['Ab','Cd']
Error
filter(None,[0,"",1,"hello"])=?
[0,"",1,"hello"]
[1,"hello"]
[0,""]
Error
Which is equivalent to sum(nums)?
reduce(lambda x,y:x+y,nums)
map(sum,nums)
filter(sum,nums)
None
In 2D list a=[[1,2],[3,4]], len(a)=?
4
2
Error
None
In 2D list a=[[1,2],[3,4]]
len(a[0]) for above?
4
2
Error
Jagged list example a=[[1,2,3],[4,5]] len(a[0])=?
3
2
Error
Which is invalid?
[x for x in range(5)]
map(lambda x:x**2,[1,2,3])
filter(lambda x:x>2,[1,2,3])
reduce(x+y,[1,2,3])
Which comprehension squares even numbers only?
[x**2 for x in range(6) if x%2==0]
[x**2 if x%2==0 for x in range(6)]
[if x%2==0: x**2 for x in range(6)]
None
map and filter return?
List
Iterator
Tuple
Generator
reduce is used to?
Apply binary function cumulatively
Filter sequence
Transform elements
Iterate indices
What does this produce? [[i for i in range(j)] for j in range(4)]
[[0,1,2,3]]
[[],[0],[0,1],[0,1,2]]
[[0],[1],[2],[3]]
Error
Jagged lists are useful when?
Data is always rectangular
Rows can vary in size
Using NumPy arrays
None
Which produces multiplication table? [[i*j for j in range(1,6)] for i in range(1,6)]
5x5 table
Error
Jagged list
Flat list
list(map(int,["1","2","3"]))=?
['1','2','3']
[1,2,3]
[123]
Error
filter(lambda x:x%2==0,range(5)) produces?
[0,2,4]
[1,3]
[0,1,2,3,4]
Iterator
reduce(lambda x,y:x+y,[],10)=?
10
0
[]
Error
Which comprehension creates 3x2 matrix? [[i+j for j in range(2)] for i in range(3)]
[[0,1],[1,2],[2,3]]
[[0,1,2],[1,2,3]]
2x3
Error
In jagged list a=[[1,2,3],[4],[5,6]] len(a)=?
3
6
Error
In jagged list a=[[1,2,3],[4],[5,6]]
Accessing a[1][0] above=?
2
4
5
Error
What is output? [x+y for x in [1,2] for y in [3,4]]
[4,5,5,6]
[[4,5],[5,6]]
[13,24]
Error
Which creates list of tuples (x,y)? [(x,y) for x in [1,2] for y in [3,4]]
[(1,3),(1,4),(2,3),(2,4)]
[[1,3],[1,4],[2,3],[2,4]]
[13,24]
Error
map(str, [1,2,3])=?
['1','2','3']
[1,2,3]
Error
filter(lambda x:x>0,[-1,0,1,2])=?
[-1,0,1,2]
[1,2]
[0,1,2]
Error
Which creates jagged list? [[i]*i for i in range(5)]
[[0],[1],[2,2],[3,3,3],[4,4,4,4]]
[[0,0],[1,1,1]]
[[0,1],[2,3]]
Error
Which is invalid about jagged lists?
Rows can vary in length
Indexing is same as 2D lists
They are rectangular
Useful for irregular data
Which generates prime numbers <=10? [x for x in range(2,11) if all(x%p for p in range(2,x))]
[2,3,5,7]
[2,4,6,8,10]
[3,5,7]
Error
list(map(lambda x:x+1,[1,2,3]))=?
[2,3,4]
[1,2,3,1,2,3]
[1,2,3]
Error
flatten=[[item for sublist in [[1,2],[3,4]] for item in sublist]] flatten=?
[[1,2,3,4]]
[1,2,3,4]
[12,34]
Error
