Test Index
CBSE Class 12 Computer Science 2025 Paper Set 4
© 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
After the loop:
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"):| count | i | count%2 != 0 ? | added | newstr |
|---|---|---|---|---|
| 0 | G | No | g | g |
| 1 | e | Yes | str(0) = 0 | g0 |
| 2 | n | No | n | g0n |
| 3 | X | Yes | str(2) = 2 | g0n2 |
mystr[:2] = "Ge", so newstr = "g0n2" + "Ge" = "g0n2Ge".Output:The new string is: g0n2GeOR (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 2With 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
Go to Question: