0% found this document useful (0 votes)
25 views5 pages

Module5 Answers Complete

The document provides answers to a question bank for Modules 4 and 5 of a Python programming course. It covers various topics including class definitions, object instantiation, operator overloading, polymorphism, and methods for manipulating objects. Each question is answered with explanations and code examples to illustrate the concepts discussed.

Uploaded by

mhyder105
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)
25 views5 pages

Module5 Answers Complete

The document provides answers to a question bank for Modules 4 and 5 of a Python programming course. It covers various topics including class definitions, object instantiation, operator overloading, polymorphism, and methods for manipulating objects. Each question is answered with explanations and code examples to illustrate the concepts discussed.

Uploaded by

mhyder105
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/ 5

BPLCK205B - Introduction to Python Programming

Answers to Module 4 and Module 5 Question Bank

Module 5 Answers - Full Set

Q1. Function print_time() that takes a time object

1. Create a class with attributes hour, minute, and second.


2. The function print_time() accepts a Time object.
3. Uses string formatting to print time in hh:mm:ss format.
4. Promotes reusability and readability.
5. Encourages class-object structure for time representation.
6. Example:
class Time:
def __init__(self, h, m, s):
self.hour = h
self.minute = m
self.second = s
def print_time(t):
print(f"{t.hour:02}:{t.minute:02}:{t.second:02}")
t = Time(10, 25, 36)
print_time(t)

Q2. What is a class? How to define and access members?

1. Class is a blueprint for creating objects.


2. Defined using `class` keyword in Python.
3. Members include attributes and methods.
4. Access members using dot notation: obj.attribute or obj.method().
5. `__init__()` initializes object state.
6. Example:
class Person:
def __init__(self, name):
self.name = name
p = Person("Alice")
print(p.name)

Q3. Define pure function and modifier with example

1. Pure function returns output based only on input.


2. No side effects or dependency on external state.
3. Modifier modifies internal state of an object.
4. Pure functions are predictable and testable.
5. Modifier is useful for mutating object attributes.
6. Example:
def square(x): return x * x
BPLCK205B - Introduction to Python Programming

Answers to Module 4 and Module 5 Question Bank

Q4. Explain printing objects with example

1. Printing object normally gives memory location.


2. Use `__str__()` method to define custom string.
3. Called automatically when using print().
4. Makes object data human-readable.
5. Helps debug and log objects.
6. Example:
class Car:
def __str__(self):
return "Car object"
c = Car()
print(c)

Q5. Discuss operator overloading and polymorphism

1. Operator overloading redefines operators for class objects.


2. Done using magic methods like __add__, __sub__.
3. Polymorphism allows methods to behave differently based on input.
4. Enhances code flexibility and readability.
5. Example:
class Vector:
def __add__(self, other):
return Vector(self.x + other.x, self.y + other.y)
6. Both concepts allow object-based customization.

Q6. Write deck methods to add, remove, shuffle and sort the card

1. Define Deck class with a list to store cards.


2. Method to add: append card to list.
3. Method to remove: use remove() or pop().
4. Shuffle: use random.shuffle().
5. Sort: use sort() based on rank.
6. Example:
import random
class Deck:
def __init__(self):
self.cards = []
def add(self, card): self.cards.append(card)
def remove(self, card): self.cards.remove(card)
def shuffle(self): random.shuffle(self.cards)
def sort(self): self.cards.sort()

Q7. Explain methods __init__ and __str__ with code


BPLCK205B - Introduction to Python Programming

Answers to Module 4 and Module 5 Question Bank

1. __init__ is a constructor called on object creation.


2. Initializes object attributes.
3. __str__ returns a string when object is printed.
4. __str__ is useful for debugging.
5. They are both special (magic) methods.
6. Example:
class Person:
def __init__(self, name): self.name = name
def __str__(self): return f"Name: {self.name}"

Q8. Define function to add complex numbers using class

1. Define class Complex with real and imag attributes.


2. Overload __add__ to add two complex numbers.
3. Create a list of N Complex objects.
4. Use loop to add all.
5. Return the result using __str__.
6. Example:
class Complex:
def __init__(self, r, i): self.r = r; self.i = i
def __add__(self, o): return Complex(self.r+o.r, self.i+o.i)

Q9. Explain class definition, instantiation, copy module

1. Class defined with `class` keyword.


2. Instantiation means creating object of class.
3. Instance can be passed to functions.
4. Instance can be returned from function.
5. copy.copy() creates shallow copy.
6. copy.deepcopy() copies all nested objects.

Q10. Define class Rectangle and print center coordinates

1. Create class Rectangle with height, width, start point.


2. Start point is (x=0, y=0).
3. Center = (width/2, height/2).
4. Use __init__ to set values.
5. Method to compute center.
6. Example:
class Rectangle:
def __init__(self): self.h=100; self.w=200; self.x=0; self.y=0
def center(self): return (self.w//2, self.h//2)
BPLCK205B - Introduction to Python Programming

Answers to Module 4 and Module 5 Question Bank

Q11. Explain polymorphism, inheritance and overloading

1. Polymorphism: same interface, different implementation.


2. Inheritance: child class inherits from parent.
3. Overloading: same operator behaves differently.
4. Promotes code reuse and flexibility.
5. Example:
class A: def greet(self): print("Hi")
class B(A): def greet(self): print("Hello")
6. Demonstrates function overriding.

Q12. What do you mean by class, object, attributes?

1. Class: blueprint or template for objects.


2. Object: instance of class.
3. Attributes: variables in a class.
4. class Car: color, speed are attributes.
5. Object has methods (functions) and attributes.
6. Example:
class Student:
def __init__(self): self.name = 'Ali'

Q13. Program based on object diagram

1. Use constructor to initialize attributes.


2. Create multiple objects from class.
3. Use a print method to display.
4. Store attributes like name, age etc.
5. Useful for class relationship practice.
6. Hint: Use init + print method for attributes.

Q14. Program using class Student to compute marks

1. Use list to store subject marks.


2. Class attributes: name, USN, marks list.
3. getMarks() reads 3 marks from input.
4. display() shows name, USN, total, percentage.
5. init() initializes name, usn, marks.
6. Example code included in main answer notes.

Q15. Explain isinstance, copy.copy(), copy.deepcopy()


BPLCK205B - Introduction to Python Programming

Answers to Module 4 and Module 5 Question Bank

1. isinstance(): checks if object is instance of a class.


2. copy.copy(): creates shallow copy.
3. copy.deepcopy(): deep copy including nested objects.
4. Shallow = shared inner list.
5. Deep = new inner list too.
6. Example:
from copy import copy, deepcopy

You might also like