Knowee
Questions
Features
Study Tools

class student: def __init__(self): self.marks = 97 self.__cgpa = 8.7 def display(self): print(self.marks)obj=student()print(obj._student__cgpa) The program runs fine and 8.7 is printed Error because private class members can’t be accessed Error because the proper syntax for name mangling hasn’t been implemented The program runs fine but nothing is printed

Question

class student: def init(self): self.marks = 97 self.__cgpa = 8.7 def display(self): print(self.marks)obj=student()print(obj._student__cgpa) The program runs fine and 8.7 is printed Error because private class members can’t be accessed Error because the proper syntax for name mangling hasn’t been implemented The program runs fine but nothing is printed

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

Solution

The program runs fine and 8.7 is printed.

This is because in Python, there is no strict concept of "private" members. However, if a member is prefixed with double underscores, it becomes a little harder to access it directly, which is often interpreted as "private". This is called name mangling.

In the given code, __cgpa is a name mangled attribute. It can be accessed outside the class using the _classname__attribute syntax. So, obj._student__cgpa is a valid way to access the __cgpa attribute of the student class, and it will print 8.7.

This problem has been solved

Similar Questions

What will be the output of the following Python code? class student: def __init__(self): self.marks = 97 self.__cgpa = 8.7 def display(self): print(self.marks)obj=student()print(obj._student__cgpa) The program runs fine and 8.7 is printed Error because private class members can’t be accessed Error because the proper syntax for name mangling hasn’t been implemented The program runs fine but nothing is printedClear ResponseSave & Next

class Demo: def __init__(self): self.a = 1 self.__b = 1  def display(self): return self.__b obj = Demo()print(obj.__b) The program has an error because there isn’t any function to return self.aThe program has an error because b is private and display(self) is returning a private memberThe program has an error because b is private and hence can’t be printedThe program runs fine and 1 is printed

What will be the output of the following Python code?class A: def __init__(self): self._x = 5 class B(A): def display(self): print(self._x)def main(): obj = B() obj.display()main()Error, invalid syntax for object declarationNothing is printed5Error, private class member can’t be accessed in a subclass

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()  The program has an error because display() is trying to print a private class memberThe program runs fine but nothing is printedThe program runs fine and 5 is printed The program has an error because display() can’t be accessed

What will be the output of the following Python code?class objects: def __init__(self): self.colour = None self._shape = "Circle"   def display(self, s): self._shape = sobj=objects()print(obj._objects_shape)The program runs fine because name mangling has been properly implementedError because the member shape is a protected memberError because the proper syntax for name mangling hasn’t been implementedError because the member shape is a private member

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.