×
NOTE!
Click on MENU to Browse between Subjects...
Advertisement
17CS664 - PYTHON APPLICATION PROGRAMMING
Answer Script for Module 4
Solved Previous Year Question Paper
CBCS SCHEME
PYTHON APPLICATION PROGRAMMING
[As per Choice Based Credit System (CBCS) scheme]
(Effective from the academic year 2017 - 2018)
SEMESTER - VI
Subject Code 17CS664
IA Marks 40
Number of Lecture Hours/Week 3
Exam Marks 60
Advertisement
These Questions are being framed for helping the students in the "FINAL Exams" Only
(Remember for Internals the Question Paper is set by your respective teachers).
Questions may be repeated, just to show students how VTU can frame Questions.
- ADMIN
×
CLICK ON THE QUESTIONS TO VIEW ANSWER
ANSWER :
Type-based dispatch:
A programming pattern that checks the type of an operand and invokes
different functions for different types.
This can be illustrated using a Program. Consider a problem of creating a class called Time, adding two Time objects, adding a number to Time object etc. that we had considered in previous section. Here is a complete program with more of OOP concepts.

Fig 6.1 Demonstration of Type-based Dispatched & Overloading

Fig 6.2 Output of Demonstration of Type-based Dispatched & Overloading
Working of above program is explained hereunder -
The class Time has __init__() method for initialization of instance attributes hour, min and sec. The default values of all these are being zero
The method time_to_int() is used convert a Time object (hours, min and sec) into single integer representing time in number of seconds.
The method int_to_time() is written to convert the argument seconds into
time object in the form of hours, min and sec. The built-in method divmod()
gives the quotient as well as remainder
after dividing first argument by second argument given to it.
Special method __eq__() is for overloading equality (==) operator. We can say one Time object is equal to the other Time object if underlying hours, minutes and seconds are equal respectively. Thus, we are comparing these instance attributes individually and returning either True of False.
When we try to perform addition, there are 3 cases -
i. Adding two time objects like T3=T1+T2.
ii. Adding integer to Time object like T4=T1+75
iii. Adding Time object to an integer like T5=130+T1
Each of these cases requires different logic. When first two cases are considered, the first argument will be T1 and hence self will be created and passed to __add__() method. Inside this method, we will check the type of second argument using isinstance() method. If the second argument is Time object, then we call addTime() method. In this method, we will first convert both Time objects to integer (seconds) and then the resulting sum into Time object again. So, we make use time_to_int() and int_to_time() here. When the 2nd argument is an integer,
it is obvious that it is number of seconds. Hence, we need to call increment() method.
Thus, based on the type of argument received in a method, we take
appropriate action. This is known as type-based dispatch.
In the 3rd case like T5=130+T1, Python tries to convert first
argument 130 into self, which is not possible. Hence, there will be an
error. This indicates that for Python, T1+5 is not same as 5+T1
(Commutative law doesn't hold good!!). To avoid the possible error, we need
to implement right-side addition
method
__radd__(). Inside this method, we can call overloaded method __add__().
The beauty of Python lies in surprising the programmer with more
facilities!! As we have implemented __add__() method (that is, overloading
of + operator), the builtin sum() will is capable of adding multiple
objects given in a sequence. This is due toPolymorphism
in Python .
Consider a list containing Time objects, and
then call
sum() on that list as -
T6=sum([T1,T2,T3,T4])
The sum() internally calls __add__() method multiple times and hence gives the appropriate result. Note down the square-brackets used to combine Time objects as a list and thenc passing it to sum().
Thus, the program given here depicts many features of OOP concepts.
Advertisement
ANSWER :
7.1 Class:
Refer 1st Question & Answer or Click Here
7.2 Instantiate of a Class:
Refer 4th Question & Answer or Click Here
IN-Short:
Class:
A programmer-defined type. A class definition creates a new class object.
Class object:
An object that contains information about a programmer-defined type. The
class object can be used to create instances of the type.
Instance:
An object that belongs to a class.
Instantiate:
To create a new object.
ANSWER :
Class Variables:
Declared inside the class definition (but outside any of the instance
methods). They are not tied to any particular object of the class, hence
shared across all the objects of the class. Modifying a class variable
affects all objects instance at the same time.
Instance Variable:
Declared inside the constructor method of class (the __init__ method). They
are tied to the particular object instance of the class, hence the contents
of an instance variable are completely independent from one object instance
to the other.
Advertisement
ANSWER :

Fig 9.1 Demonstrating a Program to calculate Age & How many Days Left for Upcoming Birthday

Fig 9.2 Output of Demonstrating a Program to calculate Age & How many Days Left for Upcoming Birthday
ANSWER :


×
NOTE: Each Page Provides only 5 Questions & Answer
Below Page NAVIGATION Links are Provided...
All the Questions on Question Bank Is SOLVED
Below Page NAVIGATION Links are Provided...
All the Questions on Question Bank Is SOLVED
Advertisement
lIKE OUR CONTENT SUPPORT US BY FOLLOWING US ON INSTAGRAM : @futurevisionbie
For immediate Notification Join the Telegram Channel
×
SUGGESTION: SHARE WITH ALL THE STUDENTS AND FRIENDS -ADMIN

MENU