Practice objective questions
for quick revision and examination
preparation. Try answering each question
before revealing the answer.
π Python Programming
π Unit 2
π― MCQs
Python Programming MCQs - Unit 2
Python Programming MCQs - Unit 2
1
Which of the following is invalid identifier declaration?
A_a = 1
B__a = 1
C__str__ = 1
Dnone of the mentioned
Correct Answernone of the mentioned
2
Which of the following is an invalid statement?
Aabc = 1,000,000
Ba b c = 1000 2000 3000
Ca,b,c = 1000, 2000, 3000
Da_b_c = 1,000,000
Correct Answera b c = 1000 2000 3000
3
What error occurs when you execute?>>>apple = mango
ASyntaxError
BNameError
CValueError
DTypeError
Correct AnswerNameError
4
What is the output of the following?x = ['ab', 'cd']
for i in x:
x.append(i.upper())
print(x)
A[βABβ, βCDβ].
B[βabβ, βcdβ, βABβ, βCDβ].
C[βabβ, βcdβ].
Dnone of the mentioned
Correct Answernone of the mentioned
5
True = False
while True:
print(True)
break
ATrue
BFalse
CError
Dnone of the mentioned
Correct AnswerError
6
What is the output of the following?for i in range(10):
if i == 5:
break
else:
print(i)
else:
print("Here")
A0 1 2 3 4 Here
B0 1 2 3 4 5 Here
C0 1 2 3 4
D1 2 3 4 5
Correct Answer0 1 2 3 4
7
What is the output of the following?print("xyyzxyzxzxyy".count('xyy', -10, -1))
A2
B0
C1
DError
Correct Answer0
8
What is the output of the following?print("ab\tcd\tef".expandtabs(1))
Aab cd ef
Babcdef
Cab cd ef
DError
Correct Answerab cd ef
9
What is the output of the following?print('ab,12'.isalnum())
ATrue
BFalse
CError
Dnone of the mentioned
Correct AnswerFalse
10
What is the output when following code is executed ?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 = " ")
A2 3 4 5 6 1
B6 1 2 3 4 5
C2 3 4 5 6 6
D1 1 2 3 4 5
Correct Answer2 3 4 5 6 6
11
values = [[3, 4, 5, 1], [33, 6, 1, 2]]
v = values[0][0]
for row in range(0, len(values)):
for column in range(0, len(values[row])):
if v < values[row][column]:
v = values[row][column]
print(v)
A3
B5
C6
D33
Correct Answer33
12
What will be the output?>>>my_tuple = (1, 2, 3, 4)
>>>my_tuple.append( (5, 6, 7) )
>>>print(len(my_tuple))
A7
B4
C3
DError
Correct AnswerError
13
Is the following piece of code valid?>>> a=(1,2,3,4)
>>> del a
AYes, the entire tuple is deleted
BYes, first element in the tuple is deleted
CNo because tuple is immutable
DNo, invalid syntax for del method
Correct AnswerYes, the entire tuple is deleted
14
What is the output for the following piece of code?>>> a={1,2,3}
>>> b=a
>>> b.remove(3)
>>> print(a)
A{1,2,3}
B{1,2}
CError, copying of sets isnβt allowed
DError, invalid syntax for remove
Correct Answer{1,2}
15
What is the output of the following code?>>> a={1,2,3}
>>> b=a.add(4)
>>>print(b)
ANone
B{1,2,3,4}
C{1,2,3}
DError
Correct AnswerNone
16
The ____________ function removes the first element of a set and the last element of a list.
Aremove()
Bdiscard()
Cclear()
Dpop()
Correct Answerpop()
17
The difference between the functions discard and remove is that:
ADiscard removes the last element of the set whereas remove removes the first element of the set
BDiscard throws an error if the specified element is not present in the set whereas remove does not throw an error in case of absence of the specified element
CRemove removes the last element of the set whereas discard removes the first element of the set
DRemove throws an error if the specified element is not present in the set whereas discard does not throw an error in case of absence of the specified element
Correct AnswerRemove throws an error if the specified element is not present in the set whereas discard does not throw an error in case of absence of the specified element
18
What is the output of the following code?a={}
a[2]=1
a[1]=[2,3,4]
print(a[1][1])
A[2,3,4]
B3
C2
D1
Correct Answer3
19
What is the output of the following piece of code?>>> a={'B':5,'A':9,'C':7}
>>> print(sorted(a))
A[βAβ,βBβ,βCβ].
B[βBβ,βCβ,βAβ].
C[5,7,9].
D[9,5,7].
Correct Answer[βAβ,βBβ,βCβ].
20
What is the output when following code is executed ?>>>names = ['Amir', 'Bear', 'Charlton', 'Daman']
>>>print(names[-1][-1])
AA
BDaman
Cn
DCharlton
Correct Answern
21
What is the output of the following?a = [0, 1, 2, 3]
for a[0] in a:
print(a[0],end=" ")
A0 0 0 0
B0 1 2 3
C0 1 2 2
D3 3 3 3
Correct Answer0 1 2 3
22
Suppose list1 is [2, 33, 222, 14, 25], What is list1[:-1] ?
A[2, 33, 222, 14].
BError
C25
D[25, 14, 222, 33, 2].
Correct Answer[2, 33, 222, 14].
23
What is the output when following code is executed ?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].
Correct Answer[44, 2, 3].
24
What will be the output?>>>t=(1,2,4,3)
>>>print(t[1:-1])
A(1,2)
B(2,3)
C(1,3)
D(2,4)
Correct Answer(2,4)
25
What is the output of the following piece of code?a={1:"A",2:"B",3:"C"}
print(a.get(1,4))