Python Questions and Answers – Lists – 3

Python Programming Questions & Answers focuses on “Lists”.

Q 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]

Show Answer Answer:-D. [3, 1, 25, 5, 20, 5, 4, 3]

Q 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]

Show Answer Answer:-A. [3, 4, 5, 20, 5, 25, 1, 3, 34, 5]

Q 3. Suppose list Example 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

Show Answer Answer:-C. [3, 5, 20, 5, 25, 1, 3]
Explanation pop() removes the element at the position specified in the parameter.

Q 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]

Show Answer Answer:-A. [3, 4, 5, 20, 5, 25, 1]
Explanation pop() by default will remove the last element.

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

>>>"Welcome to Python".split()

A. [“Welcome”, “to”, “Python”]
B. (“Welcome”, “to”, “Python”)
C. {“Welcome”, “to”, “Python”}
D. “Welcome”, “to”, “Python”

Show Answer Answer:-A. [“Welcome”, “to”, “Python”]
Explanation split() function returns the elements in a list.

Q 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’]

Show Answer Answer:-A. [‘a’, ‘b’, ‘c’, ‘d’]
Explanation Execute in the shell to verify.

Q 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

Show Answer Answer:-A. 1
Explanation First time the highest number is encountered is at index 1.

Q 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

Show Answer Answer:-C. 2 3 4 5 6 6
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”.

Q 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. [1, 4]
C. [4, 3]
D. [1, 3, 4]

Show Answer Answer:-C. [4, 3]
Explanation Lists should be copied by executing [:] operation.

Q 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. [1, 2, 3]
D. [44, 2, 3]

Show Answer Answer:-D. [44, 2, 3]
Explanation Execute in the shell to verify.

Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page