Python List Methods Quiz
1. What is the output? numbers = [10, 20, 30] numbers.append([40, 50]) print(numbers)
A. [10, 20, [40, 50], 30]
B. [40, 50, 10, 20, 30]
C. [10, 20, 30, [40, 50]]
D. [10, 20, 30, 40, 50]
2. What is the output? x = [1, 2, 3] x.insert(1, 99) print(x)
A. [1, 99, 2, 3]
B. [1, 2, 3, 99]
C. [1, 2, 99, 3]
D. [99, 1, 2, 3]
3. What is the output? x = [5, 10, 5, 20] x.remove(5) print(x)
A. [10, 5, 20]
B. [5, 10, 5, 20]
C. [10, 20]
D. [5, 10, 20]
4. What is the output? x = [10, 20, 30] y = x.pop() print(x, y)
A. [10, 20, 30] None
B. [10, 20] 30
C. [20, 30] 10
D. [10, 30] 20
5. What is the output? x = [30, 10, 20] x.sort(reverse=True) print(x)
A. [30, 20, 10]
B. [20, 10, 30]
C. [30, 10, 20]
D. [10, 20, 30]
1 comment:
What is the output?
numbers = \[10, 20, 30\] numbers.append(\[40, 50\]) print(numbers)
append adds its argument as a single element, producing a nested list.
What is the output?
x = \[1, 2, 3\] x.insert(1, 99) print(x)
insert(1, 99) places 99 at index 1 and shifts later elements to the right.
What is the output?
x = \[5, 10, 5, 20\] x.remove(5) print(x)
remove deletes only the first matching value, leaving the second 5.
What is the output?
x = \[10, 20, 30\] y = x.pop() print(x, y)
pop() without an index removes and returns the last element, so x becomes \[10, 20\] and y receives 30.
What is the output?
x = \[30, 10, 20\] x.sort(reverse=True) print(x)
reverse=True makes sort arrange the values in descending order.
Post a Comment