Knowee
Questions
Features
Study Tools

What will be the output of the following Python code?class A: def one(self): return self.two()  def two(self): return 'A' class B(A): def two(self): return 'B'obj1=A()obj2=B()print(obj1.two(),obj2.two())A AA BB BAn exception is thrown

Question

What will be the output of the following Python code?class A: def one(self): return self.two()  def two(self): return 'A' class B(A): def two(self): return 'B'obj1=A()obj2=B()print(obj1.two(),obj2.two())A AA BB BAn exception is thrown

🧐 Not the exact question you are looking for?Go ask a question

Solution

The output of the given Python code will be 'A B'.

Here's the step by step explanation:

  1. Two classes A and B are defined. Class B is a subclass of Class A.

  2. In Class A, there are two methods: 'one' and 'two'. The 'one' method returns the result of the 'two' method. The 'two' method returns the string 'A'.

  3. In Class B, the 'two' method is overridden to return the string 'B'.

  4. Two objects 'obj1' and 'obj2' are created from Class A and Class B respectively.

  5. When 'obj1.two()' is called, it refers to the 'two' method in Class A and returns 'A'.

  6. When 'obj2.two()' is called, it refers to the 'two' method in Class B (since it's overridden in Class B) and returns 'B'.

  7. Therefore, when 'print(obj1.two(),obj2.two())' is executed, it prints 'A B'.

This problem has been solved

Similar Questions

class A: def one(self): return self.two() def two(self): return 'A' class B(A): def two(self): return 'B'obj2=B()print(obj2.two())AAn exception is thrownA BB

What will be the output of the following Python code?class A: def __repr__(self): return "1"class B(A): def __repr__(self): return "2"class C(B): def __repr__(self): return "3"o1 = A()o2 = B()o3 = C()print(obj1, obj2, obj3)1 1 11 2 3‘1’ ‘1’ ‘1’An exception is thrown

What will be the output of the following Python code?class A: def __init__(self, x, y): self.x = x self.y = y def __str__(self): return 1 def __eq__(self, other): return self.x * self.y == other.x * other.yobj1 = A(5, 2)obj2 = A(2, 5)print(obj1 == obj2)False1TrueAn exception is thrown

What will be the output of the following Python code?class Demo: def __init__(self): self.a = 1 self.__b = 1 def get(self): return self.__bobj = Demo()obj.a=45print(obj.a) The program runs properly and prints 45 The program has an error because the value of members of a class can’t be changed from outside the classThe program runs properly and prints 1 The program has an error because the value of members outside a class can only be changed as self.

What will be the output of the following Python code?class Demo: def __init__(self): self.a = 1 self.__b = 1 def get(self): return self.__bobj = Demo()obj.a=45print(obj.a)

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.