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
Question
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
Solution 1
The program runs fine and 8.7 is printed.
Here's the step by step explanation:
-
A class named 'student' is defined with two attributes: 'marks' and '__cgpa'. The 'marks' attribute is public, while '__cgpa' is private due to the double underscore prefix.
-
The 'display' method is defined to print the 'marks' attribute.
-
An object 'obj' of the 'student' class is created.
-
The private attribute '__cgpa' is accessed using name mangling, which is a technique used in Python for accessing private attributes. The syntax for this is '_classname__attributename'. In this case, it is '_student__cgpa'.
-
The value of '__cgpa' (8.7) is printed.
So, the output of the code will be '8.7'.
Solution 2
The program runs fine and 8.7 is printed.
Here's the step by step explanation:
-
A class named 'student' is defined with two attributes: 'marks' and '__cgpa'. The double underscore before 'cgpa' makes it a private attribute.
-
A method named 'display' is defined within the class that prints the value of 'marks'.
-
An object 'obj' of the class 'student' is created.
-
The private attribute '__cgpa' is accessed using name mangling, which is a technique used in Python for accessing private attributes. The syntax for this is '_classname__attributename'.
-
So, 'print(obj._student__cgpa)' prints the value of '__cgpa', which is 8.7.
Therefore, the output of the code will be 8.7.
Similar Questions
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
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)
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 accessedClear ResponseSave & Next
What will be the output of the following Python code?class Demo: def __init__(self): self.a = 1 self.__b = 1 def display(self): return self.__bobj = Demo()print(obj.a)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 runs fine and 1 is printedThe program has an error as you can’t name a class member using __b
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
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.