PCEP Data Structures 2 — Questions and Answers
Question 1: What is the result of `[1, 2, 3] + [4, 5]`?
- [1, 2, 3, 4, 5] (Correct answer)
- [5, 7, 8]
- Error
- [[1, 2, 3], [4, 5]]
Correct answer: [1, 2, 3, 4, 5]
The `+` operator concatenates two lists into a single list.
Question 2: Which method removes and returns the last element of a list?
- remove()
- pop() (Correct answer)
- del()
- discard()
Correct answer: pop()
`pop()` without arguments removes and returns the last element of the list.
Question 3: What does `len({'a': 1, 'b': 2, 'c': 3})` return?
- 6
- 3 (Correct answer)
- 2
- Error
Correct answer: 3
`len()` on a dictionary returns the number of key-value pairs.
Question 4: What is the output of `(1, 2, 3)[1:3]`?
- (1, 2)
- (2, 3) (Correct answer)
- (1, 2, 3)
- (3,)
Correct answer: (2, 3)
Slicing `[1:3]` on a tuple returns elements at index 1 and 2.
Question 5: Which of the following creates an empty set?
- {}
- set() (Correct answer)
- []
- ()
Correct answer: set()
`{}` creates an empty dict, so `set()` is the only way to create an empty set.
Question 6: What happens when you try to change a value in a tuple?
- The value is updated
- A TypeError is raised (Correct answer)
- A ValueError is raised
- The tuple is converted to a list
Correct answer: A TypeError is raised
Tuples are immutable; attempting to assign to an index raises a TypeError.
Question 7: What is the result of `list(range(0, 10, 3))`?
- [0, 3, 6, 9] (Correct answer)
- [0, 3, 6, 9, 12]
- [3, 6, 9]
- [0, 3, 6]
Correct answer: [0, 3, 6, 9]
`range(0, 10, 3)` generates 0, 3, 6, 9 — stopping before 10.
What is the result of `[1, 2, 3] + [4, 5]`?