PCEP-30-02 · Question #46
You develop a Python application for your company. A list named employees contains 200 employee names, the last five being company management. You need to slice the list to display all employees…
The correct answer is B. employees[0:-5]. Option B (employees[0:-5]) correctly slices from index 0 (the first employee) up to, but not including, the last 5 elements (management), returning all 195 non-management employees. However, this is a "choose two" question - C (employees[:-5]) is equally correct, since omitting…
Question
Options
- Aemployees[1:-5]
- Bemployees[0:-5]
- Cemployees[:-5]
- Demployees[0:-4]
- Eemployees[1:-4]
How the community answered
(34 responses)- A3% (1)
- B74% (25)
- C3% (1)
- D15% (5)
- E6% (2)
Explanation
Option B (employees[0:-5]) correctly slices from index 0 (the first employee) up to, but not including, the last 5 elements (management), returning all 195 non-management employees. However, this is a "choose two" question - C (employees[:-5]) is equally correct, since omitting the start index defaults to 0, making it functionally identical to B.
Why the others are wrong:
- A (
employees[1:-5]) starts at index1, accidentally skipping the very first employee. - D (
employees[0:-4]) stops 4 from the end instead of 5, so it includes one manager. - E (
employees[1:-4]) makes both mistakes: skips the first employee and includes one manager.
Memory tip: Think of negative indices as counting backwards from the end - -5 means "stop before the last 5." When you want the full front of a list, omitting the start ([:-5]) and using 0 as the start ([0:-5]) are interchangeable in Python, so both B and C are valid.
Community Discussion
No community discussion yet for this question.