Quiz
OOPs Concept in Python Quiz 2
This quiz has a time limit, so it opens in the usual player.
Questions in this quiz (14)
1What will be the output of the following code?
class Test:
def __init__(self, s):
self.s = s
def print(self):
print(s)
a = Test("Python Class")
a.print()- AThe program gives an error because there is no constructor for class Test.
- BSignature for the print method is incorrect, so an error is thrown.
- CThe above code will execute correctly on changing print(s) to print(self.s).
2What will be the output of the following code?
class Test:
def __init__(self, s):
self.s = s
def print(self):
print(self.s)
msg = Test()
msg.print()- AThe program has an error because class Test does not have a constructor.
- BThe above code produces an error because the definition of print(s) does not include .
- CIt executes successfully but prints nothing.
- DThe program has an error because of the constructor call is made without an argument.
3What will be the output of the following code?
class Test:
def __init__(self, s = "Welcome"):
self.s = s
def print(self):
print(self.s)
msg = Test()
msg.print()- AThe program has an error because the constructor is not present in class Test.
- BThe above code produces an error because the definition of print(s) does not contain .
- CIt executes successfully but prints nothing.
- DThe program has an error because of the constructor call is made without an argument.
- EThe program executes successfully and prints Welcome.
4What will be the output of the following code snippet?
class Test:
def __init__(self):
self.x = 1
self.__y = 1
def getY(self):
return self.__y
val = Test()
print(val.x)- AThe program has an error because x is private and cannot be accessed outside of the class.
- BThe program has an error because you cannot name a variable using .
- CThe program runs fine and prints 1.
- DThe program runs fine and prints nothing.
5What will be the output of the following code snippet?
class Test:
def __init__(self):
self.x = 1
self.__y = 1
def getY(self):
return self.__y
val = Test()
print(val.__y)- AThe program has an error because y is private and should not access it from outside the class.
- BThe program has an error because you cannot name a variable using __y.
- CThe program runs fine and prints 1.
- DThe program runs fine and prints nothing.
6What will be the output of the following code snippet?
class Test:
def __init__(self):
self.x = 1
self.__y = 1
def getY(self):
return self.__y
val = Test()
val.x = 45
print(val.x)- AThe program has an error because x is private and should not access it from outside the class.
- BThe program has an error because you cannot name a variable using __y.
- CThe program runs fine and prints 1.
- DThe program runs fine and prints 45.
7Which of the following is a private data field in the given code snippet?
class Test:
def __init__(self):
__a = 1
self.__b = 1
self.__c__ = 1
__d__ = 1- A__a
- B__b
- C__c__
- D__d__
8What will be the output of the following code snippet?
class Test:
def __init__(self):
self.x = 1
self.__y = 1
def getY(self):
return self.__y
val= Test()
val.__y = 45
print(val.getY())- AThe program has an error because y is private and should not access it from outside the class.
- BThe program has an error because you cannot name a variable using __y.
- CThe code runs fine and prints 1.
- DThe code executes successfully and prints 45.
9What will be the output of the following code snippet?
def main():
myCounter = Counter()
num = 0
for i in range(0, 100):
increment(myCounter, num)
print("myCounter.counter =", myCounter.counter, ", number of times =", num)
def increment(c, num):
c.counter += 1
num += 1
class Counter:
def __init__(self):
self.counter = 0
main()- Acounter is 101 , number of times is 0
- Bcounter is 100, number of times is 0
- Ccounter is 100, number of times is 100
- Dcounter is 101, number of times is 101
10What code can we put at the third line of the definition of class B to invoke its superclass’s constructor?
class A:
def __init__(self, i = 1):
self.i = i
class B(A):
def __init__(self, j = 2):
___________________
self.j = j
def main():
b = B()
print(b.i, b.j)
main()- Asuper().__init__(self)
- Bsuper().__init__()
- CA.__init__()
- DA.__init__(self)
11What will be the output of the following code snippet?
class A:
def __init__(self, x = 1):
self.x = x
class B(A):
def __init__(self, y = 2):
super().__init__()
self.y = y
def main():
b = B()
print(b.x, b.y)
main()- A0 0
- B0 1
- C1 2
- D0 2
- E2 1
12What will be the output of the following code snippet?
class A:
def __init__(self):
self.__x = 1
self.y = 10
def print(self):
print(self.__x, self.y)
class B(A):
def __init__(self):
super().__init__()
self.__x = 2
self.y = 20
c = B()
c.print()- A1 10
- B1 20
- C2 10
- D2 20
13What will be the output of the following code snippet?
class A:
def __init__(self, x = 0):
self.x = x
def func1(self):
self.x += 1
class B(A):
def __init__(self, y = 0):
A.__init__(self, 3)
self.y = y
def func1(self):
self.y += 1
def main():
b = B()
b.func1()
print(b.x, b.y)
main()- A2 0
- B3 1
- C4 0
- D3 0
- E4 1
14What will be the output of the following code snippet?
class A:
def __init__(self, num):
self.x = num
class B(A):
def __init__(self, num):
self.y = num
obj = B(11)
print ("%d %d" % (obj.x, obj.y))- ANone None
- BNone 11
- C11 None
- D11 11
- EAttributeError: ‘B’ object has no attribute ‘x’
Information
- Description
- Coding Python Quiz on OOPs Concept in Python Quiz 2, created by PathaPadha Support on 27/10/2020.
- Author
- PathaPadha Support
- Language
- English
- Views
- 154
- Tags
- Rating
- No ratings yet Rate this quiz