Test Series - python

Test Number 38/108

Q: 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]
Solution: Execute in the shell to verify.
Q: 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]
Solution: Execute in the shell to verify.
Q: 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]
Solution: pop() removes the element at the position specified in the parameter.
Q: 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]
Solution: pop() by default will remove the last element.
Q: 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”
Solution: split() function returns the elements in a list.
Q: 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
Solution: First time the highest number is encountered is at index 1.
Q: 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
Solution: Execute in the shell to verify.
Q: 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]
Solution: Lists should be copied by executing [:] operation.
Q: 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]
Solution: Execute in the shell to verify.

You Have Score    /9