XII-CS (Ch. 2 ASSIGNMENT-2) - soln

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

DELHI PUBLIC SCHOOL, BAREILLY

XII- Computer Science


ASSIGNMENT-2
(CHAPTER 2 PYTHON REVISION TOUR- II)

Q.1 Consider the following string :

sub = "Computer Science"

What will be the output of the following string operations :

i. print(sub[: :-1]) 'ecneicS retupmoC'

ii. print(sub[-7:-1]) 'Scienc'

iii. print(sub[::2]) 'Cmue cec'

iv. print(sub[len(sub)-1]) 'e'

v. print(2*sub) 'Computer ScienceComputer Science'

vi. print(sub[::-2]) 'eniSrtpo'

vii. print(sub[:3] + sub[3:]) 'Computer Science'

viii. print (sub[ : 8 : -1]) 'ecneicS'

ix. print(sub.startswith('Comp')) True

x. print(sub.isalpha()) False

Q.2 Consider the following string “Adrs”:

Adrs = "WZ-1,New Ganga Nagar, New Delhi"

What will be the output of following string operations :

i. print(Adrs.lower()) wz-1,new ganga nagar, new delhi

ii. print(Adrs.upper()) WZ-1,NEW GANGA NAGAR, NEW DELHI

iii. print(Adrs.count('New')) 2

iv. print(Adrs.find('New')) 5

v. print(Adrs.rfind('New')) 22

vi. print(Adrs.split(',')) ['WZ-1', 'New Ganga Nagar', ' New Delhi']

vii. print(Adrs.split(' ')) ['WZ-1,New', 'Ganga', 'Nagar,', 'New', 'Delhi']

viii. print(Adrs.replace('New','Old')) WZ-1,Old Ganga Nagar, Old Delhi

ix. print(Adrs.partition(',')) ('WZ-1', ',', 'New Ganga Nagar, New Delhi')

Page 1 of 8
x. print(Adrs.index(' Delhi ')) Delhi

(b) Find the output of the following code:


s="welcome2DPS"
n = len(s)
m=""
for i in range(0, n):
if (s[i] >= 'a' and s[i] <= 'm'):
m = m +s[i].upper()
elif (s[i] >= 'n' and s[i] <= 'z'):
m = m +s[i-1]
elif (s[i].isupper()):
m = m + s[i].lower()
else:
m = m +'#'
print(m)
OUTPUT:
SELCcME#dps

(c) Find the output of the following code:


Name="PythoN3.1"
R=""
for x in range(len(Name)):
if Name[x].isupper():
R=R+Name[x].lower()
elif Name[x].islower():
R=R+Name[x].upper()
elif Name[x].isdigit():
R=R+Name[x-1]
else:
R=R+"#"
print(R)
OUTPUT:
pYTHOnN#.

Q.4 What will be the output of following program:

a='hello'; b='virat'

for i in range(len(a)):

print(a[i],b[i])

OUTPUT:
h#v

Page 2 of 8
e#i
l#r
l#a
o#t

Q.5 What will be the output of the following code segment:


list1 = [1,2,3,4,5,6,7,8,9,10]
for i in range(0,len(list1)):
if i%2 == 0:
print(list1 [i])
OUTPUT:
h#v
1
3
5
7
9

Q.6 What will be the output of the following statements?


a) list1 = [12,32,65,26,80,10]
list1.sort()
print(list1)
OUTPUT:
[10, 12, 26, 32, 65, 80]

b) list1 = [12,32,65,26,80,10]
sorted(list1)
print(list1)
OUTPUT:
[12, 32, 65, 26, 80, 10]

c) list1 = [1,2,3,4,5,6,7,8,9,10]
list1[::-2]
list1[:3] + list1[3:]
OUTPUT:
[10, 8, 6, 4, 2]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

d) list1 = [1,2,3,4,5]
list1[len(list1)-1]
OUTPUT:
5

e) Lst=[‘C’,’O’,’M’,’P’,’U’,’T’,’E’,’R’]

Page 3 of 8
print(Lst[3:6])
OUTPUT:
['P', 'U', 'T']

f) Lst = [ 12, 34, 4, 56, 78, 22, 78, 89]


print(Lst[1:6:2])

OUTPUT:
[34, 56, 22]

g) L = [ 1,2,3,4,5,6,7]
B=L
B[3:5] = 90,34
print(L)

