Week 3: Array, List, Set, Tuple Dictionary Flashcards
(21 cards)
Which of the following statements about arrays is true?
A) Arrays can store elements of different types.
B) Arrays are mutable and can change size dynamically.
C) Arrays store a fixed-size sequential collection of elements of the same type.
D) Arrays are unordered collections.
C) Arrays store a fixed-size sequential collection of elements of the same type.
Explanation: Arrays are a basic data structure that stores a fixed-size sequential collection of elements of the same type. They are not mutable and cannot change size dynamically.
Which of the following is a characteristic of a list in Python?
A) Lists are immutable.
B) Lists are unordered collections.
C) Lists can change size as items are added or removed.
D) Lists do not allow duplicate values.
C) Lists can change size as items are added or removed.
Explanation: Lists in Python are mutable sequence containers that can change size as items are added or removed. They are ordered collections and can contain duplicate values.
What will be the output of the following code?
fruits = ["Apple", "Banana", "Coconut"] print(fruits[0:2])
A) [“Apple”, “Banana”]
B) [“Banana”, “Coconut”]
C) [“Coconut”]
D) [“Apple”, “Coconut”]
A) [“Apple”, “Banana”]
Explanation: The code slices the list fruits from index 0 to 2 (excluding 2), resulting in [“Apple”, “Banana”].
Which of the following is NOT a characteristic of a set in Python?
A) Sets are unordered.
B) Sets are immutable.
C) Sets do not allow duplicate values.
D) Sets can add or remove values.
B) Sets are immutable.
Explanation: Sets in Python are unordered collections that do not allow duplicate values. They are immutable, meaning you can add or remove values.
What will be the output of the following code?
fruits = {"Apple", "Banana", "Coconut"} fruits.add("Dragon Fruit") print(fruits)
A) {“Apple”, “Banana”, “Coconut”}
B) {“Apple”, “Banana”, “Coconut”, “Dragon Fruit”}
C) {“Dragon Fruit”}
D) {“Apple”, “Banana”}
B) {“Apple”, “Banana”, “Coconut”, “Dragon Fruit”}
Explanation: The add method adds “Dragon Fruit” to the set fruits, resulting in {“Apple”, “Banana”, “Coconut”, “Dragon Fruit”}.
Which of the following statements about tuples is true?
A) Tuples are mutable.
B) Tuples are unordered collections.
C) Tuples allow duplicate values.
D) Tuples can change size dynamically.
C) Tuples allow duplicate values.
Explanation: Tuples are ordered collections that allow duplicate values. They are immutable, meaning their size and content cannot be changed after creation.
Which of the following best describes a dictionary in Python?
A) A collection of ordered elements.
B) A collection of key-value pairs.
C) A collection of unique values.
D) A collection of immutable elements.
B) A collection of key-value pairs.
Explanation: A dictionary in Python is a collection of key-value pairs, where each key is unique and is used to store and retrieve values.
What will be the output of the following code?
colors = {"Apple": "Red", "Banana": "Yellow", "Lime": "Green"} colors.update({"Coconut": "Brown"}) print(colors)
A) {“Apple”: “Red”, “Banana”: “Yellow”}
B) {“Apple”: “Red”, “Banana”: “Yellow”, “Lime”: “Green”}
C) {“Apple”: “Red”, “Banana”: “Yellow”, “Lime”: “Green”, “Coconut”: “Brown”}
D) {“Coconut”: “Brown”}
C) {“Apple”: “Red”, “Banana”: “Yellow”, “Lime”: “Green”, “Coconut”: “Brown”}
Explanation: The update method adds the key-value pair {“Coconut”: “Brown”} to the dictionary colors, resulting in {“Apple”: “Red”, “Banana”: “Yellow”, “Lime”: “Green”, “Coconut”: “Brown”}.
What will be the output of the following code?
fruits = ["Apple", "Banana", "Coconut"] print(fruits[::2])
A) [“Apple”, “Banana”]
B) [“Banana”, “Coconut”]
C) [“Apple”, “Coconut”]
D) [“Coconut”]
C) [“Apple”, “Coconut”]
Explanation: The code slices the list fruits with a step of 2, resulting in [“Apple”, “Coconut”].
What will be the output of the following code?
fruits = ["Apple", "Banana", "Coconut"] print(fruits[::-1])
A) [“Apple”, “Banana”]
B) [“Banana”, “Coconut”]
C) [“Coconut”, “Banana”, “Apple”]
D) [“Apple”, “Coconut”]
C) [“Coconut”, “Banana”, “Apple”]
Explanation: The code reverses the list fruits, resulting in [“Coconut”, “Banana”, “Apple”].
What will be the output of the following code?
fruits = ["Apple", "Banana", "Coconut"] print("Apple" in fruits) print("Pineapple" in fruits)
A) True, False
B) False, True
C) True, True
D) False, False
A) True, False
Explanation: The code checks for membership in the list fruits. “Apple” is in the list, so it returns True. “Pineapple” is not in the list, so it returns False.
What will be the output of the following code?
fruits = {"Apple", "Banana", "Coconut"} fruits.pop() print(fruits)
A) {“Apple”, “Banana”, “Coconut”}
B) {“Banana”, “Coconut”}
C) {“Apple”, “Coconut”}
D) {“Apple”, “Banana”}
B) {“Banana”, “Coconut”}
Explanation: The pop method removes an arbitrary element from the set fruits. Since sets are unordered, the exact element removed can vary, but the remaining elements will be {“Banana”, “Coconut”}.
What will be the output of the following code?
colors = {"Apple": "Red", "Banana": "Yellow", "Lime": "Green"} colors.pop("Lime") print(colors)
A) {“Apple”: “Red”, “Banana”: “Yellow”}
B) {“Apple”: “Red”, “Banana”: “Yellow”, “Lime”: “Green”}
C) {“Lime”: “Green”}
D) {“Apple”: “Red”}
A) {“Apple”: “Red”, “Banana”: “Yellow”}
Explanation: The pop method removes the key-value pair with the key “Lime” from the dictionary colors, resulting in {“Apple”: “Red”, “Banana”: “Yellow”}.
What will be the output of the following code?
colors = {"Apple": "Red", "Banana": "Yellow", "Lime": "Green"} keys = colors.keys() for key in keys: print(key)
A) Apple, Banana, Lime
B) Red, Yellow, Green
C) Apple, Red, Banana, Yellow, Lime, Green
D) Red, Apple, Yellow, Banana, Green, Lime
A) Apple, Banana, Lime
Explanation: The code iterates over the keys of the dictionary colors, printing each key: Apple, Banana, Lime.
What will be the output of the following code?
colors = {"Apple": "Red", "Banana": "Yellow", "Lime": "Green"} values = colors.values() for value in values: print(value)
A) Apple, Banana, Lime
B) Red, Yellow, Green
C) Apple, Red, Banana, Yellow, Lime, Green
D) Red, Apple, Yellow, Banana, Green, Lime
B) Red, Yellow, Green
Explanation: The code iterates over the values of the dictionary colors, printing each value: Red, Yellow, Green.
What will be the output of the following code?
colors = {"Apple": "Red", "Banana": "Yellow", "Lime": "Green"} keys = colors.keys() for key in keys: print(key)
A) Apple, Banana, Lime
B) Red, Yellow, Green
C) Apple, Red, Banana, Yellow, Lime, Green
D) Red, Apple, Yellow, Banana, Green, Lime
A) Apple, Banana, Lime
Explanation: The code iterates over the keys of the dictionary colors, printing each key: Apple, Banana, Lime.
What will be the output of the following code?
fruits = ("Apple", "Banana", "Coconut") print(fruits[1])
A) Apple
B) Banana
C) Coconut
D) IndexError
B) Banana
The code accesses the element at index 1 of the tuple fruits, which is “Banana”.
What will be the output of the following code?
fruits = ("Apple", "Banana", "Coconut") print(fruits[0:2])
A) (“Apple”, “Banana”)
B) (“Banana”, “Coconut”)
C) (“Coconut”,)
D) (“Apple”, “Coconut”)
A) (“Apple”, “Banana”)
The code slices the tuple fruits from index 0 to 2 (excluding 2), resulting in (“Apple”, “Banana”).
What will be the output of the following code?
fruits = ("Apple", "Banana", "Coconut") fruits[1] = "Blueberry" print(fruits)
A) (“Apple”, “Blueberry”, “Coconut”)
B) (“Apple”, “Banana”, “Coconut”)
C) TypeError
D) (“Blueberry”, “Banana”, “Coconut”)
C) TypeError
Tuples are immutable, meaning their elements cannot be changed after creation. Attempting to assign a new value to an element of a tuple will raise a TypeError.
What will be the output of the following code?
colors = {"Apple": "Red", "Banana": "Yellow", "Lime": "Green"} print(colors["Banana"])
A) Apple
B) Yellow
C) Green
D) KeyError
B) Yellow
Explanation: The code accesses the value associated with the key “Banana” in the dictionary colors, which is “Yellow”.
What will be the output of the following code?
colors = {"Apple": "Red", "Banana": "Yellow", "Lime": "Green"} print(colors["Orange"])
A) None
B) KeyError
C) “”
D) Orange
B) KeyError
Explanation: Attempting to access a key that does not exist in the dictionary colors will raise a KeyError.