PCEP Data Structures 3 — Questions and Answers
Question 1: What does `'key' in {'key': 1, 'val': 2}` evaluate to?
- False
- True (Correct answer)
- 1
- Error
Correct answer: True
The `in` operator checks for key membership in a dictionary.
Question 2: Which list method inserts an element at a specific index?
- append()
- extend()
- insert() (Correct answer)
- add()
Correct answer: insert()
`insert(index, value)` places a new element before the given index.
Question 3: What is the value of `d` after `d = {}; d['x'] = 10; d['x'] += 5`?
- {'x': 10}
- {'x': 15} (Correct answer)
- {'x': 5}
- Error
Correct answer: {'x': 15}
The key `'x'` is first set to 10, then incremented by 5 to become 15.
Question 4: What does `sorted({3, 1, 2})` return?
- {1, 2, 3}
- [1, 2, 3] (Correct answer)
- (1, 2, 3)
- Error
Correct answer: [1, 2, 3]
`sorted()` always returns a new list, regardless of the input type.
Question 5: What is the output of `[0] * 4`?
- [0, 1, 2, 3]
- [4]
- [0, 0, 0, 0] (Correct answer)
- 0
Correct answer: [0, 0, 0, 0]
Multiplying a list by an integer repeats its elements that many times.
Question 6: Which dictionary method returns all key-value pairs as view objects?
- keys()
- values()
- items() (Correct answer)
- pairs()
Correct answer: items()
`dict.items()` returns a view of `(key, value)` tuple pairs.
Question 7: What is the result of `{1, 2, 3} & {2, 3, 4}`?
- {1, 2, 3, 4}
- {2, 3} (Correct answer)
- {1, 4}
- Error
Correct answer: {2, 3}
The `&` operator returns the intersection — elements common to both sets.
What does `'key' in {'key': 1, 'val': 2}` evaluate to?