Skip to main content
All questions
EasyOOP · Encapsulation

Student Marks Encapsulation

Written by a community member · · 1 attempt · 0% pass

0

Implement a Python class that demonstrates encapsulation for a student's marks.

Create a Student class with:

  • __init__(self, name): stores the student's name and initializes marks as unset.
  • setMarks(self, marks): sets the student's marks only if marks is an integer between 0 and 100 inclusive.
    • If the value is invalid, raise an error (ValueError in Python).
  • getMarks(self): returns the currently stored marks.
    • If marks have not been set yet, return -1.

Notes:

  • Use a private attribute for marks.
  • Do not validate the student name.

Examples:

  • Student('Asha'), then setMarks(85), then getMarks() returns 85
  • Student('Asha'), then setMarks(120) raises ValueError