WorksheetsExploring Python Data Structures Quiz
Total questions: 15
Worksheet time: 8mins
Which of the following methods is used to add an element to the end of a list in Python?
`insert()`
`append()`
`extend()`
`add()`
What will be the output of the following code snippet? ```python my_list = [1, 2, 3] my_list.insert(1, 4) print(my_list) ```
`[1, 4, 2, 3]`
`[1, 2, 3, 4]`
`[4, 1, 2, 3]`
`[1, 2, 4, 3]`
Which of the following operations will remove all elements from a set in Python?
`set.clear()`
`set.remove()`
`set.discard()`
`set.delete()`
What is the result of the following set operation? ```python set1 = {1, 2, 3} set2 = {3, 4, 5} result = set1.union(set2) ```
`{1, 2, 3, 4, 5}`
`{3}`
`{1, 2, 3}`
`{4, 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?
`{x: x**2 for x in range(1, 4)}`
`{x: x*2 for x in range(1, 4)}`
`{x: x+2 for x in range(1, 4)}`
`{x: x**3 for x in range(1, 4)}`
Consider the following nested data structure. How would you access the value `5`? ```python nested_list = [[1, 2, 3], [4, [5, 6]], 7] ```
`nested_list[1][1][0]`
`nested_list[1][0][1]`
`nested_list[0][1][1]`
`nested_list[1][1][1]`
Which of the following data types is immutable in Python?
List
Set
Tuple
Dictionary
What will be the output of the following code? ```python my_dict = {'a': 1, 'b': 2} my_dict['c'] = 3 print(my_dict) ```
`{'a': 1, 'b': 2}`
`{'a': 1, 'b': 2, 'c': 3}`
`{'a': 1, 'c': 3}`
`{'b': 2, 'c': 3}`
Which method would you use to remove a key-value pair from a dictionary by key?
`pop()`
`remove()`
`discard()`
`delete()`
What is the result of the following code? ```python my_set = {1, 2, 3} my_set.add(2) print(my_set) ```
`{1, 2, 3, 2}`
`{1, 2, 3}`
`{1, 3}`
`{2, 3}`
Which of the following is a mutable data type in Python?
String
Tuple
List
Integer
How can you create a set with the elements 1, 2, and 3 in Python?
`set = {1, 2, 3}`
`set = [1, 2, 3]`
`set = (1, 2, 3)`
`set = {1: 2, 3}`
What will be the output of the following code? ```python my_tuple = (1, 2, 3) my_tuple[0] = 4 ```
`(4, 2, 3)`
`(1, 2, 3)`
`TypeError`
`(4, 1, 2, 3)`
Which of the following is a valid way to iterate over a dictionary's keys and values?
`for key, value in my_dict.items():`
`for key, value in my_dict:`
`for key, value in my_dict.keys():`
`for key, value in my_dict.values():`
What will be the output of the following code? ```python nested_dict = {'a': {'b': 2}} print(nested_dict['a']['b']) ```
`2`
`{'b': 2}`
`KeyError`
`None`
