The Function Can Be Defined Using The Def

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

1. Define function in python with it’s syntax.

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

2. Explain the method of reading keyboard input.

Ans:

Example of input() function:


3. How to close a file. Give Example

4. Define the term data abstraction.

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.

5. Enlist various modes in which file can be opened in python

6. Give difference between local and global variable.


7. Explain the need for function in python

8. Explain the use of return statement in python.

9. State use of namespaces in python.

Ans: Namespace is basically a collection of different names. If two names(variables)


are same and are present in the same scope then it will cause name clash. To avoid
such situation we use the keyword namespace. It maintains a name-to-object
mapping where names act as keys and the objects as values.

10. List build in class attributes.


11. List different object oriented features supported by python

Ans: Python's object-oriented programming system supports all four features:


encapsulation, abstraction, inheritance and polymorphism.

12. Write use of any four methods in math module.


13. WAP to count total number of words in given file

14. Explain the concept of method overloading with suitable example.

def product(a, b):


p = a * b
print(p)

def product(a, b, c):


p = a * b*c
print(p)

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

n = int(input("Enter number: "))

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:

n=int(input("Enter the number of lines you want to write:"))


fw=open("first.txt","wt")
for i in range(n):
line=input("Enter line:")
fw.write(line+"\n")
fw.close()

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

Contents of Destination File (second.txt):


python
programming
file
object

18. Write a short note on method overriding

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')

class OOP(Python): #Child Class


def lang(self):
print('Encapsulation.')

d = OOP()
d.lang()

Output:

19. Write how to create exception in python

Ans:

• Python has many built-in exceptions which forces your program to output an
error when something in it goes wrong.

• In Python, users can define such exceptions by creating a new class.

• 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.

You might also like