Below are the set of 20 questions which will help to give the basic knowledge of all type of programming on classes and Inheritance in Python. You can visit website pynative.com or https://www.geeksforgeeks.org/.
Steps to run below programs:
- Install Anaconda in your PC/ Laptop.
- Open Spyder.
- Copy and paste below programs.
- Press Run button. You can see the result in Console section at right side.
#1. Program to create a class Employee without any variables and methods
#Answer:
#Solution:
class Employee:
pass
#2. Program to create child class Animal that will inherit all variables and methods of Bird class
#Answer: Parrot 10
#Solution:
class Animal:
def __init__(self, name, age):
self.name = name
self.age = age
class Bird(Animal):
pass
bird_detail = Bird("Parrot", 10)
print(bird_detail.name, bird_detail.age)
#3. Program to determine the type of class, and also if child class is an instance of Parent class
#Answer: Type of class: <class '__main__.Birds'> , Instance of class: True
#Solution:
class Animals:
def __init__(self, name, age):
self.name = name
self.age = age
class Birds(Animals):
pass
bird_details = Birds("Parrot", 10)
print("Type of class: ", type(bird_details))
print("Instance of class: ", isinstance(bird_details, Animals))
#4. Program of Object Oriented Programming, inheritance with default value of variables
#Answer: The color of animal Parrot is white passengers
#Solution:
class Animals:
def __init__ (self, name, age):
self.name = name
self.age = age
def animal_color (self, color):
return f"The color of animal {self.name} is {color} passengers"
class Birds(Animals):
def animal_color(self, color="white"):
return super().animal_color(color="white")
animal_check = Birds("Parrot", 10)
print (animal_check.animal_color())
#5. Write a program with the property that has same value for every object
#Answer: Parrot 10 Black , Snake 8 Black
#Solution:
class Animal1:
color = "Black"
def __init__ (self, name, age):
self.name = name
self.age = age
class Bird1(Animal1):
pass
class Reptiles(Animal1):
pass
animal_check = Bird1("Parrot", 10)
print (animal_check.name, animal_check.age, animal_check.color)
animal_check = Reptiles("Snake", 8)
print (animal_check.name, animal_check.age, animal_check.color)
No comments:
Post a Comment