Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python Lists MCQs Quiz

Total questions: 48

Worksheet time: 24mins

Name
Class
Date
1.

Which of the following is the correct syntax for list comprehension in Python?

a)

[expression for item in iterable]

b)

[for item in iterable expression]

c)

(expression item for iterable)

d)

{expression for iterable in item}

2.

What will be the output of: [x**2 for x in range(4)]

a)

[1, 4, 9, 16]

b)

[0, 1, 4, 9]

c)

[0, 1, 2, 3]

d)

Error

3.

Which of these comprehensions filters only even numbers? nums = [1, 2, 3, 4, 5, 6]

a)

[x for x in nums if x%2]

b)

[x for x in nums if x%2==0]

c)

[x if x%2==0 for x in nums]

d)

[if x%2==0 x for x in nums]

4.

What is the output of: [word.upper() for word in ["a", "b", "c"]]

a)

['a', 'b', 'c']

b)

['A', 'B', 'C']

c)

['A B C']

d)

Error

5.

map() function returns?

a)

A list

b)

An iterator

c)

A tuple

d)

A dictionary

6.

Which of these doubles each element? nums = [1, 2, 3]

a)

list(map(lambda x: x*2, nums))

b)

map(nums, lambda x: x*2)

c)

[nums*2 for x in nums]

d)

map(lambda x: x**2, nums)

7.

filter() is used for?

a)

Modifying data

b)

Filtering data by condition

c)

Reducing sequence

d)

Iterating indexes

8.

Which keeps words longer than 3 letters? words = ["hi", "hello", "sun"]

a)

filter(lambda w: len(w)<3, words)

b)

filter(lambda w: len(w)>3, words)

c)

map(lambda w: len(w), words)

d)

reduce(lambda w: w, words)

9.

reduce() is part of which module?

a)

itertools

b)

functools

c)

operator

d)

collections

10.

reduce(lambda x,y:x+y,[1,2,3,4]) =

a)

24

b)

10

c)

[10]

d)

Error

11.

What will be output? reduce(lambda x,y: x*y, [1,2,3,4])

a)

10

b)

24

c)

1

d)

Error

12.

Which finds max element using reduce? nums=[3,7,2,9]

a)

reduce(lambda x,y: x if x>y else y, nums)

b)

reduce(lambda x,y: max(nums))

c)

reduce(max,nums)

d)

Both a and c

13.

Which of these list comprehensions is invalid?

a)

[x for x in range(5)]

b)

[x if x%2==0 for x in range(5)]

c)

[x for x in range(5) if x%2==0]

d)

[if x%2==0: x for x in range(5)]

14.

What will be output? [[i*j for j in range(3)] for i in range(3)]

a)

[[1,2,3],[2,4,6],[3,6,9]]

b)

[[0,0,0],[0,1,2],[0,2,4]]

c)

[[0,1,2],[0,2,4],[0,3,6]]

d)

Error

15.

How to create 2D list 3x3 zeros?

a)

[[0]*3]*3

b)

[[0 for j in range(3)] for i in range(3)]

c)

Both

d)

None

16.

Problem with [[0]*3]*3?

a)

It works fine

b)

Rows are references to same object

c)

Syntax error

d)

Creates jagged list

17.

Jagged list means?

a)

2D with equal length rows

b)

2D with variable length rows

c)

Flat list

d)

Error in memory

18.

Example of jagged list?

a)

[[1,2,3],[4,5,6]]

b)

[[1,2],[3,4,5,6]]

c)

[[],[],[]]

d)

[[1],[2],[3]]

19.

Accessing element at row 2 col 3 in 2D list a?

a)

a[2,3]

b)

a[2][3]

c)

a[3][2]

d)

a[2:3]

20.

Which comprehension flattens 2D list? matrix=[[1,2],[3,4]]

a)

[x for row in matrix for x in row]

b)

[x for x in row for row in matrix]

c)

flatten(matrix)

d)

Both a and b

21.

What will be output? list(map(str.upper,["ab","cd"]))

a)

['AB','CD']

b)

['ab','cd']

c)

['Ab','Cd']

d)

Error

22.

filter(None,[0,"",1,"hello"])=?

a)

[0,"",1,"hello"]

b)

[1,"hello"]

c)

[0,""]

d)

Error

23.

Which is equivalent to sum(nums)?

a)

reduce(lambda x,y:x+y,nums)

b)

map(sum,nums)

