Quiz - 2 (Arrays and Dictionaries)

Q1: Which module in Python provides array support with fixed data types?

Answer: array
Explanation: The array module allows arrays with elements of the same data type.

Q2: How do you create an empty dictionary in Python?

Answer: {}
Explanation: Curly braces {} create an empty dictionary.

Q3: What will be the output?

arr = [10, 20, 30]
print(arr[1])
Answer: 20
Explanation: Indexing starts at 0, so arr[1] gives the second element.

Q4: Which method is used to add a key-value pair in a dictionary?

Answer: update()
Explanation: The update() method or direct assignment dict[key]=value adds a key-value pair.

Q5: What will be the output?

d = {"a": 1, "b": 2}
print(d["b"])
Answer: 2
Explanation: Accessing with key "b" returns its value 2.

Q6: Which method removes the last item from a list?

Answer: pop()
Explanation: pop() removes and returns the last element of a list.

Q7: Which statement is true about dictionaries?

Answer: They store key-value pairs
Explanation: A dictionary maps unique keys to values.

Q8: What will be the output?

arr = [1, 2, 3, 4]
print(len(arr))
Answer: 4
Explanation: The list has 4 elements, so len(arr) returns 4.

Q9: Which method returns all keys of a dictionary?

Answer: keys()
Explanation: keys() returns all keys of a dictionary.

Q10: What is the default return value of dict.get("missing") if key doesn’t exist?

Answer: None
Explanation: By default, get() returns None if key is not found.
Previous Post Next Post