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)
Question
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)
Solution
The output of the code will be:
Raghu 36
Explanation: The code defines a class named "Person" with a constructor that takes three parameters: term, name, and age. The constructor assigns the values of these parameters to the instance variables term.name and term.age.
Then, an instance of the Person class is created with the name "Raghu" and age 36, and this instance is assigned to the variable p1.
Finally, the name and age of the p1 instance are printed, which are "Raghu" and 36, respectively.
Similar Questions
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
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
What will be the output of the following code?class MyClass: def __init__(self, value=5): self.value = valueobj1 = MyClass()obj2 = MyClass(10)print(obj1.value, obj2.value)
what is the output of following python code? class myclass: def __init__(self,a): self.a = a print(self.a)o=myclass()
What will be the output of below Python code?class Student: def __init__(self,name,id): self.name=name self.id=id print(self.id)std=Student("Simon",1)std.id=2print(std.id)
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.