Test Index

CBSE Class 12 Computer Science 2025 Paper Set 4 Get it on Google PlayDownload on the App Store

© examsnet.com
Question : 31 of 37
Marks: +1, -0
(a) Predict the output of the following code :
def ExamOn(mystr) :
newstr = ""
count = 0
for i in mystr:
if count%2 != 0:
newstr = newstr + str(count-1)
else:
newstr = newstr + i.lower()
count += 1
newstr = newstr + mystr[:2]
print("The new string is:", newstr)
ExamOn("GenX")
OR
(b) Write the output on execution of the following Python code:
def Change(X):
for K,V in X.items():
L1.append(K)
L2.append(V)
D={1:"ONE",2:"TWO",3:"THREE"}
L1=[]
L2=[]
Change(D)
print(L1)
print(L2)
print(D)
Solution:  
(a)
Concept: At an even count the character itself is added in lowercase; at an odd count the digit count-1 is added instead. Finally the first two characters of the original string are joined at the end.
Trace (mystr = "GenX"):
counticount%2 != 0 ?addednewstr
0GNogg
1eYesstr(0) = 0g0
2nNong0n
3XYesstr(2) = 2g0n2
After the loop: mystr[:2] = "Ge", so newstr = "g0n2" + "Ge" = "g0n2Ge".
Output:
The new string is: g0n2Ge
OR (b)
As printed, the two append lines are at the same level as the for statement, so the for loop has no body. Python stops before running anything and reports:
IndentationError: expected an indented block after 'for' statement on line 2
With the two lines indented inside the loop (the intended code), Change(D) walks through the key-value pairs of D in insertion order and appends each key to L1 and each value to L2. L1 and L2 are global lists, so append() changes them directly; D itself is not changed.
Output:
[1, 2, 3]
['ONE', 'TWO', 'THREE']
{1: 'ONE', 2: 'TWO', 3: 'THREE'}
Key point: A block that follows a statement ending in a colon (def, for, if) must be indented; otherwise Python raises an IndentationError.
© examsnet.com
Advertisements
Go to Question: