Choose the Correct Answer(s)What is the output of the following code?class test: def __init__(self): print ("Hello World") def __init__(self): print ("Happy Coding")obj = test()OptionsHappy CodingHappy WorldHello WorldNone of the above
Question
Choose the Correct Answer(s)What is the output of the following code?class test: def init(self): print ("Hello World") def init(self): print ("Happy Coding")obj = test()OptionsHappy CodingHappy WorldHello WorldNone of the above
Solution
The correct answer is "Happy Coding".
Explanation: In Python, if a class has multiple methods with the same name, only the last one is considered. Here, the class 'test' has two init methods. Python will only consider the last init method. When an object of the class 'test' is created, it will call the last init method, which prints "Happy Coding". Therefore, "Happy Coding" is printed as the output.
Similar Questions
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)
What will be the output of the following Python code?class program: def __init__(self, param): self.o1 = param class code(program): def __init__(self, param): self.o2 = param ct = code(22)print ("%d %d" % (ct.o1, ct.o2))OptionsNone NoneNone 2222 NoneError is generated
What will be the output of the following code?class MyClass: def __init__(self, value): self.value = valueobj = MyClass(10)print(obj.value)
What will be the output of the following Python code?class fruits: def __init__(self): self.price = 100 self.__bags = 5 def display(self): print(self.__bags)obj=fruits()obj.display()
what is the output of following python code? class myclass: def __init__(self,a): self.a = a print(self.a)o=myclass("Welcome")
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.