Explanations "Top Python Quiz Questions"
349 subscribers
80 photos
1 link
Explanations "Top Python Quiz Questions"
Download Telegram
What will be the output of the following Python code?
class Demo:
def __init__(self):
self.a = 1
self.__b = 1

def display(self):
return self.__b

obj = Demo()
print(obj.__b)


The program has an error because there isn’t any function to return self.a
The program has an error because b is private and display(self) is returning a private member
The program has an error because b is private and hence can’t be printed
The program runs fine and 1 is printed

Explanation:
Variables beginning with two underscores are said to be private members of the class and they can’t be accessed directly.
What will be the value of X in the following Python expression
X = 2+9*((3*12)-8)/10


30.0
30.8
28.4
27.2

Explanation:
The expression shown above is evaluated as: 2+9*(36-8)/10, which simplifies to give 2+9*(2.8), which is equal to 2+25.2 = 27.2. Hence the result of this expression is 27.2.
What will be the output of the following Python code?
re.split(r'(a)(t)', 'Maths is a difficult subject')


[‘M a t h s i s a d i f f i c u l t s u b j e c t’]
[‘Maths’, ‘is’, ‘a’, ‘difficult’, ‘subject’]
‘Maths is a difficult subject’
[‘M’, ‘a’, ‘t’, ‘hs is a difficult subject’]

Explanation:
The code shown above demonstrates the use of the function re.match. The first argument of this function specifies the pattern. Since the pattern contains groups, those groups are incorporated in the resultant list as well. Hence the output of the code shown above is [‘M’, ‘a’, ‘t’, ‘hs is a difficult subject’].
What is the output of print 0.1 + 0.2 == 0.3?

True
False
Machine dependent
Error

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.
What is the output of the following code?
aTuple = (100, 200, 300, 400, 500)
aTuple[1] = 800
print(aTuple)


TypeError
(100, 800, 200, 300, 400, 500)
(800, 100, 200, 300, 400, 500)

Explanation:
A tuple is immutable. Once a tuple is created, you cannot change its values. If you try to change its value, you will receive a TypeError: 'tuple' object does not support item assignment
What happens in the code?
class A: 
def __init__(self, i=100):
self.i=i
class B(A):
def __init__(self,j=0):
self.j=j
def main():
b= B()
print(b.i)
print(b.j)
main()


Class B inherits all the data fields of class A.
Class B needs an Argument.
The data field ‘j’ cannot be accessed by object b.
Class B is inheriting class A but the data field ‘i’ in A cannot be inherited.

Explanation:
Reason being that i is initiated with self thus making it a instantiate variable of that class which cannot be inherited by the above way.
What is output for the following code?
a = ['hat', 'mat', 'rat']
'rhyme'.join(a)


[‘hat’,’mat’,’rat’,’rhyme’]
‘hatmatratrhyme’
[‘hat mat rat rhyme’]
‘hatrhymematrhyme rat’

Explanation:
The method join() takes list of string as input and returns string as output. It removes ‘,’ and add the given string with join to the list.
What is output of following code?
class Count:
def __init__(self, count=0):
self.__count=count
a=Count(2)
b=Count(2)
print(id(a)==id(b), end = '' '')

c= ''hello''
d= ''hello''
print(id(c)==id(d))


True False
False True
False False
True True

Explanation:
The objects with same contents share the same object in the python library but this is not true for custom-defined immutable classes.
What will be the output of the following Python code snippet?
first = bool('False')
second = bool()
print(str(first) + " " + str(second))


True True
False True
True False
False False

Explanation:
The Boolean function returns true if the argument passed to the bool function does not amount to zero. In the first example, the string ‘False’ is passed to the function bool. This does not amount to zero and hence the output is true. In the second function, an empty list is passed to the function bool. Hence the output is false.
What is the output of the following?
x = 'abcd'
for i in range(len(x)):
x[i].upper()
print (x)


abcd
ABCD
error
none of the mentioned

Explanation:
Changes do not happen in-place, rather a new instance of the string is returned.
What is the value of b?
a, b, c = (1, 2, 3, 4, 5, 6, 7, 8, 9)[1::3]


2
5
6
4

