Which of the following is correct?class A: def __init__(self,name): self.name=namea1=A("john")a2=A("john")id(a1) and id(a2) will have same value.id(a1) and id(a2) will have different values.Two objects with same value of attribute cannot be created.None of the above
Question
Which of the following is correct?class A: def init(self,name): self.name=namea1=A("john")a2=A("john")id(a1) and id(a2) will have same value.id(a1) and id(a2) will have different values.Two objects with same value of attribute cannot be created.None of the above
Solution
The correct answer is: "id(a1) and id(a2) will have different values."
Explanation: In Python, the id() function returns a unique id for the specified object. Every object in Python has its own unique id. The id is the object's memory address, and will be different for each time you run the program (except for some object that has a constant unique id, like integers from -5 to 256).
So, even though the two instances of class A (a1 and a2) have the same value for the attribute 'name', they are different objects and will have different ids.
Similar Questions
Which of the following is correct?class A: def __init__(self,name): self.name=namea1=A("john")a2=A("john")
class People(): def __init__(self, name): self.name = name def namePrint(self): print(self.name)person1 = People("Sally")person2 = People("Louise")person1.namePrint()A. person1 and person2 are two different instances of the People class.B. The __init__ method is used to set initial values for attributes.C. 'self' is not needed in def namePrint(self):D. person2 has a different value for 'name' than person1.
In this following code, what is id?class User: id = 89 name = "no name" __password = None def __init__(self, new_name=None): self.is_new = True if new_name is not None: self.name = new_nameA public class attributeA public instance attributeA private class attributeA protected class attributeA public class methodA public instance method
Which of the following is correct?class A: def __init__(self): self.count=5 self.count=count+1a=A()print(a.count)
Which of the following is False with respect Python code?class Student: def __init__(self,id,age): self.id=id self.age=agestd=Student(1,20)"std" is the reference variable for object Student(1,20)id and age are called the parametersEvery class must have a constructorNone of the above
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.