Professional Documents
Culture Documents
The Function Can Be Defined Using The Def
The Function Can Be Defined Using The Def
The Function Can Be Defined Using The Def
Ans: Function is a block of instructions that performs a specific and well defined task.
The function can be defined using the def
Syntax:
def function_name(parameters):
statements
Ans:
Ans: Data abstraction is a mechanism in which the abstract class is created. The
abstract class is a class in which abstract method is defined. An abstract method is a
method which does not have any implementation or definition.
product(4, 5, 5)
Output: 100
15. Illustrate class inheritance in python with an example.
Multi-level inheritance :
Multi-level inheritance is archived when a derived class inherits another derived class.
There is no limit on the number of levels up to which, the multi-level inheritance is
archived in python.
Multiple inheritance :
Multiple inheritance means we can inherit the properties of multiple classes into one.
16. WAP which will throw exception if the value entered by user is less than 0.
Ans:
class negative(Exception):
pass
try:
if n<0:
raise negative
except negative:
print("Number is less than zero")
17. WAP to read contents of source.txt file and write same content in
destination.txt file
Ans:
fr=open("first.txt","rt")
fw=open("second.txt","wt")
for i in range(n):
line=fr.readline()
fw.write(line)
fr.close()
fw.close()
fr=open("second.txt","rt")
for i in fr:
print(i,end="")
Output:
Enter the number of lines you want to write:4
Enter line:python
Enter line:programming
Enter line:file
Enter line:object
python
programming
file
object
Override means having two methods with the same name but doing different tasks. It
means that one of the methods overrides the other. To override a method in the base
class, we must define a new method with same name and same parameters in the
derived class. Base class's method is called overridden method and the derived class
method is called overriding method.
Overriding is a very important part of OOP since it is the feature that makes
inheritance exploit its full power. Through method overriding a class may "copy"
another class, avoiding duplicated code, and at the same time enhance or customize
part of it.
Input Code:
class Python: #Parent Class
def lang(self):
print('Inheritance')
d = OOP()
d.lang()
Output:
Ans:
• Python has many built-in exceptions which forces your program to output an
error when something in it goes wrong.
• This exception class has to be derived, either directly or indirectly, from Exception
class.
• Most of the built-in exceptions are also derived from this class. With user defined
exception, one can add the desired information to the exception handlers.
20. Write a definition for a class name Circle with attributes center and radius,
where center is a point object and radius is a number instantiate a circle object that
represents a circle with its center at(150,100) and radius 75.