Foundations-of-Computer-Science practice question 4 of 15

What will the expression fam[3:6] return?

Full set: 71 questions
Answer options
A:

A. A list with elements at index 4, 5, and 6

B:

B. A list with elements at index 3, 4, 5, and 6

C:

C. A list with elements at index 3, 4, and 5

Correct answer
D:

D. A list with elements at index 6

Correct Answer:
C. C. A list with elements at index 3, 4, and 5
Explanation

The expression fam[3:6] demonstrates list slicing in Python. The syntax [start:stop] extracts a sub-list. The start index, 3, is inclusive, meaning the element at index 3 is the first one included. The stop index, 6, is exclusive, meaning the slicing stops just before index 6. Therefore, the expression returns a new list containing the elements from fam at indices 3, 4, and 5.

Why Incorrect

A. This is incorrect because the starting index in a slice is inclusive. The element at index 3 would be included.
B. This is incorrect because the stopping index in a slice is exclusive. The element at index 6 would not be included.
D. This is incorrect because a slice with a start and stop index returns a range of elements, not a single element.

References

1. Python Software Foundation. (2023). The Python Tutorial, 3.1.3. Lists. Python 3.11.4 documentation. Retrieved from https://docs.python.org/3/tutorial/introduction.html#lists

2. DeNero, J. (n.d.). Composing Programs, 2.4.2 Slicing. University of California, Berkeley. Based on the textbook "Structure and Interpretation of Computer Programs." Retrieved from http://composingprograms.com/pages/24-sequences.html#slicing

Scroll to Top