python MCQ
python MCQ
UNIT-1
4 + 3 % 5
a) 7
b) 2
c) 4
d) 1
View Answer-a
10. Which of the following character is used to give single-line comments in Python?
a) //
b) #
c) !
d) /*
View Answer-b
i = 1
while True:
if i%3 == 0:
break
print(i)
i + = 1
a) 1 2 3
b) error
c) 1 2
d) none of the mentioned
View Answer-b
12. Which of the following functions can help us to find the version of python that we are
currently working on?
a) sys.version(1)
b) sys.version(0)
c) sys.version()
d) sys.version
View Answer-d
13. Python supports the creation of anonymous functions at runtime, using a construct
called __________
a) pi
b) anonymous
c) lambda
d) none of the mentioned
View Answer-c
15. What will be the output of the following Python code snippet if x=1?
x<<2
a) 4
b) 2
c) 1
d) 8
View Answer-a
2**(3**2)
(2**3)**2
2**3**2
a) any number of
b) 0
c) 1
d) 2
View Answer-a
min(max(False,-3,-4), 2,7)
a) -4
b) -3
c) 2
d) False
View Answer-d
25. Which of the following is not a core data type in Python programming?
a) Tuples
b) Lists
c) Class
d) Dictionary
View Answer-c
26. What will be the output of the following Python expression if x=56.236?
print("%.2f"%x)
a) 56.236
b) 56.23
c) 56.0000
d) 56.24
View Answer-d
len(["hello",2, 4, 6])
a) Error
b) 6
c) 4
d) 3
View Answer-c
x = 'abcd'
for i in x:
print(i.upper())
a)
b) a b c d
c) error
d)
View Answer-d
30. What is the order of namespaces in which Python looks for an identifier?
a) Python first searches the built-in namespace, then the global namespace and finally
the local namespace
b) Python first searches the built-in namespace, then the local namespace and finally
the global namespace
c) Python first searches the local namespace, then the global namespace and finally the
built-in namespace
d) Python first searches the global namespace, then the local namespace and finally the
built-in namespace
View Answer-b
UNIT-2
31. What will be the output of the following Python code snippet?
a) 4 3 2 1
b) error
c) 1 2 3 4
d) none of the mentioned
View Answer-a
1. >>>"a"+"bc"
a) bc
b) abc
c) a
d) bca
View Answer-b
33. Which function is called when the following Python program is executed?
f = foo()
format(f)
a) str()
b) format()
c) __str__()
d) __format__()
View Answer-c
34. Which one of the following is not a keyword in Python language?
a) pass
b) eval
c) assert
d) nonlocal
View Answer d
35. What will be the output of the following Python code?
1. class tester:
2. def __init__(self, id):
3. self.id = str(id)
4. id="224"
5.
6. >>>temp = tester(12)
7. >>>print(temp.id)
a) 12
b) 224
c) None
d) Error
View Answer-a
def foo(x):
x[0] = ['def']
x[1] = ['abc']
return id(x)
q = ['abc', 'def']
print(id(q) == foo(q))
a) Error
b) None
c) False
d) True
View Answer-d
37. Which module in the python standard library parses options received from the
command line?
a) getarg
b) getopt
c) main
d) os
View Answer-b
z=set('abc')
z.add('san')
z.update(set(['p', 'q']))
z
a) Abc. def
b) abc. def
c) Abc. Def
d) ABC. DEF
View Answer-a
41. Which of the following statements is used to create an empty set in Python?
a) ( )
b) [ ]
c) { }
d) set()
View Answer-d
a. 1st_string
b. my_string_1
c. _
d. foo
A) Exponential
B) Parentheses
C) Division
D) Multiplication
E) Subtraction
F) Addition
a. float(‘nan’)
b. float(‘inf’)
c. float(’12+34′)
d. float(’56’+’78’)
45. The value of the Python expression given below would be:
4+2**5//10
a. 77
b. 0
c. 3
d. 7
Answer: (d) 7
a. bool
b. float
c. int
d. None
47. What is the output of the Python code given below, if the date of the system is June
21st, 2017 (Wednesday)?
[] or {}
{} or []
a.
[] []
b.
[]
{}
c.
{}
{}
d.
{}
[]
Answer: (d)
{}
[]
48. The output of this Python code would be:
d. Error
a = [‘mn’, ‘op’]
for i in a:
i.upper()
print(a)
a. [None, None]
b. [‘MN’, ‘OP’]
c. [‘mn’, ‘op’]
print(“mno. PQR”.capitalize())
a. Mno. Pqr
b. Mno. pqr
c. MNO. PQR
d. mno. pqr
a. –
b. +
c. *
Answer: (a) –
a. shuffle(list1)
b. list1.shuffle()
c. random.shuffleList(list1)
d. random.shuffle(list1)
54. In the following statements of Python, which ones will result into the output: 6?
A = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
a. A[3][2]
b. A[2][3]
c. A[1][2]
d. A[2][1]
>>> m=6,7,8,9
>>> m
56. Which function removes a set’s first and the last element from a list?
a. pop
b. remove
c. dispose
d. discard
>>> x={1:”X”,2:”Y”,3:”Z”}
>>> del x
Answer: (d) the del would delete all the keys in dictionary
sum(1,2,3)
sum([2,4,6])
a. 6, 12
b. Error, Error
c. Error, 12
d. 6, Error
print(type(y))
find(‘letters’,X=’1′,Y=’2′)
a. Dictionary
b. An exception is thrown
c. String
d. Tuple
b. The memory space taken by the recursive functions is more than that of non-
recursive function
UNIT-3
a = [‘mn’, ‘op’]
print(len(list(map(list, a))))))
a. 4
b. 2
c. Not specified
d. Error
62. Which of these functions can NOT be defined under the sys module?
a. sys.argv
b. sys.readline
c. sys.path
d. sys.platform
a. re.compile
b. re.findall
c. re.match
d. re.purge
d. delete an attribute
66. Find out the private data field among the following:
def __init__(self):
__m = 1
self.__n = 1
self.__o__ = 1
__p__= 1
a. __m
b. __n
c. __o__
d. __p__
a. accept
b. finally
c. try
d. except
Answer: b
Explanation: Python is a high-level programming language known for its readability
and simplicity.
a) //
b) #
c) –
d) /* */
Answer: b
Explanation: The hash symbol (#) is used for single-line comments.
a) 10
b) 7
c) 25
d) 3
Answer: c
a) 2var
b) _myVar
c) my-var
d) my var
Answer: b
a) A list of numbers
b) A tuple of numbers
c) A sequence of numbers
d) A dictionary of numbers
Answer: c
a) int
b) float
c) char
d) str
Answer: c
Answer: a
75. Which of the following statements is true about Python’s variable naming
convention?
Answer: c
a) abcabcabcabc
b) abcabc
c) abcabcabc
d) Error: cannot multiply sequence by non-int of type ‘str’
Answer: c
77. Which of the following is used to open a file in Python for reading and writing?
a) r+
b) rw
c) a+
d) Both a and c
Answer: d
Answer: b
79. Which of the following is NOT a valid way to comment out multiple lines of code
in Python?
Answer: d
a) 4.5
b) 4
c) 4.0
d) Error: division by zero
Answer: a.
a) tuple
b) array
c) set
d) dictionary
Answer: b
Answer: b.
a) tuple
b) string
c) list
d) set
Answer: c
a) True
b) False
c) Error: invalid syntax
d) Error: x and y are not defined
Answer: a
Answer: d
86. What does the len() function in Python return?
87. Which of the following is the correct way to open a file named “example.txt” in
Python for reading?
Answer: a
88. What does the append() method do when used with lists in Python?
Answer: a
89. Which of the following is the correct way to define a function in Python?
a) def function_name:
b) function function_name():
c) define function_name():
d) def function_name():
Answer: d
a) [1, 2]
b) [3, 4]
c) [2, 3]
d) [2, 4]
Answer: b
UNIT-4
91. Which of the following is the correct way to import the math module in Python?
a) import math
b) include math
c) use math
d) require math
Answer: a
92. Which of the following is not a valid comparison operator in Python?
a) <=
b) =>
c) ==
d) !=
Answer: b
93. What does the pop() method do when used with lists in Python?
Answer: b
94. Which of the following is true about Python’s None keyword?
Answer: b
a) 3.3333
b) 3.0
c) 3
d) 4
Answer: a
Answer: d
a) dlroW olleH
b) Hello World
c) World Hello
d) olleH dlroW
print(type(3.5))Copy Code
a) <class ‘float’>
b) <class ‘int’>
c) <class ‘string’>
d) <class ‘number’>
a) 1_variable
b) variable_name
c) variable-name
d) None
Answer: b) variable_name
a) True
b) False
c) Error
d) None
Answer: b) False
d) To define a function
Answer: a
x = 10
if x > 5:
print("x is greater than 5")
a) x is greater than 5
b) x is less than 5
c) x is equal to 5
d) No output
Answer: a
103. Which keyword is used to execute a block of code if the condition in the if
statement is false?
a) else
b) elif
c) while
d) for
Answer: a
b) To define a loop
Answer: a
x=5
if x < 3:
print("x is less than 3")
elif x == 3:
print("x is equal to 3")
else:
print("x is greater than 3")
Answer: c
106. How can you execute multiple statements under a single if block in Python?
Answer: b
x = 10
if x < 5:
print("x is less than 5")
elif x > 15:
print("x is greater than 15")
else:
print("x is between 5 and 15")
Answer: c
Answer: a
a) while condition:
b) while condition():
c) while (condition):
Answer: a
Answer: a
Answer: b
Answer: d
Answer: a
114. How can you skip the current iteration of a loop and continue with the next
iteration?
Answer: d
Answer: a
for i in range(3):
print(i)
a) 0 1 2
b) 1 2 3
c) 2 1 0
d) 3 2 1 0
Answer: a
Answer: c
for i in range(3):
for j in range(3):
print(i + j, end=' ')
print()
a) 0 1 2
123
234
b) 0 1 2
123
234
345
c) 0 1 2
123
234
456
d) 0 1 2
234
456
Answer: a
num = 5
while num > 0:
print(num)
num -= 1
if num == 3:
break
else:
print("Done")
a) 5 4 3 2 1
b) 5 4
c) Done
d) 5 4 3
Answer: b
x = 10
if x > 5:
print("A")
elif x > 7:
print("B")
else:
print("C")
a) A
b) B
c) C
d) A and B
Answer: a
UNIT-5
x=5
while x > 0:
print(x, end=" ")
x -= 2
if x == 1:
break
else:
print("Done")
a) 5 3 1
b) 5 3
c) 5 3 Done
d) 5 3 1 Done
Answer: a
for i in range(5):
if i == 2:
continue
print(i, end=" ")
a) 0 1 3 4
b) 0 1 2 3 4
c) 0 1 3 4 5
d) 0 1 2 3 4 5
Answer: a
num = 0
while num < 5:
print(num)
num += 1
else:
print("Loop completed.")
a) 0 1 2 3 4
b) 0 1 2 3 4 Loop completed.
c) Loop completed.
d) This code will result in an error.
Answer: b
x = 10
if x > 5:
print("Greater than 5")
if x > 8:
print("Greater than 8")
if x > 12:
print("Greater than 12")
else:
print("Equal or less than 12")
a) Greater than 5
Greater than 8
Equal or less than 12
b) Greater than 5
Greater than 8
Greater than 12
Equal or less than 12
c) Greater than 5
Greater than 8
for i in range(3):
for j in range(3):
print(i * j, end=' ')
print()
a) 0 0 0
012
024
b) 0 1 2
024
036
c) 0 0 0
012
024
036
d) 0 0 0
012
012
Answer: a
num = 10
while num > 0:
print(num)
num -= 3
else:
print("Loop completed.")
a) 10 7 4 1 Loop completed.
b) 10 7 4 1
c) Loop completed.
d) This code will result in an error.
Answer: a
for i in range(3):
pass
print(i)
a) 0 1 2
b) 1 2 3
c) 2 1 0
d) 3 2 1 0
Answer: c
Answer: d
Answer: b
x = 10
if x > 5:
print("Hello")
elif x > 8:
print("Hi")
else:
print("Hey")
a) Hey
b) Hi
c) Hello
d) Hello, Hi
Answer: c
a) \n2\n\n4\n*
b) \n2\n\n4\n*\n
c) \n2\n\n*\n4\n
d) \n\n2\n*\n4\n
Answer: b
x=5
while x > 0:
print(x)
x -= 1
else:
print("Done")
a) 5\n4\n3\n2\n1\nDone
b) Done\n5\n4\n3\n2\n1
c) 5\n4\n3\n2\n1
d) Done
Answer: a
a) 1\n2
b) 1\n2\n3\n4\n5\nLoop completed.
c) 1\n2\n3\n4\n5
d) 1\n2\n3
Answer: a