Week 3: Array, List, Set, Tuple Dictionary Flashcards

(21 cards)

1
Q

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.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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

A) [“Apple”, “Banana”]

Explanation: The code slices the list fruits from index 0 to 2 (excluding 2), resulting in [“Apple”, “Banana”].

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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”}

A

B) {“Apple”, “Banana”, “Coconut”, “Dragon Fruit”}

Explanation: The add method adds “Dragon Fruit” to the set fruits, resulting in {“Apple”, “Banana”, “Coconut”, “Dragon Fruit”}.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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”}

A

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”}.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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”]

A

C) [“Apple”, “Coconut”]

Explanation: The code slices the list fruits with a step of 2, resulting in [“Apple”, “Coconut”].

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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”]

A

C) [“Coconut”, “Banana”, “Apple”]

Explanation: The code reverses the list fruits, resulting in [“Coconut”, “Banana”, “Apple”].

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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”}

A

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”}.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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

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”}.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

A) Apple, Banana, Lime

Explanation: The code iterates over the keys of the dictionary colors, printing each key: Apple, Banana, Lime.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

A

B) Red, Yellow, Green

Explanation: The code iterates over the values of the dictionary colors, printing each value: Red, Yellow, Green.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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

A) Apple, Banana, Lime

Explanation: The code iterates over the keys of the dictionary colors, printing each key: Apple, Banana, Lime.

17
Q

What will be the output of the following code?

fruits = ("Apple", "Banana", "Coconut")
print(fruits[1])

A) Apple
B) Banana
C) Coconut
D) IndexError

A

B) Banana

The code accesses the element at index 1 of the tuple fruits, which is “Banana”.

18
Q

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

A) (“Apple”, “Banana”)

The code slices the tuple fruits from index 0 to 2 (excluding 2), resulting in (“Apple”, “Banana”).

19
Q

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”)

A

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.

20
Q

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

A

B) Yellow

Explanation: The code accesses the value associated with the key “Banana” in the dictionary colors, which is “Yellow”.

21
Q

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

A

B) KeyError

Explanation: Attempting to access a key that does not exist in the dictionary colors will raise a KeyError.