B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP Python OOP Practice Questions | LiveCodeProgramming

Python OOP Practice Problems

Below are some useful exercises based on Python OOP concepts using class Employee.

नीचे Python OOP concepts पर आधारित कुछ अभ्यास प्रश्न दिए गए हैं जिनमें Employee class का उपयोग किया गया है।

1. Create Employee and Print Details
class Employee:
    def __init__(self, name, age, salary):
        self.name = name
        self.age = age
        self.salary = salary

e1 = Employee('Aryan', 24, 50000)
print(e1.name, e1.age, e1.salary)

Output:

Aryan 24 50000

This creates an object and prints its details.

यह एक object बनाता है और उसकी details print करता है।

2. Add a Method to Show Salary
class Employee:
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary
    def show_salary(self):
        print(f'{self.name} earns ₹{self.salary}')

e2 = Employee('Nikita', 60000)
e2.show_salary()

Output:

Nikita earns ₹60000

Method show_salary() displays the employee's salary.

show_salary() method salary को print करता है।

3. Inheritance Practice
class Employee:
    def __init__(self, name):
        self.name = name

class Manager(Employee):
    def role(self):
        return f'{self.name} is a Manager'

m1 = Manager('Shruti')
print(m1.role())

Output:

Shruti is a Manager

Manager class inherits Employee.

Manager class, Employee से inherit कर रही है।

4. Polymorphism with Method Overriding
class Employee:
    def work(self):
        return 'Working...'

class Developer(Employee):
    def work(self):
        return 'Writing code'

d = Developer()
print(d.work())

Output:

Writing code

Developer overrides work() method.

Developer class work() को override कर रही है।

5. Use of Getters and Setters
class Employee:
    def __init__(self, name):
        self.__name = name

    def get_name(self):
        return self.__name

    def set_name(self, new_name):
        self.__name = new_name

e = Employee('Aman')
e.set_name('Amit')
print(e.get_name())

Output:

Amit

Encapsulation with private variable and getter/setter.

Private variable के साथ getter और setter method।