2. Unit I
Programming and Computational
Thinking (PCT-2)
(80 Theory + 70 Practical)
DCSc & Engg, PGDCA,ADCA,MCA.MSc(IT),Mtech(IT),MPhil (Comp. Sci)
Department of Computer Science, Sainik School Amaravathinagar
Cell No: 9431453730
Praveen M Jigajinni
Prepared by
Courtesy CBSE
Class XII
4. CONSTRUCTOR
Constructor is a special type of function that
is called automatically whenever an object of
that class is created.
General format or syntax :
def __init__(self):
# body of the constructor
Once the object is created, you can
make sure that every variable in the object is
correctly initialised by defining
an __init__ method in your class, which
pretty much means init-iate.
8. TYPES OF CONSTRUCTOR
We have two types of constructors in
Python.
1. default constructor – this is the one, which we
have seen in the above example. This
constructor doesn’t accept any arguments.
2. parameterized constructor – constructor with
parameters is known as parameterized
constructor.
13. DESTRUCTOR
Destructors are called when an object
gets destroyed. It’s the polar opposite of the
constructor, which gets called on creation.
Destructor can be very useful for releasing
resources before coming out of the program like
closing files, releasing memories etc
Destructor is defined using __del__() keyword
16. PYTHON GARBAGE COLLECTION
Python deletes unneeded objects (built-in
types or class instances) automatically to free the
memory space.
The process by which Python periodically
reclaims blocks of memory that no longer are in
use is termed as Garbage Collection.
Python's garbage collector runs during
program execution and is triggered when an
object's reference count reaches zero.
20. (c) Write a class PICTURE in Python with following
specifications : 4
Instance Attributes
– Pno # Numeric value
– Category # String value
– Location # Exhibition Location with
# String value
Methods :
– FixLocation() # A method to assign
# Exhibition Location as per
#Category
# as shown in the following table
CBSE QUESTION PAPER - 2015 DELHI
21. – Enter() # A function to allow user to
enter values
# Pno, Category and call
FixLocation() method
– SeeAll() # A function to display all the
data members
Category Location
Classic Amina
Modern Jim Plaq
Antique Ustad Khan
CBSE QUESTION PAPER - 2015 DELHI
23. (c) Write a class PHOTO in Python with following
specifications : 4
Instance Attributes
– Pno # Numeric value
– Category # String Value
– Exhibit # Exhibition Gallery with String value
Methods:
– FixExhibit() #A method to assign
#Exhibition Gallery as per
#Category
#as shown in the following table
CBSE QUESTION PAPER - 2015 DELHI
24. CBSE QUESTION PAPER - 2015 DELHI
Category Exhibit
Antique Zaveri
Modern Johnsen
Classic Terenida
-Register() #A function to allow user to
enter values #Pno, Category
and call FixExhibit() method
– ViewAll() #A function to display all the
data members
26. CBSE QUESTION PAPER - 2016 DELHI
(c) Write a class CITY in Python with following
specifications : 4
Instance Attributes
– Code # Numeric value
– Name # String value
– Pop # Numeric value for Population
– KM # Numeric value
– Density # Numeric value for Population
Density than 12000.
27. CBSE QUESTION PAPER - 2016 DELHI
Methods:
– CalDen() # Method to calculate Density
as Pop/KM
– Record() # Method to allow user to
enter values Code, Name, Pop,
KM and call CalDen() method
– See() # Method to display all the
data members also display a
message “Highly Populated
Area” if the Density is more
than 12000.
28. CLASS TEST
1. What is Constructor? Explain
2. What are the types of constructors?
3. What is destructors? Explain
4. Write a program to illustrate constructors
5. Write a program to illustrate distructors
Class : XII Time: 40 Min
Topic: Const & distruct Max Marks: 20
Each Question carries 4 marks