0% found this document useful (0 votes)
8 views12 pages

P 2

The document contains a series of multiple-choice questions (MCQs) related to Python programming. Each question tests knowledge on various aspects of Python, including syntax, data types, functions, and modules. The document includes 47 questions with answer options labeled A through D.

Uploaded by

ppl.gecbidar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views12 pages

P 2

The document contains a series of multiple-choice questions (MCQs) related to Python programming. Each question tests knowledge on various aspects of Python, including syntax, data types, functions, and modules. The document includes 47 questions with answer options labeled A through D.

Uploaded by

ppl.gecbidar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

MCQs

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


A. 4.5
B. 4.0
C. 4
D. Error
2. Which function overloads the >> operator?
A. more()
B. gt()
C. ge()
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
4. Which module in Python supports regular expressions?
A. re
B. regex
C. pyregex
D. None of the above
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
.
6. Which of these is not a core data type?
A. Lists
B. Dictionary
C. Tuples
D. Class
7. What data type is the object below? L = [1, 23, „hello?, 1]
A. List
B. Dictionary
C. Tuple
D. Array
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
9. What is the output of the expression : 3*1**3
A. 27
B. 9
C. 3
D. 1
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
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%
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
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
14. What is the output of the following program :
print 'cd'.partition('cd')
A. („cd?)
B. (”)
C. („cd?, ”, ”)
D. (”, „cd?, ”)
15. What is the output of the following program :
print 'abcefd'.replace('cd', '12')
A. ab1ef2
B. abcefd
C. ab1efd
D. ab12ed2
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
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
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
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
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
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
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]
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
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
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]

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?
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
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}
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
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
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
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]
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
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?]
35. What is the type of each element in sys.argv?
A. set
B. list
C. tuple
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
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
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
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
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
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
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
43. Program code making use of a given module is called a ______ of the
module.
A. Client
B. Docstring
C. Interface
D. Modularity
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
45. What is the output of the following program?
tday=datetime.date.today()
print(tday.month())
A. August
B. Aug
C. 08
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)
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
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()
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))

Find Errors in Python Programs and solve


them

1.Example:

if x = 10:
print("x is equal to 10")

2.Example:
for i in range(10):
print(i)

3. Example:
my_variable = 5
print(my_vairable)

4.Example:

x = "5"
y = 10
result = x + y
5.Example:
my_list = [1, 2, 3, 4]
print(my_list[5])

6.Example:

my_dict = {"name": "John", "age": 25}


print(my_dict["gender"])
In this example, we are trying to access the value for the key
"gender", which does not exist in the dictionary.

7. Example:

my_list = [1, 2, 3, 4]
my_list.append(5)
my_list.add(6)
8. example
x= “10”
y= 5
Z=x+y
print(z)
9.Example
my_string = "Hello, world!"
my_string.reverse()

10. def calculate_factorial(n):

result = 1
for i in range(1, n):
result = result * i
return result

print(calculate_factorial(5))

You might also like