Free PCAP Data Structure Questions and Answers — Questions and Answers
Question 1: Which data structure is used to store a sequence of items that are ordered and changeable in Python?
- List (Correct answer)
- Tuple
- Set
- Dictionary
Correct answer: List
Lists in Python are ordered, mutable sequences used to store collections of items.
Question 2: What is the correct syntax to create a tuple in Python?
- tuple = (1, 2, 3) (Correct answer)
- tuple = [1, 2, 3]
- tuple = {1, 2, 3}
- tuple = <1, 2, 3>
Correct answer: tuple = (1, 2, 3)
Tuples in Python are created using parentheses.
Question 3: Which method would you use to add an item to the end of a list in Python?
- add()
- append() (Correct answer)
- insert()
- extend()
Correct answer: append()
The append() method adds an item to the end of a list.
Question 4: How do you access the value associated with a specific key in a dictionary?
- dict[key] (Correct answer)
- dict. key
- dict. get(key)
- dict{key}
Correct answer: dict[key]
Values in a dictionary are accessed using square brackets with the key inside.
Question 5: Which data structure does not allow duplicate elements?
- Dictionary
- Set (Correct answer)
- List
- Tuple
Correct answer: Set
Sets in Python do not allow duplicate elements.
Question 6: What will be the output of the following code?br my_dict = {""name"": ""John"", ""age"": 30}br print(my_dict.get(""address"", ""Not found""))
- address
- Not found (Correct answer)
- None
- Error
Correct answer: Not found
The get method returns the specified default value if the key is not found in the dictionary.
Question 7: Which of the following is true about tuples?
- Both B and C (Correct answer)
- They are mutable
- They are immutable
- They can have duplicate elements
Correct answer: Both B and C
Tuples are immutable and can have duplicate elements.
Question 8: How can you remove the last item from a list?
- list.drop()
- list.pop() (Correct answer)
- list.remove()
- list.delete()
Correct answer: list.pop()
The pop() method removes and returns the last item from a list.
Question 9: What will be the result of the following code?br my_set = {1, 2, 3}br my_set.add(2)br print(my_set)
- {1, 2, 3} (Correct answer)
- {1, 2, 3, 2}
- {1, 3}
- {2, 3}
Correct answer: {1, 2, 3}
Sets do not allow duplicate elements, so adding an existing element does not change the set.
Question 10: Which method would you use to combine two dictionaries in Python 3.9 or later?
- dict1 | dict2 (Correct answer)
- dict1.merge(dict2)
- dict1.update(dict2)
- dict1 + dict2
Correct answer: dict1 | dict2
The | operator is used to merge two dictionaries in Python 3.9 and later.
Question 11: How do you check if a key exists in a dictionary?
- if dict.contains(key):
- if key in dict: (Correct answer)
- if dict.has_key(key):
- if key exists in dict:
Correct answer: if key in dict:
The in keyword is used to check if a key exists in a dictionary
Which data structure is used to store a sequence of items that are ordered and changeable in Python?