Explanation:
The slice expression on the right side of the assignment produces the tuple (2, 5, 8). The assignment is thus equivalent to this compound tuple packing/unpacking assignment: a, b, c = (2, 5, 8). As a result b is given the value 5.
What is the output of the following?
D = dict() 
for x in enumerate(range(2)):
D[x[0]] = x[1]
D[x[1]+7] = x[0]
print(D)


{0: 1, 7: 0, 1: 1, 8: 0}
{1: 1, 7: 2, 0: 1, 8: 1}
{0: 0, 7: 0, 1: 1, 8: 1}
KeyError

Explanation:
enumerate() will return a tuple, the loop will have x = (0, 0), (1, 1). Thus D[0] = 0, D[1] = 1, D[0 + 7] = D[7] = 0 and D[1 + 7] = D[8] = 1.
Note:
Dictionary is unordered, so the sequence of the key-value pair may differ in each output.
What is the output of the following piece of code?
class A():
def disp(self):
print("A disp()")
class B(A):
pass
obj = B()
obj.disp()


Invalid syntax for inheritance
Error because when object is created, argument must be passed
Nothing is printed
A disp()

Explanation:
Class B inherits class A hence the function disp () becomes part of class B’s definition. Hence disp() method is properly executed and the line is printed.
What will be the output of the following code?
x=100
def f1():
global x
x=90
def f2():
global x
x=80
print(x)


100
90
80
Error

Explanation:
The output of the code shown above is 100. This is because the variable ‘x’ has been declared as global within the functions f1 and f2.
What will be the output of the code?
class A:
def __init__(self):
self.__i = 1
self.j = 5

def display(self):
print(self.__i, self.j)
class B(A):
def __init__(self):
super().__init__()
self.__i = 2
self.j = 7
c = B()
c.display()


2 7
1 5
1 7
2 5

Explanation:
Any change made in variable i isn’t reflected as it is the private member of the superclass.
What will be the output of the following code?
f=lambda x:bool(x%2)
print(f(20), f(21))


False True
False False
True True
True False

Explanation:
The code shown above will return true if the given argument is an odd number, and false if the given argument is an even number. Since the arguments are 20 and 21 respectively, the output of this code is: False True.
What will be the output shape of the following Python code?
import turtle
t=turtle.Pen()
for i in range(0,4):
t.forward(100)
t.left(120)


square
rectangle
triangle
kite

Explanation:
According to the code shown above, 4 lines will be drawn. Three lines will be in the shape of a triangle. The fourth line will trace the base, which is already drawn. Hence the base will be slightly thicker than the rest of the lines. However there will be no change in the shape due to this extra line. Hence the output shape will be a triangle.
What will be the output of the statements?
def foo():
try:
return 1
finally:
return 2
k = foo()
print(k)


1
2
3
error, there is more than one return statement in a single try-finally block

Explanation:
The finally block is executed even there is a return statement in the try block.
What will be the output of the following code?
a=[13,56,17]
a.append([87])
a.extend([45,67])
print(a)


[13, 56, 17, [87], 45, 67]
[13, 56, 17, 87, 45, 67]
[13, 56, 17, 87, [45, 67]]
[13, 56, 17, [87], [45, 67]]

Explanation:
The append function simply adds its arguments to the list as it is while extend function extends its arguments and later appends it.
When a value is truncated to 3 decimal places, which of the following is true?

Both positive and negative numbers are rounded down.
Positive numbers are rounded down and negative numbers are rounded up.
Positive numbers are rounded up and negative numbers are rounded down.
Both positive and negative numbers are rounded up.

Explanation:
When you truncate a positive number, you just chop off digits past the digit to which you are rounding. For example, truncating 1.7365 to three decimal places results in 1.736. The result is the same as rounding down to three decimal places. On the other hand, truncating -1.7365 to three decimal places results in -1.736, which is to the right of -1.7365 on the number line, and is therefore the same as rounding up to three decimal places.
What will be the output of the code?
A = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
[A[i][i] for i in range(len(A))]


[1, 5, 9]
[3, 5, 7]
[4, 5, 6]
[2, 5, 8]

Explanation:
We can perform tasks like pulling out a diagonal. The expression shown above uses range to generate the list of offsets and the indices with the row and column the same, picking out A[0][0], then A[1][1] and so on. Hence the output of the code is: [1, 5, 9].