c)

filter(sum,nums)

d)

None

24.

In 2D list a=[[1,2],[3,4]], len(a)=?

a)

4

b)

2

c)

Error

d)

None

25.

In 2D list a=[[1,2],[3,4]]

len(a[0]) for above?

a)

4

b)

2

c)

Error

26.

Jagged list example a=[[1,2,3],[4,5]] len(a[0])=?

a)

3

b)

2

c)

Error

27.

Which is invalid?

a)

[x for x in range(5)]

b)

map(lambda x:x**2,[1,2,3])

c)

filter(lambda x:x>2,[1,2,3])

d)

reduce(x+y,[1,2,3])

28.

Which comprehension squares even numbers only?

a)

[x**2 for x in range(6) if x%2==0]

b)

[x**2 if x%2==0 for x in range(6)]

c)

[if x%2==0: x**2 for x in range(6)]

d)

None

29.

map and filter return?

a)

List

b)

Iterator

c)

Tuple

d)

Generator

30.

reduce is used to?

a)

Apply binary function cumulatively

b)

Filter sequence

c)

Transform elements

d)

Iterate indices

31.

What does this produce? [[i for i in range(j)] for j in range(4)]

a)

[[0,1,2,3]]

b)

[[],[0],[0,1],[0,1,2]]

c)

[[0],[1],[2],[3]]

d)

Error

32.

Jagged lists are useful when?

a)

Data is always rectangular

b)

Rows can vary in size

c)

Using NumPy arrays

d)

None

33.

Which produces multiplication table? [[i*j for j in range(1,6)] for i in range(1,6)]

a)

5x5 table

b)

Error

c)

Jagged list

d)

Flat list

34.

list(map(int,["1","2","3"]))=?

a)

['1','2','3']

b)

[1,2,3]

c)

[123]

d)

Error

35.

filter(lambda x:x%2==0,range(5)) produces?

a)

[0,2,4]

b)

[1,3]

c)

[0,1,2,3,4]

d)

Iterator

36.

reduce(lambda x,y:x+y,[],10)=?

a)

10

b)

0

c)

[]

d)

Error

37.

Which comprehension creates 3x2 matrix? [[i+j for j in range(2)] for i in range(3)]

a)

[[0,1],[1,2],[2,3]]

b)

[[0,1,2],[1,2,3]]

c)

2x3

d)

Error

38.

In jagged list a=[[1,2,3],[4],[5,6]] len(a)=?

a)

3

b)

6

c)

Error

39.

In jagged list a=[[1,2,3],[4],[5,6]]

Accessing a[1][0] above=?

a)

2

b)

4

c)

5

d)

Error

40.

What is output? [x+y for x in [1,2] for y in [3,4]]

a)

[4,5,5,6]

b)

[[4,5],[5,6]]

c)

[13,24]

d)

Error

41.

Which creates list of tuples (x,y)? [(x,y) for x in [1,2] for y in [3,4]]

a)

[(1,3),(1,4),(2,3),(2,4)]

b)

[[1,3],[1,4],[2,3],[2,4]]

c)

[13,24]

d)

Error

42.

map(str, [1,2,3])=?

a)

['1','2','3']

b)

[1,2,3]

c)

Error

43.

filter(lambda x:x>0,[-1,0,1,2])=?

a)

[-1,0,1,2]

b)

[1,2]

c)

[0,1,2]

d)

Error

44.

Which creates jagged list? [[i]*i for i in range(5)]

a)

[[0],[1],[2,2],[3,3,3],[4,4,4,4]]

b)

[[0,0],[1,1,1]]

c)

[[0,1],[2,3]]

d)

Error

45.

Which is invalid about jagged lists?

a)

Rows can vary in length

b)

Indexing is same as 2D lists

c)

They are rectangular

d)

Useful for irregular data

46.

Which generates prime numbers <=10? [x for x in range(2,11) if all(x%p for p in range(2,x))]

a)

[2,3,5,7]

b)

[2,4,6,8,10]

c)

[3,5,7]

d)

Error

47.

list(map(lambda x:x+1,[1,2,3]))=?

a)

[2,3,4]

b)

[1,2,3,1,2,3]

c)

[1,2,3]

d)

Error

48.

flatten=[[item for sublist in [[1,2],[3,4]] for item in sublist]] flatten=?

a)

[[1,2,3,4]]

b)

[1,2,3,4]

c)

[12,34]

d)

Error