Sample Question Bank
Sample Question Bank
Create a Python program to define a class `MyClass` with attributes `name` and `age`, instantiate the class, and use the instance to access these
attributes. (Difficulty: Easy)
class MyClass:
def __init__(self, name, age):
self.name = name
self.age = age
# Access attributes
print(f"Name: {my_object.name}, Age: {my_object.age}")
2. Write a Python program to define a class `Car` with a default constructor that initializes attributes for `brand` and `year`. Include a method
`display_info` to print the car's details. Create an object of the class and call the method to display the information. (Difficulty: Easy)
class Car:
def __init__(self):
self.brand = "Toyota"
self.year = 2020
def display_info(self):
print(f"Brand: {self.brand}, Year: {self.year}")
class Animal:
def __init__(self, name, age):
self.name = name
self.age = age
def display_info(self):
print(f"Name: {self.name}, Age: {self.age}")
4. Write a Python program to demonstrate the functionality of the `Bank` class by creating an instance with attributes `name`, `accno`, and `phno`.
Showcase how to access its public, private, and protected attributes. (Difficulty: Easy)
class Bank:
def __init__(self, name, accno, phno):
self.name = name # Public attribute
self._accno = accno # Protected attribute
self.__phno = phno # Private attribute
def get_private_phno(self):
return self.__phno
@property
def private_phno(self):
return self.__phno
@private_phno.setter
def private_phno(self, value):
if len(str(value))==10 and str(value).isdigit(): # Example condition to control setting
self.__phno = value
else:
raise ValueError("Phno to contain 10 digit")
#Method 3 - Access the private attribute using the decorator @property and @attributename.setter
# Access private attribute via property (getter)
print(f"Phone Number(Using decorator property): {bank_account.private_phno}")
5. Create a Python program to define a class `Student` with private attributes `name` and `regno`. Instantiate an object of the class, access its
private attributes, and update the `name` attribute. (Difficulty: Medium)
class Student:
def __init__(self, name, regno):
self.__name = name # Private attribute
self.__regno = regno
def get_name(self):
return self.__name
# Create an instance
student = Student("Amir", "12345")
6. Write a Python program to define a class `Car` with attributes `make`, `model`, `mileage`, and `price`. Include methods to display the car's details,
start and stop the car, and update its price. Create two car objects and demonstrate the functionality by calling their methods. (Difficulty: Easy)
class Car:
def __init__(self, make, model, mileage, price):
self.make = make
self.model = model
self.mileage = mileage
self.price = price
def display_details(self):
print(f"Make: {self.make}, Model: {self.model}, Mileage: {self.mileage}, Price: {self.price}")
def start(self):
print(f"The {self.make} {self.model} is starting.")
def stop(self):
print(f"The {self.make} {self.model} is stopping.")
# Demonstrate functionality
car1.display_details()
car1.start()
car1.update_price(18000)
car1.stop()
car2.display_details()
7. Create a Python program to define a class `Movie` with attributes `Heroname` and `year`. Instantiate the class and use the instance to access
these attributes. (Difficulty: Easy)
class Movie:
def __init__(self, heroname, year):
self.heroname = heroname
self.year = year
class Student:
def __init__(self):
self._name = "Amir" # Protected attribute
9. Develop a Python program to define a class `College` with a private attribute `__name`, initialized through the constructor. Instantiate an object of
the class with the name `"Presidency"` and access the private attribute using an instance method. (Difficulty: Easy)
class College:
def __init__(self, name):
self.__name = name # Private attribute
def get_name(self):
return self.__name
10. Write a Python program to create a class `Sms` with three attributes (`source1`, `source2`, and `source3`) representing different social media
sources. These should be public, protected, and private, respectively. Initialize them to `"Whatsapp"`, `"Instagram"`, and `"Facebook"`. Create an
instance of the class and print the values of each attribute. (Difficulty: Medium)
class Sms:
def __init__(self):
self.source1 = "Whatsapp" # Public attribute
self._source2 = "Instagram" # Protected attribute
self.__source3 = "Facebook" # Private attribute
def get_private_source(self):
return self.__source3
11. Create a Python program defining a class `Person` with private attributes `__name` and `__age`. Implement getter and setter methods for both,
with validation to ensure `age` is positive. Instantiate the class with `name = "John"` and `age = 25`, print the values using getters, update `age` to
30 using the setter, and print the updated age. (Difficulty: Easy)
class Person:
def __init__(self, name, age):
self.__name = name
self.__age = age
def get_name(self):
return self.__name
def get_age(self):
return self.__age
# Update age
person1.set_age(30)
print(f"Updated Age: {person1.get_age()}")
12. Demonstrate the functionality of the `Bank` class by creating an instance with attributes `name`, `accno`, and `phno`. Show how to access
public, protected, and private attributes, including a method to retrieve the private attribute. (Difficulty: Easy)
class Bank:
def __init__(self, name, accno, phno):
self.name = name # Public attribute
self._accno = accno # Protected attribute
self.__phno = phno # Private attribute
def get_private_phno(self):
return self.__phno
@property
def private_phno(self):
return self.__phno
#Method 3 - Access the private attribute using the decorator @property and @attributename.setter
# Access private attribute via property (getter)
print(f"Phone Number(Using decorator property): {bank1.private_phno}")
Create two objects of the `Employee` class. Use `display_employee_details()` to check the initial attribute values. Then, use
`set_employee_details()` to update the attribute values and call `display_employee_details()` again to verify the updated values. (Difficulty: Easy)
class Person:
def __init__(self, name, age):
self.__name = name
self.__age = age
def get_name(self):
return self.__name
def get_age(self):
return self.__age
# Update age
person1.set_age(30)
print(f"Updated Age: {person1.get_age()}")
Create two objects of the `Student` class. Call `display_student_details()` to check the initial values. Use `set_student_details()` to update the
attributes, then call `display_student_details()` again to verify the updates. (Difficulty: Easy)
class Student:
def __init__(self):
self.student_id = None
self.name = None
self.grade = None
def display_student_details(self):
print(f"ID: {self.student_id}, Name: {self.name}, Grade: {self.grade}")
# Initial values
student1.display_student_details()
student2.display_student_details()
student1.display_student_details()
student2.display_student_details()
Instantiate two objects of the `Person` class. Use `display_person_details()` to check the initial values, then use `set_person_details()` to update the
attributes and verify the updates using `display_person_details()`. (Difficulty: Easy)
class Person:
def __init__(self):
self.name = None
self.age = None
self.address = None
self.phone_number = None
def display_person_details(self):
print(f"Name: {self.name}, Age: {self.age}, Address: {self.address}, Phone: {self.phone_number}")
person1.display_person_details()
person2.display_person_details()
16. Write a Python program to define a `Book` class with the following:
● 1. Attributes: `title` (public), `author` (protected), and `publisher` (private).
● 2. Use a parameterized constructor to initialize these attributes.
● 3. Access and display each attribute, demonstrating how public, protected, and private attributes can be accessed.(Difficulty: Hard)
class Book:
def __init__(self, title, author, publisher):
self.title = title # Public attribute
self._author = author # Protected attribute
self.__publisher = publisher # Private attribute
def get_publisher(self):
return self.__publisher
@property
def private_publisher(self):
return self.__publisher
@private_publisher.setter
def private_publisher(self, value):
if len(str(value))>0 or str(value).isalpha(): # Example condition to control setting
self.__publisher = value
else:
raise ValueError("Publisher to contain only alphabets no special characters and numbers allowed")
#Method 3 - Access the private attribute using the decorator @property and @attributename.setter
# Access private attribute via property (getter)
print(f"Publisher(Using decorator property): {book1.private_publisher}")
17. Create a Python program to define an `Employee` class with the following:
● 1. Attributes: `employee_id`, `name`, `department`, and `salary`.
● 2. A method `calculate_salary()` that calculates the total salary based on:
○ - Required input: basic salary.
○ - Optional inputs: bonus and overtime hours.
○ - Overtime pay is calculated at a rate of INR 750 per hour.
○ - If only the basic salary is provided, the total salary equals the basic salary. If bonus and/or overtime are provided, include them in
the total salary.(Difficulty: Medium)
class Employee:
def __init__(self, employee_id, name, department, salary):
self.employee_id = employee_id
self.name = name
self.department = department
self.salary = salary
18. Write a Python program to define a `Book` class with the following:
● 1. A non-parameterized constructor that initializes attributes to `None`.
● 2. A public method `set_book_details()` to set attribute values using the provided parameters.
● 3. A public method `display_book_details()` to display the book's details.
Create two objects of the `Book` class. Call `display_book_details()` to check the initial values, then use `set_book_details()` to update the attributes
and call `display_book_details()` again to verify the updates. (Difficulty: Easy)
class Book:
def __init__(self):
self.title = None
self.author = None
self.publisher = None
def display_book_details(self):
print(f"Title: {self.title}, Author: {self.author}, Publisher: {self.publisher}")
# Create two book objects
book1 = Book()
book2 = Book()
# Initial values
book1.display_book_details()
book2.display_book_details()
Create multiple book objects and demonstrate the functionality of class and instance methods. (Difficulty: Medium)
class Book:
no_of_books = 0 # Class variable
def display_book_details(self):
print(f"Title: {self.title}, Author: {self.author}, Price: {self.price}")
@classmethod
def get_no_of_books_created(cls):
return cls.no_of_books
book1.display_book_details()
book2.display_book_details()
class Book:
no_of_books = 0 # Class variable
def display_book_details(self):
print(f"Title: {self.title}, Author: {self.author}, Price: {self.price}")
@staticmethod
def validate_price(price):
return price > 0
def check_if_discount_available(self):
return self.price > 999
@classmethod
def get_no_of_books_created(cls):
return cls.no_of_books
book1.display_book_details()
book2.display_book_details()
# Validate prices
print(f"Is the price of book1 valid? {Book.validate_price(book1.price)}")
print(f"Is the price of book2 valid? {Book.validate_price(book2.price)}")