OUTPUT:
[1, 2, 3, 90, 34, 6, 7]

h) colors=["violet", "indigo", "blue", "green", "yellow", "orange", "red"]


del colors[4]
colors.remove("blue")
colors.pop(3)
print(colors)

OUTPUT:
['violet', 'indigo', 'green', 'red']

i) A = [17, 24, 15, 30]


A.insert( 2, 33)
print ( A [-4])

OUTPUT:
24

Q.7 Find the output of the following statements:


tup1 = (23,1,45,67,45,9,55,45)
tup2 = (100,200)
i. print(tup1.index(45))
ii. print(tup1.count(45))
iii. print(tup1+ tup2)
iv. print(len(tup2))
v. print(max(tup1))
vi print(min(tup1))
vii. print(sum(tup2))

Page 4 of 8
viii. print(sorted(tup1))
print(tup1)
ix. S = 1, (2,3,4), 5, (6,7)
len(S)
x. T = (2,66,77,55,6,9,55,8)
print(T.index(55))
OUTPUT:
2
3
(23, 1, 45, 67, 45, 9, 55, 45, 100, 200)
2
67
1
300
[1, 9, 23, 45, 45, 45, 55, 67]
(23, 1, 45, 67, 45, 9, 55, 45)
3

Q.8 Find the output of the following code:

s="LOST"
L=[10,21,33,4]
D={}
for I in range(len(s)):
if I%2==0:
D[L.pop()]=s[I]
else:
D[L.pop()]=I+3

for K,V in D.items():


print(K,V,sep='*')

OUTPUT:
4*L
33*4
21*S
10*6

Q.3 MCQ:

1. Identify the valid Python statement from the following:


a) a=dict() b) b= { } c) c=[] d) d=dict{ }
ANS: option A,B,C
2. Which of the following is not a sequential datatype in Python ?
a) Dictionary b) String c) List d) Tuple

