Python MCQ
Python MCQ
Python MCQ
31 characters
63 characters
79 characters
Correct Answer:
Identifiers can be of any length.
2. Given a function that does not return any value, What value is thrown by default when executed in shell.
Int
bool
void
none
Correct Answer:
none
3. In python we do not specify types, it is directly interpreted by the compiler, so consider the following
operation to be performed.
x = 13 // 2
x = int(13 / 2)
x = 13 % 2
(1.0, 4.0)
(1.0, 1.0)
(4.0. 1.0)
(4.0, 4.0)
Correct Answer:
(1.0, 4.0)
Explanation: Expression 1 is evaluated as: 2**9, which is equal to 512. Expression 2 is evaluated as 8**2, which is
equal to 64. The last expression is evaluated as 2**(3**2). This is because the associativity of ** operator is from
right to left. Hence the result of the third expression is 512.
64, 512, 64
64, 64, 64
Correct Answer:
512, 64, 512
print("%s" % example[4:7])
world
sn
rl
Correct Answer:
wo
error
u
t
y
Correct Answer:
y
8. Given a string example=”hello” what is the output of example.count(‘l’)?
l occurs twice in hello.
2
1
None
0
Correct Answer:
2
9. What will be the output of the following Python code?
example = "helle"
example.rfind("e")
-1
4
1
Correct Answer:
4
10. Read the information given below carefully and write a list comprehension such that the output is: [‘e’,
‘o’]
w="hello"
v=('a', 'e', 'i', 'o', 'u')
The tuple ‘v’ is used to generate a list containing only vowels in the string ‘w’. The result is a list containing only
vowels present in the string “hello”. Hence the required list comprehension is: [x for x in w if x in v].
[x for w in v if x in v]
[x for x in w if x in v]
[x for x in v if w in v]
[x for v in w for x in w]
Correct Answer:
[x for x in w if x in v]
11. What will be the output of the following Python list comprehension?
The list comprehension shown above returns a list of non-prime numbers up to 50. The logic behind this is that the
square root of 50 is almost equal to 7. Hence all the multiples of 2-7 are not prime in this range.
A list of prime numbers up to 50
Error
Correct Answer:
A list of non prime numbers, up to 50
The built-in function pow() can accept two or three arguments. When it takes in two arguments, they are evaluated
as x**y. When it takes in three arguments, they are evaluated as (x**y)%z.
(x**y)**z
(x**y) / z
(x**y) % z
(x**y)*z
Correct Answer:
(x**y) % z
min(max(False,-3,-4), 2,7)
The function max() is being used to find the maximum value from among -3, -4 and false. Since false amounts to the
value zero, hence we are left with min(0, 2, 7) Hence the output is 0 (false).
2
False
-4
Correct Answer:
False
my_tuple = (1, 2, 3, 4)
my_tuple.append( (5, 6, 7) )
print len(my_tuple)
Tuples are immutable and don’t have an append method. An exception is thrown in this case.
1
2
Error
Correct Answer:
Error
l=[2, 3, [4, 5]] l2=l.copy() l2[0]=88 l l2 The code shown above depicts deep copy. In deep copy, the base address of
the objects is not copied. Hence the modification done on one list does not affect the other list.
Correct Answer:
[2, 3, [4, 5]] [88, 2, 3, [4, 5]
16. What will be the output of the following Python code and state the type of copy that is depicted?
l1=[2, 4, 6, 8] l2=[1, 2, 3] l1=l2 l2 The code shown above depicts shallow copy and the output of the code is: [1, 2,
3].
Correct Answer:
[1, 2, 3], shallow copy
ef fact(num): if num == 0: return 1 else: return _____________________ Suppose n=5 then, 5*4*3*2*1 is returned
which is the factorial of 5.
num*fact(num-1)
(num-1)*(num-2)
num*(num-1)
fact(num)*fact(num-1)
Correct Answer:
num*fact(num-1)
def f1(x): global x x+=1 print(x) f1(15) print("hello") The code shown above will result in an error because ‘x’ is a
global variable. Had it been a local variable, the output would be: 16
error
hello
16 hello
Correct Answer:
error
def f1(a,b=[]):
b.append(a)
return b
print(f1(2,[3,4]))
In the code shown above, the integer 2 is appended to the list [3,4]. Hence the output of the code is [3,4,2]. Both the
variables a and b are local variables.
[3,2,4]
[2,3,4]
Error
[3,4,2]
View Answer
Correct Answer:
[3,4,2]
20. Program code making use of a given module is called a ______ of the module.
Program code making use of a given module is called the client of the module. There may be multiple clients for a
module.
Client
Docstring
Interface
Modularity
Correct Answer:
Client
Refer documentation.
__add__()
__plus__()
__sum__()
none of the mentioned
Correct Answer:
__add__()
__invert__() overloads ~.
!
~
^
–
Correct Answer:
~
23. Let A and B be objects of class Foo. Which functions are called when print(A + B) is executed?
The function __add__() is called first since it is within the bracket. The function __str__() is then called on the
object that we received after adding A and B.
__add__(), __str__()
__str__(), __add__()
__sum__(), __str__()
__str__(), __sum__()
Correct Answer:
__add__(), __str__()
class A:
def __init__(self): self.__x = 1
class B(A):
def display(self):
print(self.__x)
def main():
obj = B()
obj.display()
main()
Correct Answer:
Error, private class member can’t be accessed in a subclass
t[5]
The expression shown above results in a name error. This is because the name ‘t’ is not defined.
IndexError
NameError
TypeError
syntaticalError
Correct Answer:
NameError
assert
eval
nonlocal
pass
Correct Answer:
eval
print('{:,}'.format(1112223334))
1122343444
1222333444
1112223334
1332224444
Correct Answer:
1112223334
return
assert
eval
class
Correct Answer:
return
assert
class
3*1**3
27
9
81
3
Correct Answer:
3
round(45.8)
round(6352.898,2,5)
round()
round(62.898,6,5)
Correct Answer:
round(45.8)
class
list
tuple
set
Correct Answer:
class
print('{:#}'.format(1112223334))
1,22,4,222,4
1112223334
1,4,2,3
1234
Correct Answer:
1112223334
Upper class
Lower class
Captialized
Correct Answer:
None of the above
11
-11
5
-5
Correct Answer:
5
my_Tutoriallinks
1Tutorial_links
_
_a_
Correct Answer:
1Tutorial_links
No
yes
Correct Answer:
No
5
1
3
25
Correct Answer:
1
list.sum(5)
list1.add(5)
list1.append(5)
list.addelement(5)
Correct Answer:
list1.append(5)
12
7
15
13
Correct Answer:
13
Y=[2,5J,6] Y.sort()
[2,6,5J]
[5J,2,6]
Error
[6,5J,2]
Correct Answer:
Error
hello
hel
HELLO
Correct Answer:
[‘h’, ‘e’, ‘l’, ‘l’, ‘o’].
42. In which of the following we can use "in" operator to check the elements
dictionary
list
tuple
Correct Answer:
list
6
36
8
4
Correct Answer:
8
[2, 6, 4].
[1, 3, 2, 3, 2, 1]
[1, 3, 2, 1, 3]
[1, 3, 2, 1, 3, 2] .
Correct Answer:
[1, 3, 2, 1, 3, 2] .
List is immutable
Tuple is mutable
Correct Answer:
List is mutable & Tuple is immutable
values = [[3, 4, 5, 1], [33, 6, 1, 2]] v = values[0][0] for lst in values: for element in lst: if v > element: v = element
print(v)
3
1
2
33
Correct Answer:
1
47. Output is
[1,2,5]
Error ';"
Correct Answer:
[0,1,2]
List = [True, 50, 10] List.insert(2, 5) print(List, "Sum is: ", sum(List))
Correct Answer:
[True, 50, 5, 10] Sum is: 66
49. Output is
l = [1,2,6,5,7,8] l.insert(9)
l=[9,1,2,6,5,7,8]
Error
l=[1,2,6,5,7,8,9]
Correct Answer:
Error
50. Assume the output
180[(45),(45),(45),(45)].
(45,45,45,45).[45,45,45,45].
180[45,45,45,45].
Syntax error
Correct Answer:
180[45,45,45,45].
a=[14,52,7]
b=a.copy()
b is a
False
True
Correct Answer:
False
52. Output is
a=[1,2,3,4]
b=[sum(a[0:x+1]) for x in range(0,len(a))]
print(b)
10
[1,3,4]
4
[1,3,6,10]
Correct Answer:
[1,3,6,10]
list
tuple
dictionary
set
Correct Answer:
dictionary
54. Output is
Error
Correct Answer:
Hello foo and bin
No
yes
Correct Answer:
yes