Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

101.

What is inheritance and access control?

Answer»

When you declare a derived class, a visibility mode can precede each base class in the base list of the derived class. This does not alter the access attributes of the individual members of a base class , but allows the derived class to access the members of a base class with restriction.

Classes can be derived using any of the three visibility mode:

1. In a public base class, public and protected members of the base class remain public and protected members of the derived class.

2. In a protected base class, public and protected members of the base class are protected members of the derived class.

3. In a private base class, public and protected members of the base class become private members of the derived class.

4. In all these cases, private members of the base class remain private and cannot be used by the derived class. However it can be indirectly accessed by the derived class using the public or protected member function of the base class since they have the access privilege for the private members of the base class.

102.

Write the derived class using any of the three visibility mode.

Answer»

Classes can be derived using any of the three visibility mode:

1. In a public base class, public and protected members of the base class remain public and protected members of the derived class.

2. In a protected base class, public and protected members of the base class are protected members of the derived class.

3. In a private base class, public and protected members of the base class become private members of the derived class.

103.

Pay roll information system: Declare the base class ’employee’ with employee’s number, name, designation, address, phone number. Define and declare the function getdata() and putdata() to get the employee’s details and print employee’s details. Declare the derived class salary with basic pay, DA, HRA, Gross pay, PF, Income tax and Net pay. Declare and define the function getdata() to call getdata() and get the basic pay. Define the function calculate() to find the net pay. Define the function display() to call putdata() and display salary details.Create the derived class object. Read the number of employees. Call the function getdata() and calculate() to each employees. Call the display() function.

Answer»

class employee (object):

# constructor to create an object

def init (self):

self.eno=0

self.name=‘ ’

self.designation=‘ ’

self.address = ‘ ’

self.phonenumber=0

def getdata(self):

self.eno=input(“Enter Employee Number”)

self.name=raw_input(“Enter Employee Name’)

self.designation=raw_input(“Enter Employee Designation”)

self.address = raw_input(“Enter Employee Address”)

self.phonenumber=input(“Enter Employee Phone Number”)

def putdata (self):

print “Employee Number”,self.eno

print “Employee Name”,self.name

print “Employee Designation”,self.designation

print “Employee Address”,self.address

print “Employee Phone Number”,self phonenumber

class salary (employee):

# Salary details

def_init_(self):

super(salaryself)._init_()

self.basic=0

self.DA=0

self.HRA=0

self.Gross=0

self.PF=0

self.tax=0

self.netpay=0

def getdatal(self): self.getdata()

self.basic=float(raw_input(“Enter the basic pay’’))

self.DA=float(raw_input(“Enter the DA”))

self.HRA=float(raw_input(“Enter the HRA))

self.PF = float(raw_input(“Enter the PF”))

def calculate(self):

self.Gross= self.basic+ self.FîRA + self.DA

if .self. Gross < 100000:

self.tax=0

elif self.Gross < 500000:

self.tax=self.Gross*0.10

elif self.Gross < 1000000:

self.tax=self.Gross*0.15

else:

self.tax=self.Gross*0.20

self.netpay= self.Gross – self.PF – self.tax

def display(self):

self.putdataO

print “Gross Income”,self.Gross

print “Tax “,self.tax

print “Net Income”,self.netpay

salaryobj =salary()

num = int(raw_input(“Enter no. of employees:-”))

for i in range(0,num):

salaryobj.getdatal()

salaryobj.calculate()

salaryobj.display()

104.

Define a class employee in Python with the given specifications:Instance variables:Employee number, name Methods:Getdata()- To input employee number and namePrintdata()- To display employee number and nameDefine another class payroll, which is derived from employeeInstance variable Methods:Inputdata() – To call Getdata() and input salary.Outdata() – To call Printdata() and to display salary.Define another class leave, which is derived from payroll.Instance variable No of days Methods:acceptdata() – To call Inputdata() and input no of days.showdata() – To call Outdata() and to display no of days.Implement the above program in python.

Answer»

class Employee(object):

# constructor to create an object

def_init_(self):

self.eno=0

self.name=“”

def Getdata(self):

self.eno=input(“Enter the Employee no:”)

self.name= raw_input(“Enter the Employee Name:”)

def Printdata(self):

print “Employee Number”,

self.eno print “Employee Name”,

self.name

class payroll(Employee):

def_init_(self):

super(payroll,self). _init_()

self.salary=0

def Inputdata(self): self.GetdataQ

self.salary=float(raw_input(“Enter the salary:”))

def Outdata(self): self.Printdata()

print “Salary is”,self.salary

class leave(payroll):

def_init_(self):

super(leave,self)._init_()

self.Noofdays=0 def acceptdata(self):

self.Inputdata()

self.Noofdays =input(“Enter leave days”)

def showdata(self): self.Outdata()

print “No. of leave days”,

self.Noofdays leaveobj = leave()

leaveobj. acceptdataO

leaveobj.showdata()