Page 5 of 8
ANS: option A, Dictionary
3. Consider the statements given below and then choose the correct output from the
given options:
str=“COMPUTER”
print(str[:4]+”$”+str[-5:])
a) COMP$PUTER b) COMP$UTER c) COMP$RETU d) COMP$RETUP
ANS: option A, COMP$PUTER
4. Select the correct output of the code :
S="Amrit Mahotsav @ 75"
A=S.split(" ", 2)
print(A)
a) ('Amrit', 'Mahotsav', '@', '75') b)['Amrit', 'Mahotsav', '@ 75']
c) ('Amrit', 'Mahotsav', '@ 75') d)['Amrit', 'Mahotsav', '@','75']
ANS: option B, ['Amrit', 'Mahotsav', '@ 75']
5. Select the correct output of the code :
x='apple,pear,peach'
y=x.split(',')
print(z)
a) ['apple', 'pear', 'peach'] b) ['pear','apple', 'peach']
c) ['peach','apple', 'pear'] d) 'apple pear peach'
ANS: option A, ['apple', 'pear', 'peach']
6. For a string S declared as S = 'PYTHON', which of the following is incorrect?
a) N=len(S) b) T = S c) 'T' in S d) S[0] = 'M'
ANS: option D, S[0] = 'M'
7. Which of the following is/are immutable object types in Python ?
a) List b) String c) Tuple d) Dictionary
ANS: option B String, C Tuple
8. What shall be the output for the execution of the following Python Code ?
Cities = ['Delhi', 'Mumbai']
Cities[0], Cities[1] = Cities[1], Cities[0]
print(Cities)
ANS: ['Mumbai', 'Delhi']
9. Given the dictionary D={'Rno': 1, 'Name':'Suraj'} ,
write the output of print(D('Name')).
ANS: TypeError -It should be D['Name']
10. Suppose a tuple T is declared as T = (10, 12, 43, 39), which of the following is
incorrect?
a) print(T[1]) b) T[2] = -29 c) print(max(T)) d) print(len(T))
ANS: option B (Tuple is Immutable)
11. Identify the valid declaration of L: L = [‘Mon’, ‘23’, ‘hello’, ’60.5’]
a) dictionary b) string c)tuple d) list
ANS: option D, list
12. If the following code is executed, what will be the output of the following code?
name="ComputerSciencewithPython"
Page 6 of 8
print(name[3:10])
ANS: 'puterSc'
13. Given a Tuple tup1= (10, 20, 30, 40, 50, 60, 70, 80, 90).
What will be the output of print (tup1 [3:7:2])?
a) (40,50,60,70,80) b)(40,50,60,70) c)[40,60] d) (40,60)
ANS: option D, (40,60)
14. Which of the following operator cannot be used with string data type?
a) + b) in c) * d) /
ANS: option D,/
15. What will be the output of the following code?
tup1 = (1,2,[1,2],3)
tup1[2][1]=3.14
print(tup1)
a) (1,2,[3.14,2],3) b) (1,2,[1,3.14],3)
c) (1,2,[1,2],3.14) d) Error Message
ANS: option B, (1, 2, [1, 3.14], 3)
16. What is the correct way to add an element to the end of a list in Python?
a) list.add(element) b) list.append(element)
c) list.insert(element) d) list.extend(element)
ANS: option B, list.append(element)
17. What will be the output of :
print("Welcome To My Blog"[2:6] + "Welcome To My Blog"[5:9])
a) Lcomme b) lcomme T c) lcomme To d) lcomme
ANS: option B,lcomme T
18. Which of the following statement(s) would give an error during the execution of
the following code?
R = {'pno':52,'pname':'Virat', 'expert':['Badminton','Tennis'] ,'score':(77,44)}
print(R) #Statement 1
R['expert'][0]='Cricket' #Statement 2
R['score'][0]=50 #Statement 3
R['pno']=50 #Statement 4
a)Statement 1 b)Statement 2 c)Statement 3 d) Statement 4
ANS: #Statement 3- can’t modify Tuple
19. Given the following dictionaries:
dict_student = {"rno" : "53", "name" : 'Rajveer Singh'}
dict_marks = {"Accts" : 87, "English" : 65}
Which statement will append the contents of dict_marks in dict_student?
a) dict_student + dict_marks b) dict_student.add(dict_marks)
c) dict_student.merge(dict_marks) d) dict_student.update(dict_marks)
ANS: option D, dict_student.update(dict_marks)
20. What will be the output of the following code?
L=["One , Two", "Three", "Four"]
print(len(L)/2*len(L[0]))
a) 6.5 b) 13 c) 13.5 d) 6.0
Page 7 of 8
ANS: option C, 13.5 (3/2*9)
21. What will be the output of the following code?
s=”Python is fun”
l=s.split()
s_new = '-'.join( [l[0].upper() , l[1], l[2].capitalize()] )
print(s_new)
a) Python-is-fun b) PYTHON-Is –Fun
c) PYTHON-IS-Fun d) PYTHON-is-Fun
ANS: option D,'PYTHON-is-Fun'(l[0] is ‘python’, l[2]convert 1st Alph
22. Which of the following will delete key-value pair for key = “Red” from a
dictionary D1?
a) delete D1("Red") b) del D1["Red"]
c) del.D1["Red"] d) D1.del["Red"]
ANS: option C, del D1["Red"]
23. Consider the statements given below and then choose the correct output from
the given options:
pride="#G20 Presidency"
print(pride[-2:2:-2])
a) ndsr b) ceieP0 c) ceieP d) yndsr
ANS: option B, 'ceieP0'
24. Which of the following statement(s) would give an error during execution of the
following code?
tup = (20,30,40,50,80,79)
print(tup) #Statement 1
print(tup[3]+50) #Statement 2
print(max(tup)) #Statement 3
tup[4]=80 #Statement 4
a) Statement 1 b) Statement 2 c) Statement 3 d) Statement 4
ANS: #Statement 4- can’t modify tuple elem as is immutable
25. Given the following Tuple :
Tup= (10, 20, 30, 50)
Which of the following statements will result in an error?
a) print (Tup [0]) b) print (Tup [1:2])
c) Tup.insert (2,3) d) print (len (Tup))
ANS: option C, can’t modify/insert in tuple as it is immutable
26. Select the correct output of the code:
S= "Amrit Mahotsav @ 75"
A=S.partition (" ")
print (A)
a) ('Amrit Mahotsav', '@','75') b) ['Amrit', 'Mahotsav ','@', '75']
c) ('Amrit', 'Mahotsav @ 75') d) (' it', '', Mahotsav @ 75')
ANS: option D, ('Amrit', ' ', 'Mahotsav @ 75')
27. ___________function is used to arrange the elements of a list in ascending
order.
a) sort() b) arrange() c) ascending() d) asort()
ANS: option A, sort()
Page 8 of 8

You might also like