0% found this document useful (0 votes)
10 views

Namma Kalvi 12th Computer Science Unit 3 Sura Guide Em (1)

The document covers Unit III of a computer science curriculum, focusing on lists, tuples, sets, and dictionaries in Python. It includes definitions, functions, and examples related to these data structures, along with evaluation questions and answers. The content is structured into parts, providing both theoretical explanations and practical coding examples.

Uploaded by

Gokul Mathivanan
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)
10 views

Namma Kalvi 12th Computer Science Unit 3 Sura Guide Em (1)

The document covers Unit III of a computer science curriculum, focusing on lists, tuples, sets, and dictionaries in Python. It includes definitions, functions, and examples related to these data structures, along with evaluation questions and answers. The content is structured into parts, providing both theoretical explanations and practical coding examples.

Uploaded by

Gokul Mathivanan
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/ 11

www.surabooks.

com Namma Kalvi


This material only for sample www.surabooks.com

-

UNIT III MODULARITY AND OOPS
www.nammakalvi.org
 

Chapter LISTS, TUPLES, SETS

9 AND DICTIONARY
CHAPTER SNAPSHOT
9.1 Introduction to List
9.2 Tuples

ns
9.3 Sets
9.4 Dictionaries

Evaluation
io
at
5. If List=[17,23,41,10] then List.append(32) will
Part - I
ic
result
Choose the best answer  (1 mark) (a) [32,17,23,41,10] (b) [17,23,41,10,32]
bl

1. Pick odd one in connection with collection (c) [10,17,23,32,41] (d) [41,32,23,17,10]
Pu

data type  [Ans. (b) [17,23,41,10,32]]


(a) List (b) Tuple 6. Which of the following Python function can be
(c) Dictionary (d) Loop used to add more than one element within an
 [Ans. (d) Loop] existing list?
ra

(a) append() (b) append_more()


2. Let list1=[2,4,6,8,10], then print(List1[-2])
(c) extend() (d) more()
Su

will result in
 [Ans. (c) extend()]
(a) 10 (b) 8 (c) 4 (d) 6
 [Ans. (b) 8] 7. What will be the result of the following Python
code?
3. Which of the following function is used to
count the number of elements in a list? S=[x**2 for x in range(5)]
(a) count() (b) find() print(S)
(c) len() (d) index() (a) [0,1,2,4,5] (b) [0,1,4,9,16]
 [Ans. (c) len()] (c) [0,1,4,9,16,25] (d) [1,4,9,16,25]
 [Ans. (b) [0,1,4,9,16]]
4. If List=[10,20,30,40,50] then List[2]=35 will
result 8. What is the use of type() function in python?
(a) [35,10,20,30,40,50] (a) To create a Tuple
(b) [10,20,30,40,50,35] (b) To know the type of an element in tuple
(c) [10,20,35,40,50] (c) To know the data type of python object
(d) [10,35,30,40,50][Ans. (c) [10,20,35,40,50]] (d) To create a list.
 [Ans. (c) To know the data type of python
[109] object]

[email protected] Ph: 81242 01000 / 812430 1000 / 96001 75757


Unit III - Chapter 9

9.
www.surabooks.com

Sura’s
This material only for sample

➠ XII Std - Computer Science


Which of the following statement is not
correct?
3.
  www.surabooks.com

www.nammakalvi.org
What will be the value of x in following python
code?
(a) A list is mutable List1=[2,4,6[1,3,5]]
(b) A tuple is immutable x=len(List1)
(c) The append() function is used to add an Ans. x = 4.
element 4. Differentiate del with remove( ) function of
(d) The extend() function is used in tuple to add List.
elements in a list Ans.
 [Ans. (d) The extend() function is used in del function remove ( )
 tuple to add elements in a list]
function
10. Let setA={3,6,9}, setB={1,3,9}. What will be (i) del statement is remove( ) function
the result of the following snippet? used to delete is used to delete
print(setA|setB) known elements elements of a
list if its index is
(a) {3,6,9,1,3,9} (b) {3,9} whereas unknown.
(c) {1} (d) {1,3,6,9}

