Python Questions and Answers – Lists – 3

This set of Python Programming Questions & Answers focuses on “Lists”.

1. Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after list1.reverse()?
a) [3, 4, 5, 20, 5, 25, 1, 3]
b) [1, 3, 3, 4, 5, 5, 20, 25]
c) [25, 20, 5, 5, 4, 3, 3, 1]
d) [3, 1, 25, 5, 20, 5, 4, 3]
View Answer

Answer: d
Explanation: Execute in the shell to verify.

2. Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listExample.extend([34, 5])?
a) [3, 4, 5, 20, 5, 25, 1, 3, 34, 5]
b) [1, 3, 3, 4, 5, 5, 20, 25, 34, 5]
c) [25, 20, 5, 5, 4, 3, 3, 1, 34, 5]
d) [1, 3, 4, 5, 20, 5, 25, 3, 34, 5]
View Answer

Answer: a
Explanation: Execute in the shell to verify.

3. Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listExample.pop(1)?
a) [3, 4, 5, 20, 5, 25, 1, 3]
b) [1, 3, 3, 4, 5, 5, 20, 25]
c) [3, 5, 20, 5, 25, 1, 3]
d) [1, 3, 4, 5, 20, 5, 25]
View Answer

Answer: c
Explanation: pop() removes the element at the position specified in the parameter.
advertisement
advertisement

4. Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listExample.pop()?
a) [3, 4, 5, 20, 5, 25, 1]
b) [1, 3, 3, 4, 5, 5, 20, 25]
c) [3, 5, 20, 5, 25, 1, 3]
d) [1, 3, 4, 5, 20, 5, 25]
View Answer

Answer: a
Explanation: pop() by default will remove the last element.

5. What will be the output of the following Python code?

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
>>>"Welcome to Python".split()

a) [“Welcome”, “to”, “Python”]
b) (“Welcome”, “to”, “Python”)
c) {“Welcome”, “to”, “Python”}
d) “Welcome”, “to”, “Python”
View Answer

Answer: a
Explanation: split() function returns the elements in a list.
advertisement

6. What will be the output of the following Python code?

>>>list("a#b#c#d".split('#'))

a) [‘a’, ‘b’, ‘c’, ‘d’]
b) [‘a b c d’]
c) [‘a#b#c#d’]
d) [‘abcd’]
View Answer

Answer: a
Explanation: Execute in the shell to verify.
advertisement

7. What will be the output of the following Python code?

myList = [1, 5, 5, 5, 5, 1]
max = myList[0]
indexOfMax = 0
for i in range(1, len(myList)):
    if myList[i] > max:
        max = myList[i]
        indexOfMax = i
 
>>>print(indexOfMax)

a) 1
b) 2
c) 3
d) 4
View Answer

Answer: a
Explanation: First time the highest number is encountered is at index 1.

8. What will be the output of the following Python code?

myList = [1, 2, 3, 4, 5, 6]
for i in range(1, 6):
    myList[i - 1] = myList[i]
 
for i in range(0, 6): 
    print(myList[i], end = " ")

a) 2 3 4 5 6 1
b) 6 1 2 3 4 5
c) 2 3 4 5 6 6
d) 1 1 2 3 4 5
View Answer

Answer: c
Explanation: The code shifts all elements of a list by one position towards the beginning, except the first element which is replaced by the second. The last element is duplicated since there is no next element to assign its value to. The output is “2 3 4 5 6 6”.

9. What will be the output of the following Python code?

>>>list1 = [1, 3]
>>>list2 = list1
>>>list1[0] = 4
>>>print(list2)

a) [1, 3]
b) [4, 3]
c) [1, 4]
d) [1, 3, 4]
View Answer

Answer: b
Explanation: Lists should be copied by executing [:] operation.

10. What will be the output of the following Python code?

def f(values):
    values[0] = 44
 
v = [1, 2, 3]
f(v)
print(v)

a) [1, 44]
b) [1, 2, 3, 44]
c) [44, 2, 3]
d) [1, 2, 3]
View Answer

Answer: c
Explanation: Execute in the shell to verify.

Sanfoundry Global Education & Learning Series – Python.

To practice all programming questions on Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.