110
88k

Python Programming Solved MCQs

These multiple-choice questions (MCQs) are designed to enhance your knowledge and understanding in the following areas: Computer Science Engineering (CSE) , Information Technology Engineering (IT) , Bachelor of Science in Information Technology FY (BSc IT) , Bachelor of Science in Information Technology SY (BSc IT) , Bachelor of Science in Information Technology TY (BSc IT) .

1.

What is the output of the following code : print 9//2

A. 4.5
B. 4.0
C. 4
D. Error
Answer» C. 4
2.

Which function overloads the >> operator?

A. more()
B. gt()
C. ge()
D. rshift()
Answer» D. rshift()
3.

What is the output of the following program :
i = 0
while i < 3:
print i
print i+1

A. 0 2 1 3 2 4
B. 0 1 2 3 4 5
C. 0 1 1 2 2 3
D. 1 0 2 4 3 5
Answer» C. 0 1 1 2 2 3
4.

Which module in Python supports regular expressions?

A. re
B. regex
C. pyregex
D. None of the above
Answer» A. re
5.

What is the output of the following program : print 0.1 + 0.2 == 0.3

A. True
B. False
C. Machine dependent
D. Error
Answer» B. False
Explanation: Neither of 0.1, 0.2 and 0.3 can be represented accurately in binary. The
round off errors from 0.1 and 0.2 accumulate and hence there is a difference of 5.5511e-17
between (0.1 + 0.2) and 0.3.
6.

Which of these is not a core data type?

A. Lists
B. Dictionary
C. Tuples
D. Class
Answer» D. Class
7.

What data type is the object below? L = [1, 23, „hello‟, 1]

A. List
B. Dictionary
C. Tuple
D. Array
Answer» A. List
8.

What is the output of the following program :
def myfunc(a):
a = a + 2
a = a * 2
return a
print myfunc(2)

A. 8
B. 16
C. Indentation Error
D. Runtime Error
Answer» C. Indentation Error
9.

What is the output of the expression : 3*1**3

A. 27
B. 9
C. 3
D. 1
Answer» C. 3
10.

What is the output of the following program : print '{0:.2}'.format(1.0 / 3)

A. 0.333333
B. 0.33
C. 0.333333:-2
D. Error
Answer» B. 0.33
11.

What is the output of the following program : print '{0:-2%}'.format(1.0 / 3)

A. 0.33
B. 0.33%
C. 33.33%
D. 33%
Answer» C. 33.33%
12.

What is the output of the following program :
i = 0
while i < 3:
print i
i += 1
else:
print 0

A. 0 1 2 3 0
B. 0 1 2 0
C. 0 1 2
D. Error
Answer» B. 0 1 2 0
13.

What is the output of the following program :
i = 0
while i < 5:
print(i)
i += 1
if i == 3:
break
else:
print(0)

A. 0 1 2 0
B. 0 1 2
C. Error
D. None of the above
Answer» B. 0 1 2
14.

What is the output of the following program : print 'cd'.partition('cd')

A. („cd‟)
B. (”)
C. („cd‟, ”, ”)
D. (”, „cd‟, ”)
Answer» D. (”, „cd‟, ”)
15.

What is the output of the following program : print 'abcefd'.replace('cd', '12')

A. ab1ef2
B. abcefd
C. ab1efd
D. ab12ed2
Answer» B. abcefd
16.

What will be displayed by the following code?
def f(value, values):
v = 1
values[0] = 44
t = 3
v = [1, 2, 3]
f(t, v)
print(t, v[0])

A. 1 1
B. 1 44
C. 3 1
D. 3 44
Answer» D. 3 44
17.

Predict the output of following python programs
dictionary1 = {'Google' : 1,
'Facebook' : 2,
'Microsoft' : 3
}
dictionary2 = {'GFG' : 1,
'Microsoft' : 2,
'Youtube' : 3
}
dictionary1.update(dictionary2);
for key, values in dictionary1.items():
print(key, values)

A. Compilation error
B. Runtime error
C. („Google‟, 1) („Facebook‟, 2) („Youtube‟, 3) („Microsoft‟, 2) („GFG‟, 1)
D. None of these
Answer» C. („Google‟, 1) („Facebook‟, 2) („Youtube‟, 3) („Microsoft‟, 2) („GFG‟, 1)
18.

What is the output of the following program?
dictionary1 = {'GFG' : 1,
'Google' : 2,
'GFG' : 3
}
print(dictionary1['GFG']);

A. Compilation error due to duplicate keys
B. Runtime time error due to duplicate keys
C. 3
D. 1
Answer» C. 3
19.

What is the output of the following program?
temp = dict()
temp['key1'] = {'key1' : 44, 'key2' : 566}
temp['key2'] = [1, 2, 3, 4]
for (key, values) in temp.items():
print(values, end = "")

A. Compilation error
B. {„key1‟: 44, „key2‟: 566}[1, 2, 3, 4]
C. Runtime error
D. None of the above
Answer» B. {„key1‟: 44, „key2‟: 566}[1, 2, 3, 4]
20.