ns
(ii) The del statement The remove ()
 [Ans. (d) {1,3,6,9}] function can be
can also be used to
11. Which of the following set operation includes delete entire list. used to delete one

io
or more elements
all the elements that are in two sets but not the if the index value
one that are common to two sets? at is not known.
(a) Symmetric difference (b) Difference
5. Write the syntax of creating a Tuple with n
(c) Intersection (d) Union
ic
number of elements.
 [Ans. (a) Symmetric difference]
Ans. Syntax:
bl

12. The keys in Python, dictionary is specified by Tuple_Name = tuple( [list elements] )
(a) = (b) ; (c) + (d) : 6. What is Set in Python?
Pu

 [Ans. (d) :] Ans. (i) In Python, a Set is another type of collection


Part - II data type. A Set is a mutable and an
unordered collection of elements without
Answer the following questions
ra

duplicates.
 (2 marks) (ii) That means the elements within a set cannot
be repeated. This feature used to include
Su

1. What is List in Python? membership testing and eliminating


Ans. (i) A list in Python is known as a “sequence duplicate elements.
data type” like strings. It is an ordered
collection of values enclosed within square Part - III
brackets []. Answer the following questions
(ii) Each value of a list is called as element.  (3 marks)
It can be of any type such as numbers,
characters, strings and even the nested lists
1. What are the advantages of Tuples over a list?
as well.
Ans. (i) The elements of a list are changeable
2. How will you access the list elements in reverse
(mutable) whereas the elements of a tuple
order?
are unchangeable (immutable), this is the
Ans. Python enables reverse or negative indexing
key difference between tuples and list.
for the list elements. Thus, python lists index in
opposite order. The python sets -1 as the index (ii) The elements of a list are enclosed within
value for the last element in list and -2 for the square brackets. But, the elements of a tuple
preceding element and so on. This is called as are enclosed by parenthesis.
Reverse Indexing. (iii) Iterating tuples is faster than list.

110
[email protected] Ph: 81242 01000 / 812430 1000 / 96001 75757
2.

www.surabooks.com

Write a short note about sort( ).


Ans. Sort ( ) : Sorts the element in list
This material only for sample

Sura’s
6.
www.surabooks.com

➠ XII Std - Computer Science


What are the difference between List and
Dictionary?

Lists, Tuples, Sets and Dictionary


Both arguments are optional Ans.
(i)  If reverse is set as True, list sorting is in List Dictionary
descending order. (i) List is an ordered But, a dictionary is
(ii)  Ascending is default. set of elements a data structure that
is used for matching
(iii) Key=myFunc; “myFunc” - the name of the
one element (Key)
user defined function that specifies the with another (Value).
sorting criteria.
(iv) sort( ) will affect the original list. (ii) 
The index values The remove ()
Example : can be used to function can be used
List.sort(reverse=True|False, key=myFunc) access a particular to delete one or more
element. elements if the index
MyList=['Thilothamma', 'Tharani', 'Anitha',
value is not known.

ns
 'SaiSree', 'Lavanya']
(iii) Lists are used to dictionary is used
MyList.sort( ) look up a value to take one value
print(MyList) whereas. and look up another

io
MyList.sort(reverse=True) at value.
print(MyList)
Part - IV
Output:
Answer the following questions
ic
['Anitha', 'Lavanya', 'SaiSree', 'Tharani',
 (5 marks)
bl

'Thilothamma']
['Thilothamma', 'Tharani', 'SaiSree', 'Lavanya',
Pu

1. What the different ways to insert an element in


 'Anitha'] a list. Explain with suitable example.
3. What will be the output of the following code? Ans. Adding more elements in a list :
list = [2**x for x in range(5)] (i) In Python, append( ) function is used to
ra

print(list) add a single element and extend( ) function


Ans. Output : [1, 2, 4, 8, 16] is used to add more than one element to an
Su

