python mid -1 answer's
1.A) Define class, In what way how can we implement multipath inheritance.? (5marks)
class is a user-defined blueprint or prototype from which objects are created. Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to it for maintaining its state. Class instances can also have methods (defined by their class) for modifying their state.
2)Describe encapsulation with example ? (5marks)
Encapsulation is one of the fundamental concepts in object-oriented programming (OOP). It describes the idea of wrapping data and the methods that work on data within one unit. This puts restrictions on accessing variables and methods directly and can prevent the accidental modification of data. To prevent accidental change, an object’s variable can only be changed by an object’s method. Those types of variables are known as private variable.
A class is an example of encapsulation as it encapsulates all the data that is member functions, variables, etc.
example:
class
Base:
def
__init__(
self
):
self
._a
=
2
class
Derived(Base):
def
__init__(
self
):
Base.__init__(
self
)
print
(
"Calling protected member of base class: "
)
print
(
self
._a)
obj1
=
Derived()
obj2
=
Base()
print
(obj2.a)
3)Explain is constructer method
Constructors are generally used for instantiating an object. The task of constructors is to initialize(assign values) to the data members of the class when an object of the class is created. In Python the __init__() method is called the constructor and is always called when an object is created.
Syntax of constructor declaration :
def __init__(self):
# body of the constructor
7)Explain Exception Handling (try/except)in python with example (5m)
The try
block lets you test a block of code for errors.
The except
block lets you handle the error.
The finally
block lets you execute code, regardless of the result of the try- and except blocks.
Exception Handling
When an error occurs, or exception as we call it, Python will normally stop and generate an error message.
These exceptions can be handled using the try
statement:
Example
The try
block will generate an exception, because x
is not defined:
try:
print(x)
except:
print("An exception occurred")
ouput:
An exception occurred
8)Describe self and super keyword ?
self :
The reason you need to use self. is because Python does not use the @ syntax to refer to instance attributes.: the first parameter of methods is the instance the method is called on
super()
.The super()
builtin returns a proxy object (temporary object of the superclass) that allows us to access methods of the base class.
9)What is ZeroDivisionError exception in python?
the error when we divided a number by zero is called zero division error
5)How to import modules in Python? Explain with examples
When the import is used, it searches for the module initially in the local scope by calling __import__() function. The value returned by the function is then reflected in the output of the initial code.
example:
import
math
print
(math.pi)
Output:
3.141592653589793
6)How is class attributes and instance (object) attributes accessed?
Class attributes
Class attributes belong to the class itself they will be shared by all the instances. Such attributes are defined in the class body parts usually at the top, for legibility.
class sampleclass:
count = 0
def increase( self ):
sampleclass.count + = 1
s1 = sampleclass()
s1.increase()
print (s1.count)
s2 = sampleclass()
s2.increase()
print (s2.count)
print (sampleclass.count)
|
Output:
1
2
2
Instance Attributes
Unlike class attributes, instance attributes are not shared by objects. Every object has its own copy of the instance attribute (In case of class attributes all object refer to single copy).
To list the attributes of an instance/object, we have two functions:-
1. vars()– This function displays the attribute of an instance in the form of an dictionary.
2. dir()– This function displays more attributes than vars function,as it is not limited to instance. It displays the class attributes as well. It also displays the attributes of its ancestor classes.
class emp:
def __init__( self ):
self .name = 'xyz'
self .salary = 4000
def show( self ):
print ( self .name)
print ( self .salary)
e1 = emp()
print ( "Dictionary form :" , vars (e1))
print ( dir (e1))
|
Output :
Dictionary form :{'salary': 4000, 'name': 'xyz'}
['__doc__', '__init__', '__module__', 'name', 'salary', 'show']
4)What is exception ? Explain NameError ?
An exception is an event, which occurs during the execution of a program that disrupts the normal flow of the program's instructions. In general, when a Python script encounters a situation that it cannot cope with, it raises an exception. An exception is a Python object that represents an error.
When a Python script raises an exception, it must either handle the exception immediately otherwise it terminates and quits.\