What is the output of the following program?
data = [2, 3, 9]
temp = [[x for x in[data]] for x in range(3)]
print (temp)

A. [[[2, 3, 9]], [[2, 3, 9]], [[2, 3, 9]]]
B. [[2, 3, 9], [2, 3, 9], [2, 3, 9]]
C. [[[2, 3, 9]], [[2, 3, 9]]]
D. None of these
Answer» A. [[[2, 3, 9]], [[2, 3, 9]], [[2, 3, 9]]]
21.

What is the output of the following program?
data = [x for x in range(5)]
temp = [x for x in range(7) if x in data and x%2==0]
print(temp)

A. [0, 2, 4, 6]
B. [0, 2, 4]
C. [0, 1, 2, 3, 4, 5]
D. Runtime error
Answer» B. [0, 2, 4]
22.

What is the output of the following program?
L1 = [1, 2, 3, 4]
L2 = L1
L3 = L1.copy()
L4 = list(L1)
L1[0] = [5]
print(L1, L2, L3, L4)

A. [5, 2, 3, 4] [5, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4]
B. [[5], 2, 3, 4] [[5], 2, 3, 4] [[5], 2, 3, 4] [1, 2, 3, 4]
C. [5, 2, 3, 4] [5, 2, 3, 4] [5, 2, 3, 4] [1, 2, 3, 4]
D. [[5], 2, 3, 4] [[5], 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4]
Answer» D. [[5], 2, 3, 4] [[5], 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4]
23.

What is the output of the following program?
import sys
L1 = tuple()
print(sys.getsizeof(L1), end = " ")
L1 = (1, 2)
print(sys.getsizeof(L1), end = " ")
L1 = (1, 3, (4, 5))
print(sys.getsizeof(L1), end = " ")
L1 = (1, 2, 3, 4, 5, [3, 4], 'p', '8', 9.777, (1, 3))
print(sys.getsizeof(L1))

A. 0 2 3 10
B. 32 34 35 42
C. 48 64 72 128
D. 48 144 192 480
Answer» C. 48 64 72 128
24.

What is the output of the following program?
T = (1, 2, 3, 4, 5, 6, 7, 8)
print(T[T.index(5)], end = " ")
print(T[T[T[6]-3]-6])

A. 4 0
B. 5 8
C. 5 IndexError
D. 4 1
Answer» B. 5 8
25.

What is the output of the following program?
L = [1, 3, 5, 7, 9]
print(L.pop(-3), end = ' ')
print(L.remove(L[0]), end = ' ')
print(L)

A. 5 None [3, 7, 9]
B. 5 1 [3, 7, 9]
C. 5 1 [3, 7, 9]
D. 5 None [1, 3, 7, 9]
Answer» A. 5 None [3, 7, 9]
26.

What is the output of the following program?
def REVERSE(L):
L.reverse()
return(L)
def YKNJS(L):
List = list()
List.extend(REVERSE(L))
print(List)
L = [1, 3.1, 5.31, 7.531]
YKNJS(L)

A. [1, 3.1, 5.31, 7.531]
B. [7.531, 5.31, 3.1, 1]
C. IndexError
D. AttributeError: „NoneType‟ object has no attribute „REVERSE‟
Answer» B. [7.531, 5.31, 3.1, 1]
27.

What is the output of the following program?
from math import sqrt
L1 = [x**2 for x in range(10)].pop()
L1 + = 19
print(sqrt(L1), end = " ")
L1 = [x**2 for x in reversed(range(10))].pop()
L1 + = 16
print(int(sqrt(L1)))

A. 10.0 4.0
B. 4.3588 4
C. 10 .0 4
D. 10.0 0
Answer» C. 10 .0 4
28.

What is the output of the following program?
D = dict()
for x in enumerate(range(2)):
D[x[0]] = x[1]
D[x[1]+7] = x[0]
print(D)

A. KeyError
B. {0: 1, 7: 0, 1: 1, 8: 0}
C. {0: 0, 7: 0, 1: 1, 8: 1}
D. {1: 1, 7: 2, 0: 1, 8: 1}
Answer» C. {0: 0, 7: 0, 1: 1, 8: 1}
29.

What is the output of the following program?
D = {1 : 1, 2 : '2', '1' : 1, '2' : 3}
D['1'] = 2
print(D[D[D[str(D[1])]]])

A. 2
B. 3
C. „2‟
D. KeyError
Answer» B. 3
30.

What is the output of the following program?
D = dict()
for i in range (3):
for j in range(2):
D[i] = j
print(D)

A. {0: 0, 1: 0, 2: 0}
B. {0: 1, 1: 1, 2: 1}
C. {0: 0, 1: 0, 2: 0, 0: 1, 1: 1, 2: 1}
D. TypeError: Immutable object
Answer» B. {0: 1, 1: 1, 2: 1}
31.

What is the output of the following program? from math import *
a = 2.13
b = 3.7777
c = -3.12
print(int(a), floor(b), ceil(c), fabs(c))