existing list.
4. Explain the difference between del and clear( ) (ii) Syntax:
in dictionary with an example. List.append (element to be added)
Ans. In Python dictionary, del keyword is used to List.extend ( [elements to be added])
delete a particular element. The clear( ) function (iii) In extend( ) function, multiple elements
should be specified within square bracket
is used to delete all the elements in a dictionary.
as arguments of the function.
5. List out the set operations supported by (iv) Example :
python. >>> Mylist=[34, 45, 48]
Ans. The python supports the set operations such as >>> Mylist.append(90)
Union, Intersection, difference and Symmetric >>> print(Mylist)
difference. [34, 45, 48, 90]

111
[email protected] Ph: 81242 01000 / 812430 1000 / 96001 75757
Unit III - Chapter 9
www.surabooks.com

Sura’s
(v)
➠ XII Std - Computer Science
In the above example, Mylist is created
with three elements. Through >>> Mylist.
 
This material only for sample

Output :
2
www.surabooks.com

www.nammakalvi.org

append(90) statement, an additional value 4


90 is included with the existing list as last 6
element, following print statement shows 8
all the elements within the list MyList. 10
Inserting elements in a list : Creating a list with series of values :
(i) Append( ) function in Python is used to (i) Using the range( ) function, a list can be
add more elements in a list. But, it includes created with series of values. To convert
the result of range( ) function into list, one
elements at the end of a list. To include an more function called list is needed( ). The
element at your desired position, use insert list( ). function makes the result of range( )
( ) function. The insert( ) function is used as a list.
to insert an element at any position of a list. (ii) Syntax: List_Varibale = list ( range ( ) )
(ii) Syntax: (iii) Example :
List.insert (position index, element) >>> Even_List = list(range(2,11,2))

ns
(iii) Example : >>> print(Even_List)
>>> MyList=[34,98,47,'Kannan', [2, 4, 6, 8, 10]
 'Gowrisankar', 'Lenin', 'Sreenivasan' ]

io
(iv) In the above code, list( ) function takes the
>>> print(MyList) at result of range( ) as Even_List elements.
[34, 98, 47, 'Kannan', 'Gowrisankar',  Thus, Even_List list has the elements of first
 'Lenin', 'Sreenivasan'] five even numbers.
ic
>>> MyList.insert(3, 'Ramakrishnan') Similarly, we can create any series of values
>>> print(MyList) using range( ) function. The following
bl

[34, 98, 47, 'Ramakrishnan', 'Kannan',  example explains how to create a list with
squares of first 10 natural numbers.
 'Gowrisankar', 'Lenin', 'Sreenivasan']
Pu

Example : Generating squares of first 10


2. What is the purpose of range( )? Explain with natural numbers
an example. squares = [ ]
Ans. The range( ) is a function used to generate a series
ra

for x in range(1,11):
of values in Python. Using range( ) function, you s = x ** 2
can create list with series of values. The range( )
Su

squares.append(s)
function has three arguments.
print (squares)
Syntax of range ( ) function:
range (start value, end value, step value) 3. What is nested tuple? Explain with an example.
where, Ans. Nested Tuples :
(i) start value – beginning value of series. (i) In Python, a tuple can be defined inside
Zero is the default beginning value. another tuple; called Nested tuple. In a
(ii) end value – upper limit of series. Python nested tuple, each tuple is considered as
takes the ending value as upper limit – 1. an element. The for loop will be useful to
access all the elements in a nested tuple.
(iii) step value – It is an optional argument,
which is used to generate different interval (ii) Example :
of values. Toppers = (("Vinodini", "XII-F", 98.7), 
Generating first 10 even numbers :  ("Soundarya", "XII-H", 97.5),
for x in range (2, 11, 2): ("Tharani", "XII-F", 95.3), ("Saisri",
print(x)  "XII-G", 93.8))
for i in Toppers:
print(i)

112
[email protected] Ph: 81242 01000 / 812430 1000 / 96001 75757

www.surabooks.com

