0% found this document useful (0 votes)
7 views7 pages

Module 5 Python Ques & Answers

The document is a question bank for a Python programming module, covering topics such as pure functions, classes and objects, special methods (__str__ and __init__), copying objects using the copy module, polymorphism, and creating complex numbers. It includes examples and explanations for defining classes, instantiating objects, and implementing methods. Additionally, it provides a sample implementation of an Employee class with methods to set age and salary, and display employee information.

Uploaded by

pritibagi123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

Module 5 Python Ques & Answers

The document is a question bank for a Python programming module, covering topics such as pure functions, classes and objects, special methods (__str__ and __init__), copying objects using the copy module, polymorphism, and creating complex numbers. It includes examples and explanations for defining classes, instantiating objects, and implementing methods. Additionally, it provides a sample implementation of an Employee class with methods to set age and salary, and display employee information.

Uploaded by

pritibagi123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Introduction to Python Programming (BPLCK205B) Module5

Question Bank – Module 5

1. Define pure function. Illustrate with an example Python program. 8M


Pure functions demonstrate a development plan, called prototype and patch, which is a way of
tackling a complex problem by starting with a simple prototype and incrementally dealing
with the complications.
Program ex:
Program to give duration of a movie:

To test this function, we create two Time objects: start contains the start time of a movie.

The result, 10:80:00 is not what we are expecting, here’s an improved version,

Page1
Introduction to Python Programming (BPLCK205B) Module5
2. Define a class and object. Create a class named Rectangle and initialize it with
height = 100, width = 200, and starting point (x = 0, y = 0). Write a program to display
the center point coordinates of the rectangle.

Class: A user-defined or programmer-defined type is called a class. It combines data and


functions into a single entity. A class can contain a set of variables (also called attributes or
member variables) and functions (called methods).

Objects: Object is an instance of a class. Creating a new object (instance) is called


instantiation.

class Rectangle:

>>> center = find_center(box)


>>> print_point(center)
(50, 100)

3. Discuss __str__() and __init__() methods used in class definition.


The __str__ method:
__str__ is a special method, like __init__, that is supposed to return a string representation of
an object.
For example, here is a str method for Time objects:

When you print an object, Python invokes the str method:

The init method:


The init method (short for “initialization”) is a special method that gets invoked when an
object is instantiated. Its full name is __init__ (two underscore characters, followed by init,
and then two more underscores).

Page2
Introduction to Python Programming (BPLCK205B) Module5

It is common for the parameters of __init__ to have the same names as the attributes. The
Statement
self.hour = hour
stores the value of the parameter hour as an attribute of self.
The parameters are optional, so if you call Time with no arguments, you get the default
values.

If you provide one argument, it overrides hour:

If you provide two arguments, they override hour and minute.

4. Explain the concept of copying using copy module with an example.

Aliasing can make a program difficult to read because changes in one place might have
unexpected effects in another place. It is hard to keep track of all the variables that might refer
to a given object.
Copying an object is often an alternative to aliasing. The copy module contains a function
called copy that can duplicate any object:
>>> p1 = Point()
>>> p1.x = 3.0
>>> p1.y = 4.0
>>> import copy
>>> p2 = copy.copy(p1)
p1 and p2 contain the same data, but they are not the same Point.
>>> print_point(p1)
(3, 4)
>>> print_point(p2)
(3, 4)

Page3
Introduction to Python Programming (BPLCK205B) Module5
>>> p1 is p2
False

Fig. Object diagram

5. Briefly explain the printing of objects with example.


We define a class named Time by using a function named print_time:

To call this function, you have to pass a Time object as an argument:

To make print_time a method, all we have to do is move the function definition inside the
class definition.

There are two ways to call print_time. The first (and less common) way is to use
function syntax:

In this use of dot notation, Time is the name of the class, and print_time is the name of the
method. start is passed as a parameter.
The second (and more concise) way is to use method syntax:

6. What is Polymorphism? Demonstrate polymorphism with functions to find histogram


to count the numbers of times each letters appears in a word and in sentence.
The word "polymorphism" means "many forms", and in programming it refers to writing
functions that work correctly for arguments with different types.

Page4
Introduction to Python Programming (BPLCK205B) Module5
Histogram to count the number of times each letter appears in word.

This function also works for lists, tuples, and even dictionaries, as long as the elements of
s are hashable, so they can be used as keys in d.

7. Define a function which takes two objects representing complex numbers and returns
new complex number with a addition of two complex numbers. Define a suitable class
'Complex' to represent the complex number. Develop a program to read N(N2) complex
numbers and to compute the addition of N complex numbers.

class Complex():
def initComplex(self):
self.realPart = int(input("Enter the Real Part: "))
self.imgPart int(input("Enter the Imaginary Part: "))
def display(self):
print(self.realPart,"+",self.imgPart,"i", sep=" ")
def sum(self, c1, c2):
self.realPart=c1.realPart+ c2.realPart
self.imgPart=c1.imgPart + c2.imgPart
c1= Complex()
c2 = Complex()
c3 = Complex()
print("Enter first complex number")
c1.initComplex()
print("First Complex Number: ")
c1.display()
print("Enter second complex number")
c2.initComplex()
print("Second Complex Number: ")
c2.display()
Page5
Introduction to Python Programming (BPLCK205B) Module5
print("Sum of two complex numbers is ")
c3.sum(c1.c2)
c3.display()

8. What is a Class? How to define class in Python? Flow to initiate a class and how the
class members are accessed?
Class: A user-defined or programmer-defined type is called a class. It combines data and functions into
a single entity. A class can contain a set of variables (also called attributes or member variables) and
functions (called methods).

How to define a class:


A user-defined or programmer-defined type is also called a class.
The syntax of the class definition is:
class Point:
"""Represents a point in 2-D space."""
The header indicates that the new class is created with the name Point. The body of the class
is a string that explains what the class is defined for. You can also add variables and methods
inside a class.
Flow to initiate a class and how the class members are accessed:
To create an object also called an instance of Point, you call Point as if it were a function.
Creating a new object is called instantiation, and the object is an instance of the class.
blank = Point()
One can assign values to an instance using dot notation.
blank.x = 3.0
blank.y = 4.0
The value of an element of an object can be displayed as shown below example.
print ('The value of element x is: ',blank.x)
print ('The value of element y is: ',blank.y)
Output
The value of element x is: 3.0
The value of element y is: 4.0

9. Define classes and objects in python. Create a class called Employee and initialize it
with employee id and name. Design method to :
i) Set Age- to assign age to the employee
ii) Set Salary to assign salary to the employee
Display- to display all information of the employee.

Page6
Introduction to Python Programming (BPLCK205B) Module5
class Employee:
def __init__(self, emp_id, name):
self.emp_id = emp_id
self.name = name

def setAge(self, age):


self.age = age

def setSalary(self, salary):


self.salary = salary

def display(self):
print("Employee ID:", self.emp_id)
print("Name:", self.name)
print("Age:", self.age)
print("Salary:", self.salary)

employee1 = Employee("12345", "John Doe")


employee1.setAge(30)
employee1.setSalary(50000)
employee1.display()

Output:
Employee ID: 12345
Name: John Doe
Age: 30
Salary: 50000

Page7

You might also like