Class a:
def __init__(self):
print('one')
def f(self):
print(float())
print(hex(-255))
class b(a):
def __init__(self):
print('two')
def f(self):
print(float())
print(hex(-42))
x = b()
x.f()
Answers
Explanation:
class A:
def __init__(self, i = 0):
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()
A. Class B inherits A, but the data field in i in A is not inherited.
B. Class B inherits A and automatically inherits all data fields in A.
C. When you create an object B, you have to pass an integer such as B(5).
D. The data field j cannot be accessed by object b.
12.2 What will be displayed by the following code?
class A:
def __init__(self, i = 1):
self.i = i
class B(A):
def __init__(self, j = 2):
super().__init__()
self.j = j
def main():
b = B()
print(b.i, b.j)
main()
A. 0 0
B. 0 1
C. 1 2
D. 0 2
E. 2 1
12.3 What is the output of the following code?
class ParentClass:
def __init__(self):
self.__x = 1
self.y = 10
def print(self):
print(self.__x, self.y)
class ChildClass(ParentClass):
def __init__(self):
super().__init__()
self.__x = 2
self.y = 20
c = ChildClass()
c.print()
A. 1 10
B. 1 20
C. 2 10
D. 2 20
12.4 Suppose A is a subclass of B, to invoke the __init__ method in B from A, you write _________.
A. super().__init__()
B. super().__init__(self)
C. B.__init__()
D. B.__init__(self)
12.5 What code can you put in the third line in class B to invoke B's 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()
A. super().__init__(self)
B. super().__init__()
C. A.__init__()
D. A.__init__(self)
Section 12.3 Overriding Methods
12.6 What will be displayed by the following code?
class A:
def __init__(self, i = 0):
self.i = i
def m1(self):
self.i += 1
class B(A):
def __init__(self, j = 0):
A.__init__(self, 3)
self.j = j
def m1(self):
self.j += 1
def main():
b = B()
b.m1()
print(b.i, b.j)
main()
A. 2 0
B. 3 1
C. 4 0
D. 3 0
E. 4 1
12.7 Which of the following statements is true?
A. A subclass is a subset of a superclass.
B. When invoking a constructor from a subclass, its superclass's no-arg constructor is always invoked.
C. You can override a non-private method defined in a superclass.
D. You can override the initializer defined in a superclass.
E. You can override a private method defined in a superclass.
Section 12.4 The object Class
12.8 What will be displayed by the following code?
class A:
def __new__(self):
self.__init__(self)
print("A's __new__() invoked")
def __init__(self):
print("A's __init__() invoked")
class B(A):
def __new__(self):
print("B's __new__() invoked")
def __init__(self):
print("B's __init__() invoked")
def main():
b = B()
a = A()
main()
A. B's __new__() invoked and followed by A's __init__() invoked
B. B's __new__() invoked followed by A's __new__() invoked
C. B's __new__() invoked, followed by A's __init__() invoked, and followed by A's __new__() invoked
D. A's __init__() invoked and followed by A's __new__() invoked
12.9 Which of the following statements is true?
A. By default, the __new__() method invokes the __init__ method.
B. The __new__() method is defined in the object class.
C. The __init__() method is defined in the object class.
D. The __str__() method is defined in the object class.
E. The __eq__(other) method is defined in the object class.
Section 12.5 Polymorphism and Dynamic Binding
12.10 What will be displayed by the following code?
class A:
def __init__(self):
self.i = 1
def m(self):
self.i = 10
class B(A):
def m(self):
self.i += 1
return self.i
def main():
b = B()
print(b.m())
main()
A. 1
B. 2
C. 10
D. i is not accessible from b.
12.11 What will be displayed by the following code?
class A:
def __str__(self):
return "A"
class B(A):
def __str__(self):
return "B"
class C(B):
def __str__(self):
return "C"
def main():
b = B()
a = A()
c = C()
print(a, b, c)
main()
A. C C C
B. A B C
C. A A A
D. B B B
12.12 What will be displayed by the following code?
class A:
def __str__(self):
return "A"
class B(A):
def __init__(self):
super().__init__()
class C(B):
def __init__(self):
super().__init__()
def main():
b = B()
a = A()
c = C()
print(a, b, c)
main()
A. C C C
B. A B C
C. A A A
D. B B B
12.13 What will be displayed by the following code?
class A:
def __init__(self, i = 2, j = 3):
self.i = i
self.j = j
def __str__(self):
return "A"
def __eq__(self, other):
return self.i * self.j == other.i * other.j
def main():
x = A(1, 2)
y = A(2, 1)
print(x == y)
main()
A. True
B. False
C. 2
D. 1
12.14 What will be displayed by the following code?
class Person:
def getInfo(self):
return "Person's getInfo is called"
def printPerson(self):
print(self.getInfo(), end = ' ')
class Student(Person):
def getInfo(self):
return "Student's getInfo is called"
def main():
Person().printPerson()
Student().printPerson()
main()
A. Person's getInfo is called Person's getInfo is called
B. Person's getInfo is called Student's getInfo is called
C. Student's getInfo is called Person's getInfo is called
D. Student's getInfo is called Student's getInfo is called
Answer:
This code defines two classes, "a" and "b", which are related by inheritance. Class "a" has an __init__ method and a f method. The __init__ method simply prints the string "one" when an object of class "a" is created. The f method prints the result of calling float() (which is 0.0) and then the result of calling hex(-255) (which is '-0xFF').
Explanation:
Class "b" is a subclass of class "a", and it also has an __init__ method and a f method. The __init__ method of class "b" overrides the __init__ method of class "a" and prints the string "two" instead of "one" when an object of class "b" is created. The f method of class "b" also overrides the f method of class "a" and prints the result of calling float() (which is 0.0) and then the result of calling hex(-42) (which is '-0x2a').
Finally, the last line of the code creates an object of class "b" and assigns it to the variable x. Then it calls the f method on the x object, which will print 0.0 and -0x2a as the output.
More question and answers:
https://brainly.in/question/15138221
https://brainly.in/question/15526769
#SPJ3