(iii) Output :
('Vinodini', 'XII-F', 98.7)
This material only for sample

Sura’s
www.surabooks.com

➠ XII Std - Computer Science


(ii) The operator & is used to intersect two sets
in python. The function intersection( ) is

Lists, Tuples, Sets and Dictionary


('Soundarya', 'XII-H', 97.5) also used to intersect two sets in python.
('Tharani', 'XII-F', 95.3) (iii) Example : Program to insect two sets using
('Saisri', 'XII-G', 93.8) intersection operator
set_A={'A', 2, 4, 'D'}
4. Explain the different set operations supported
by python with suitable example. set_B={'A', 'B', 'C', 'D'}
Ans. The python supports the set operations such as
print(set_A & set_B)
Union, Intersection, difference and Symmetric Output :
difference. {'A', 'D'}
Union: It includes all elements from two or (iv) Example : Program to insect two sets using
more sets intersection function
set_A={'A', 2, 4, 'D'}
Set A Set B
set_B={'A', 'B', 'C', 'D'}
print(set_A.intersection(set_B))

ns
Output :
{'A', 'D'}

io
Difference :
(i)In python, the operator | is used to union of (i) It includes all elements that are in fi rst set
two sets. The function union( ) is also used
at (say set A) but not in the second set (say set B)
to join two sets in python.
ic
(ii) Example : Program to Join (Union) two Set A Set B
bl

sets using union operator


set_A={2,4,6,8}
Pu

set_B={'A', 'B', 'C', 'D'}


U_set=set_A|set_B
print(U_set)
Output :
ra

(ii) The minus (–) operator is used to difference


{2, 4, 6, 8, 'A', 'D', 'C', 'B'} set operation in python. The function
(iii) Example : Program to Join (Union) two difference( ) is also used to difference
Su

sets using union function operation.


set_A={2,4,6,8} (iii) Example : Program to difference of two
set_B={'A', 'B', 'C', 'D'} sets using minus operator
set_U=set_A.union(set_B) set_A={'A', 2, 4, 'D'}
print(set_U)
set_B={'A', 'B', 'C', 'D'}
Output : {'D', 2, 4, 6, 8, 'B', 'C', 'A'}
Intersection : print(set_A - set_B)
(i) It includes the common elements in two Output : {2, 4}
sets (iv) Example : Program to difference of two
sets using difference function
Set A Set B
set_A={'A', 2, 4, 'D'}
set_B={'A', 'B', 'C', 'D'}
print(set_A.diff erence(set_B))
Output : {2, 4}

113
[email protected] Ph: 81242 01000 / 812430 1000 / 96001 75757
Unit III - Chapter 9
www.surabooks.com

Sura’s ➠ XII Std - Computer Science


Symmetric difference :
(i) It includes all the elements that are in two
2.
 
This material only for sample www.surabooks.com

www.nammakalvi.org
Write a program that prints the maximum
value in a Tuple.
sets (say sets A and B) but not the one that Ans. tuple 1 = (5, 17, 15, 20, 7, 3)
are common to two sets. print (“Maximum value”, max (tuple 1))
3. Write a program that finds the sum of all the
Set A Set B
numbers in a Tuples using while loop.
Ans. li = [15, 25, 17]
s=0
i=0
while i < len (i):
(ii) The caret (^) operator is used to symmetric s = s + li [i]
difference set operation in python. The i+=1

ns
function symmetric_difference( ) is also print (s)
used to do the same operation.
4. Write a program that finds sum of all even
(iii) Example : Program to symmetric

io
numbers in a list.
difference of two sets using caret operator
Ans. list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
set_A={'A', 2, 4, 'D'}
at e=0
set_B={'A', 'B', 'C', 'D'} for i in list:
ic
print(set_A ^ set_B) if i % 2 = = 0:
bl

Output : e=e+i
{2, 4, 'B', 'C'} print (“Sum of even number”, e)
Pu

(iv) Example : Program to difference of two


