Choose the Correct AnswerWhat is the output of the following code?class Person: def __init__(term, name, age): term.name = name term.age = agep1 = Person("Raghu", 36)print(p1.name)print(p1.age)OptionsRaghu 36Type Errorterm Raghu 36None of the Above
Question
Choose the Correct AnswerWhat is the output of the following code?class Person: def init(term, name, age): term.name = name term.age = agep1 = Person("Raghu", 36)print(p1.name)print(p1.age)OptionsRaghu 36Type Errorterm Raghu 36None of the Above
Solution
The correct answer is "Raghu 36".
Here's the step by step explanation:
-
A class named 'Person' is defined with a constructor method 'init'. This method is used to initialize the attributes of an object. In this case, the attributes are 'name' and 'age'.
-
The constructor method takes three parameters - 'term', 'name', and 'age'. 'term' is a reference to the instance of the class and 'name' and 'age' are the attributes of the class.
-
An object 'p1' of the class 'Person' is created with the name "Raghu" and age 36.
-
When 'print(p1.name)' is executed, it prints the name of the object 'p1', which is "Raghu".
-
Similarly, 'print(p1.age)' prints the age of the object 'p1', which is 36.
So, the output of the code is:
Raghu 36
Similar Questions
What is the output of the following code?class Person: def __init__(term, name, age): term.name = name term.age = agep1 = Person("term","Raghu", 36)print(p1.name)print(p1.age)
Problem statementSend feedbackWhat will be the output of the following code?class Student: def __init__(self,name,age): self.name = “Rohan” self.age = 60 def print_student_details(self): print(self.name, end= “ “) print(self.age)s = Student("saif",20)s.print_student_details()Options: Pick one correct answer from belowRohan 60NoneErrorNone of the Above
What will be the output of the following code?class Student: def store_details(self): self.age = 60 self.name = ‘Parikh’ def print_details(self): print(self.name, end=” ”) print(self.age)s = Student()s.store_details()s.print_details()Options: Pick one correct answer from belowErrorParikh 60Parikh NoneNone of the above
Problem statementSend feedbackWhat will be the output of this code?class Student: def __init__(self,name): self._name = name class Student1(Student): def __init__(self,name): super().__init__(name)s = Student("saif")print(s._name)
What is the output of the below code?Code:class Cat: def __init__(self, age): self.age = age def set_age(self, num): self.age = num def get_age(self): return self.age cat1 = Cat(2)cat2 = Cat(4)cat1.set_age(cat2.get_age())cat2.set_age(5)cat3 = Cat(cat1.get_age() + cat2.get_age())print(cat3.get_age())*10965
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.