A. 2 3 -4 3
B. 2 3 -3 3.12
C. 2 4 -3 3
D. 2 3 -4 3.12
Answer» B. 2 3 -3 3.12
32.

What is the output of the following program?

A. [0, „2‟, „3‟, „4‟, „5‟, 0]
B. [„6‟, „2‟, „3‟, „5‟, „5‟, „6‟]
C. [„0‟, „2‟, „3‟, „5‟, „5‟, „0‟]
D. [0, „2‟, „3‟, „5‟, „5‟, 0]
Answer» D. [0, „2‟, „3‟, „5‟, „5‟, 0]
33.

What is the output of the following program?
import string
import string
Line1 = "And Then There Were None"
Line2 = "Famous In Love"
Line3 = "Famous Were The Kol And Klaus"
Line4 = Line1 + Line2 + Line3
print(string.find(Line1, 'Were'), string.count((Line4), 'And'))

A. True 1
B. 15 2
C. (15, 2)
D. True 2
Answer» C. (15, 2)
34.

What is the output of the following program?
line = "What will have so will"
L = line.split('a')
for i in L:
print(i, end=' ')

A. [„What‟, „will‟, „have‟, „so‟, „will‟]
B. Wh t will h ve so will
C. What will have so will
D. [„Wh‟, „t will h‟, „ve so will‟]
Answer» B. Wh t will h ve so will
35.

What is the type of each element in sys.argv?

A. set
B. list
C. tuple
D. string
Answer» D. string
36.

What is the length of sys.argv?

A. number of arguments
B. number of arguments + 1
C. number of arguments – 1
D. none of the mentioned
Answer» B. number of arguments + 1
37.

What is the output of the following code?
def foo(k):
k[0] = 1
q = [0]
foo(q)
print(q)

A. [0].
B. [1].
C. [1, 0].
D. [0, 1].
Answer» B. [1].
38.

What is the output of the following code?
def foo(fname, val):
print(fname(val))
foo(max, [1, 2, 3])
foo(min, [1, 2, 3])

A. 3 1
B. 1 3
C. error
D. none of the mentioned
Answer» A. 3 1
39.

What is the output of the following?
elements = [0, 1, 2]
def incr(x):
return x+1
print(list(map(elements, incr)))

A. [1, 2, 3].
B. [0, 1, 2].
C. error
D. none of the mentioned
Answer» C. error
40.

What is the output of the following?
elements = [0, 1, 2]
def incr(x):
return x+1
print(list(map(incr, elements)))

A. [1, 2, 3].
B. [0, 1, 2].
C. error
D. none of the mentioned
Answer» A. [1, 2, 3].
41.

What is the output of the following?
def to_upper(k):
return k.upper()
x = ['ab', 'cd']
print(list(map(to_upper, x)))

A. [„AB‟, „CD‟].
B. [„ab‟, „cd‟].
C. none of the mentioned
D. error
Answer» A. [„AB‟, „CD‟].
42.

What is the output of the following?
x = ['ab', 'cd']
print(len(list(map(list, x))))

A. 2
B. 4
C. error
D. none of the mentioned
Answer» A. 2
43.

Program code making use of a given module is called a ______ of the module.

A. Client
B. Docstring
C. Interface
D. Modularity
Answer» A. Client
44.

What is the output of the following piece of code?
#mod1
def change(a):
b=[x*2 for x in a]
print(b)
#mod2
def change(a):
b=[x*x for x in a]
print(b)
from mod1 import change
from mod2 import change
#main
s=[1,2,3]
change(s)

A. [2,4,6].
B. [1,4,9].
C. [2,4,6].
D. There is a name clash
Answer» D. There is a name clash
45.

What is the output of the following program? tday=datetime.date.today() print(tday.month())

A. August
B. Aug
C. 08
D. 8
Answer» D. 8
46.

Which of the following formatting options can be used in order to add „n‟ blank spaces after a given string „S‟?

A. print(“-ns”%S)
B. print(“-ns”%S)
C. print(“%ns”%S)
D. print(“%-ns”%S)
Answer» D. print(“%-ns”%S)
47.

What is the output of the following program?
f = None
for i in range (5):
with open("data.txt", "w") as f:
if i > 2:
break
print(f.closed)

A. True
B. False
C. None
D. Error
Answer» A. True
48.

To read the entire remaining contents of the file as a string from a file object infile, we use

A. infile.read(2)
B. infile.read()
C. infile.readline()
D. infile.readlines()
Answer» B. infile.read()
49.

Suppose t = (1, 2, 4, 3), which of the following is incorrect?

A. print(t[3])
B. t[3] = 45
C. print(max(t))
D. print(len(t))
Answer» B. t[3] = 45

Done Studing? Take A Test.

Great job completing your study session! Now it's time to put your knowledge to the test. Challenge yourself, see how much you've learned, and identify areas for improvement. Don’t worry, this is all part of the journey to mastery. Ready for the next step? Take a quiz to solidify what you've just studied.