5. Write a program that reverse a list using a
sets using symmetric difference function
loop.
set_A={'A', 2, 4, 'D'}
ra

Ans. list = [1, 2, 3, 4, 5]


set_B={'A', 'B', 'C', 'D'}
i=–1
Su

print(set_A.symmetric_difference(set_B))
while i > = – 5:
Output :
print (list [i])
{2, 4, 'B', 'C'}
i=i+–1
Hands on Experience 6. Write a program to insert a value in a list at the
specified location.
1. Write a program to remove duplicates from a
list. Ans. list = [1, 2, 4, 5]
Ans. mylist = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5] list insert (2, 3)
r = [] print (list)
for i in mylist:
if i not in res:
res. append (i)
print (res)

114
[email protected] Ph: 81242 01000 / 812430 1000 / 96001 75757
7.

www.surabooks.com This material only for sample

Write a program that creates a list of numbers


from 1 to 50 that are either divisible by 3 or
Sura’s
9.
www.surabooks.com

➠ XII Std - Computer Science


Write a program that counts the number of
times a value appears in the list. Use a loop to

Lists, Tuples, Sets and Dictionary


divisible by 6. do the same.
Ans. a = [ ] Ans. list = [8, 6, 8, 10, 8, 20, 10, 8, 8]
for i in range (1, 51): x=8
if i % 3 = = 0 or i % 6 = = 0 x = li . count (x)
(a) append (i)
print (x)
print (a)
8. Write a program to create a list of numbers in 10. Write a program that prints the maximum and
the range 1 to 20. Then delete all the numbers minimum value in a dictionary.
from the list that are divisible by 3.
Ans. a = [ ] Ans. dict = {‘a’ : 10, ‘b’ : 20, ‘c’ : 5}
for x in range (1, 21): v = dict . values ()
(a) append (i) print (“Maximum = ”, max (v))

ns
for n, i enumerate (a):
if (i % 3 = = 0): print (“Minimum = ”, min (v))
del a [n]

io
print (a)

additional questions and Answers


at
Choose the Correct Answer 1 MARK 6. a = [‘A’, 2, 3, [4, 5, 6]] is an example of
ic
(a) Tuple (b) Set
bl

1. How many collection of datatypes in python? (c) List (d) Dictionary


(a) 3 (b) 4 (c) 2 (d) 5  [Ans. (c) List]
Pu

 [Ans. (b) 4]
7. Which of the following can be used to access
2. Which of the following is not datatype in an element in a list?
Python? (a) Subscript
ra

(a) List (b) Tuples (b) Function


(c) String (d) Set
(c) Integer
Su

 [Ans. (c) String]


(d) Identifier
3. Which of the following is an ordered collection
 [Ans. (a) Subscript]
of values?
(a) Set (b) Tuples 8. In li = [10, 23, 41, 75], the negative index value
(c) List (d) Dictionary of 41 is
 [Ans. (c) List] (a) 2 (b) – 1 (c) – 2 (d) – 3
4. Which of the following datatype enclosed with  [Ans. (c) –2]
[ ]?
(a) Tuples (b) List 9. If python sets – 1 as the index value for the last
(c) Dictionary (d) Set element is called
 [Ans. (b) List] (a) Oppositive indexing
5. In list, the negative index number begin with (b) Tuple indexing
(a) 0 (b) 1 (c) – 1 (d) 0.1 (c) Reverse index
 [Ans. (c) –1] (d) List indexing
 [Ans. (c) Reverse index]

115
[email protected] Ph: 81242 01000 / 812430 1000 / 96001 75757

www.surabooks.com This material only for sample www.surabooks.com

 
Chapter PYTHON CLASSES AND
10 OBJECTS
www.nammakalvi.org

ns
CHAPTER SNAPSHOT
10.1 Introduction

io
10.2 Defining classes
at
10.3 Creating Objects
10.4 Accessing Class Members
ic
10.5 Class Methods
10.6 Constructor and Destructor in Python
bl

10.7 Public and Private Data Members


Pu
ra
Su

[133]

[email protected] Ph: 81242 01000 / 812430 1000 / 96001 75757


Unit III - Chapter 10
www.surabooks.com

Sura’s ➠ XII Std - Computer Science

Evaluation
 
This material only for sample www.surabooks.com

www.nammakalvi.org

self.name=name
Part - I S=Student(“Tamil”)
Choose the best answer  (1 mark) (a) Error (b) Tamil
(c) name (d) self
1. Which of the following are the key features of
 [Ans. (b) Tamil]
an Object Oriented Programming language?
(a) Constructor and Classes 9. Which of the following is the private class
(b) Constructor and Object variable?
(c) Classes and Objects (a) __num (b) ##num
(d) Constructor and Destructor (c) $$num (d) &&num
 [Ans. (c) Classes and Objects]  [Ans. (a) __num]

2. Functions defined inside a class: 10. The process of creating an object is called as:
(a) Functions (b) Module (a) Constructor (b) Destructor

ns
(c) Methods (d) section (c) Initialize (d) Instantiation
 [Ans. (c) Methods]  [Ans. (d) Instantiation]

io
3. Class members are accessed through which
operator?
Part - II
Answer the following questions
(a) & (b) . (c) # (d) %
at
 [Ans. (b) . ]  (2 marks)
ic
4. Which of the following method is automatically 1. What is class?
bl

executed when an object is created? Ans. A class is a way of binding data members and
(a) __object__( ) (b) __del__( ) member function together. Class is a template
Pu

(c) __func__( ) (d) __init__( ) for the object.


 [Ans. (d) __init__( )]
2. What is instantiation?
5. A private class variable is prefixed with Ans. Once a class is created, next to create an object
ra

(a) __ (b) && (c) ## (d) ** or instance of that class. This process of creating
 [Ans. (a) __] object is called as “Class Instantiation”.
Su

6. Which of the following method is used as 3. What is the output of the following program?
destructor? class Sample:
(a) __init__( ) (b) __dest__( ) __num=10
(c) __rem__( ) (d) __del__( ) def disp(self):
 [Ans. (d) __del__( )] print(self.__num)
7. Which of the following class declaration is S=Sample()
correct? S.disp()
(a) class class_name print(S.__num)
(b) class class_name<> Ans. 10
(c) class class_name:
(d) class class_name[ ] 10
 [Ans. (c) class class_name:] 4. How will you create constructor in Python?
8. Which of the following is the output of the Ans. General format of __init__ method
following program?  (Constructor function)
class Student: def __init__(self, [args ……..]):
def __init__(self, name): <statements>

134
[email protected] Ph: 81242 01000 / 812430 1000 / 96001 75757
5.

www.surabooks.com

What is the purpose of Destructor?


This material only for sample

Sura’s
www.surabooks.com

➠ XII Std - Computer Science


def display(self):
print("Good Morning ", self.__name)

Python Classes and Objects


Ans. Destructor is also a special method gets
execution automatically when an object exits obj=Greeting('Bindu Madhavan')
from the scope. obj.display()
Part - III Ans. Good Morning Bindu Madhavan
Answer the following questions 5. How do define constructor and destructor in
 (3 marks) Python?
1. What are class members? How do you define it? Ans. (i) Constructor is the special function that
Ans. Variables defined inside a class are called as is automatically executed when an object
“Class Variable” and functions are called as of a class is created. In Python, there is a
“Methods”. Class variable and methods are special function called “init” which act as a
together known as members of the class. The Constructor.
class members should be accessed through (ii) It must begin and end with double
objects or instance of class. underscore. This function will act as an
ordinary function; but only difference is, it

ns
2. Write a class with two private class variables is executed automatically when the object is
and print the sum using a method. created.
Ans. Class sum: (iii) This constructor function can be defined

io
– a = 10 with or without arguments. This method is
used to initialize the class variables.
– b = 20

at General format of __init__ method (Construc
tor function)
def disp (self):
def __init__(self, [args ……..]):
ic
print (“sum =”, (self. – a + self. - b))
<statements>
s = sum ()
Part - IV
bl

s. disp ()
Find the error in the following program to get Answer the following questions
Pu

3.
the given output?  (5 marks)
class Fruits: 1. Write a menu driven program to add or delete
stationary items. You should use dictionary to
def __init__(self, f1, f2):
store items and the brand.
ra

self.f1=f1
Ans. Class stationary:
self.f2=f2
Su

def getdata (self):


def display(self):
self. Itemname = input (“enter name of the item”)
print("Fruit 1 = %s, Fruit 2 = %s"
self. Brand = input (“enter brand name”)
 %(self.f1, self.f2))
item = {}
F = Fruits ('Apple', 'Mango') ch = ‘y’
del F.display while (ch = = ‘y’)
F.display() print (“1. Add items \n 2. Delete items”)
Output : n = int (input (“enter your choice”))
Fruit 1 = Apple, Fruit 2 = Mango if (n = = 1):
Ans. The statement del F.display should not be used s = stationary ()
to display the required output. s. get data ()
4. What is the output of the following program? item. Append {S}
class Greeting: dif (n = = 2):
del item
def __init__(self, name):
else: print (“Invalid Input”)
self.__name = name
ch = input (“Do you want to continue
(Y/N)”)

135
[email protected] Ph: 81242 01000 / 812430 1000 / 96001 75757
Unit III - Chapter 10
www.surabooks.com

Sura’s ➠ XII Std - Computer Science


Hands on Experience
 
This material only for sample www.surabooks.com

www.nammakalvi.org
A = area ()
b = int (input (“enter base”))
1. Write a program using class to store name and h = int (input (“enter height”))
marks of students in list and print total marks.
(a) area (b, h)
Ans. Class total :
name = [] 3. Write a menu driven program to read, display,
mark = [] add and subtract two distances.
def get (self): Ans. def add (d1, d2)
num = input (“How many students? :”) return d1 + d2
for i in self. num; def subtract (d1, d2)
self. n = input (“Enter student name”)
return d1 – d2
self name. append (self. n)
self. M = int inp (“enter mark of the student”) “/. Read \n”, “2. Display \n”,
mark = append (self. m) print (“Menu \n, 3. Add \n”, “4. Subtract \n”)
def display (self): ch = int (input (“enter your choice”))

ns
for i in self. Num if (ch = = 1):
print (name [i] “:”, mark [i]) x = int (input (“enter distance 1”))
t = t + mark [i]

io
y = int (input (“enter distance 2”))
print (“Total marks”, t)
dif (ch = = 2):
t = total ()
t. get ()
at print (“Distance 1”, x)
t. display () print (“Distance 2”, y)
ic
2. Write a program using class to accept three dif (ch = = 3):
bl

sides of a triangle and print its area. (add(x, y)


Ans. Class area: dif (ch = = 4):
Pu

def area (self, b, h): print (substract (x, y))


print (“Area of Triangle” , (b * h)/2) else:
print (“Invalid Input”)

additional questions and Answers


ra
Su

Choose the Correct Answer 1 MARK 3. Functions of the class are called as
(a) Methods (b) Members
1. Which of the following is not an object (c) Variables (d) Loog
oriented language?  [Ans. (a) Methods]
(a) C (b) C++
(c) Java (d) Python 4. In Python, every class name followed by
 [Ans. (a) C] (a) ; (b) : (c) : : (d) .
2. Which of the following called as instances of a  [Ans. (b) :]
class or class variable? 5. Which of the following with a valid class
(a) Methods definition?
(b) Objects (a) Class classname () statement_1
(c) Functions (b) Class classname : : statement_1
(d) Datatypes (c) Class classname statement_1
 [Ans. (b) Objects] (d) Class classname statement_1
www.nammakalvi.org  [Ans. (c) Class classname statement_1]

136
[email protected] Ph: 81242 01000 / 812430 1000 / 96001 75757

You might also like