Test Index

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

© examsnet.com
Question : 31 of 37
Marks: +1, -0
(a) Write the output of the following code :
def Exam2026(given) :
new=[]
for ch in given[1:-1]:
if ch.isupper():
new.reverse()
elif ch not in new:
new.append(ch)
elif ch in new:
new.pop()
print(new)
Exam2026("Gold-24Medals")
OR
(b) Write the output of the following code :
def Exam2026(given):
new = 0
while given:
if new % 2:
new += given % 10
else:
new += given % 5
print(new, end='-')
given //= 10
Exam2026(123456)
Solution:  
(a)
Explanation:
given[1:-1] drops the first and the last character, so the loop runs over "old-24Medal". An uppercase letter reverses the list (it is never added); any other character is appended if it is not in the list, and if it is already in the list the last element is popped (pop() removes the last element, not that character).
chRule appliednew after the step
onot in new: append['o']
lnot in new: append['o', 'l']
dnot in new: append['o', 'l', 'd']
-not in new: append['o', 'l', 'd', '-']
2not in new: append['o', 'l', 'd', '-', '2']
4not in new: append['o', 'l', 'd', '-', '2', '4']
Muppercase: reverse['4', '2', '-', 'd', 'l', 'o']
enot in new: append['4', '2', '-', 'd', 'l', 'o', 'e']
dalready in new: pop last['4', '2', '-', 'd', 'l', 'o']
anot in new: append['4', '2', '-', 'd', 'l', 'o', 'a']
lalready in new: pop last['4', '2', '-', 'd', 'l', 'o']
Output:
['4', '2', '-', 'd', 'l', 'o']
OR (b)
Explanation:
In each pass, if new is odd (new % 2 is 1, i.e. True) the last digit given % 10 is added; if new is even, given % 5 is added. The value is printed with '-' after it (no new line), and given //= 10 removes the last digit. The loop stops when given becomes 0.
givennew % 2Value addednewPrinted
1234560 (even)123456 % 5 = 111-
123451 (odd)12345 % 10 = 566-
12340 (even)1234 % 5 = 41010-
1230 (even)123 % 5 = 31313-
121 (odd)12 % 10 = 21515-
11 (odd)1 % 10 = 11616-
Output:
1-6-10-13-15-16-
© examsnet.com
Advertisements
Go to Question: