wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Exploring Python Data Structures Quiz

Total questions: 15

Worksheet time: 8mins

Name
Class
Date
1.

Which of the following methods is used to add an element to the end of a list in Python?

a)

`insert()`

b)

`append()`

c)

`extend()`

d)

`add()`

2.

What will be the output of the following code snippet? ```python my_list = [1, 2, 3] my_list.insert(1, 4) print(my_list) ```

a)

`[1, 4, 2, 3]`

b)

`[1, 2, 3, 4]`

c)

`[4, 1, 2, 3]`

d)

`[1, 2, 4, 3]`

3.

Which of the following operations will remove all elements from a set in Python?

a)

`set.clear()`

b)

`set.remove()`

c)

`set.discard()`

d)

`set.delete()`

4.

What is the result of the following set operation? ```python set1 = {1, 2, 3} set2 = {3, 4, 5} result = set1.union(set2) ```

a)

`{1, 2, 3, 4, 5}`

b)

`{3}`

c)

`{1, 2, 3}`

d)

`{4, 5}`

5.

Which of the following is a correct dictionary comprehension to create a dictionary with keys as numbers from 1 to 3 and values as their squares?

a)

`{x: x**2 for x in range(1, 4)}`

b)

`{x: x*2 for x in range(1, 4)}`

c)

`{x: x+2 for x in range(1, 4)}`

d)

`{x: x**3 for x in range(1, 4)}`

6.

Consider the following nested data structure. How would you access the value `5`? ```python nested_list = [[1, 2, 3], [4, [5, 6]], 7] ```

a)

`nested_list[1][1][0]`

b)

`nested_list[1][0][1]`

c)

`nested_list[0][1][1]`

d)

`nested_list[1][1][1]`

7.

Which of the following data types is immutable in Python?

a)

List

b)

Set

c)

Tuple

d)

Dictionary

8.

What will be the output of the following code? ```python my_dict = {'a': 1, 'b': 2} my_dict['c'] = 3 print(my_dict) ```

a)

`{'a': 1, 'b': 2}`

b)

`{'a': 1, 'b': 2, 'c': 3}`

c)

`{'a': 1, 'c': 3}`

d)

`{'b': 2, 'c': 3}`

9.

Which method would you use to remove a key-value pair from a dictionary by key?

a)

`pop()`

b)

`remove()`

c)

`discard()`

d)

`delete()`

10.

What is the result of the following code? ```python my_set = {1, 2, 3} my_set.add(2) print(my_set) ```

a)

`{1, 2, 3, 2}`

b)

`{1, 2, 3}`

c)

`{1, 3}`

d)

`{2, 3}`

11.

Which of the following is a mutable data type in Python?

a)

String

b)

Tuple

c)

List

d)

Integer

12.

How can you create a set with the elements 1, 2, and 3 in Python?

a)

`set = {1, 2, 3}`

b)

`set = [1, 2, 3]`

c)

`set = (1, 2, 3)`

d)

`set = {1: 2, 3}`

13.

What will be the output of the following code? ```python my_tuple = (1, 2, 3) my_tuple[0] = 4 ```

a)

`(4, 2, 3)`

b)

`(1, 2, 3)`

c)

`TypeError`

d)

`(4, 1, 2, 3)`

14.

Which of the following is a valid way to iterate over a dictionary's keys and values?

a)

`for key, value in my_dict.items():`

b)

`for key, value in my_dict:`

c)

`for key, value in my_dict.keys():`

d)

`for key, value in my_dict.values():`

15.

What will be the output of the following code? ```python nested_dict = {'a': {'b': 2}} print(nested_dict['a']['b']) ```

a)

`2`

b)

`{'b': 2}`

c)

`KeyError`

d)

`None`