Test Index
CBSE Class 12 Computer Science 2026 Paper Set 4
© 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 //= 10Exam2026(123456)
Solution:
(a)Explanation:
Output:
Output:
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).| ch | Rule applied | new after the step |
|---|---|---|
| o | not in new: append | ['o'] |
| l | not in new: append | ['o', 'l'] |
| d | not in new: append | ['o', 'l', 'd'] |
| - | not in new: append | ['o', 'l', 'd', '-'] |
| 2 | not in new: append | ['o', 'l', 'd', '-', '2'] |
| 4 | not in new: append | ['o', 'l', 'd', '-', '2', '4'] |
| M | uppercase: reverse | ['4', '2', '-', 'd', 'l', 'o'] |
| e | not in new: append | ['4', '2', '-', 'd', 'l', 'o', 'e'] |
| d | already in new: pop last | ['4', '2', '-', 'd', 'l', 'o'] |
| a | not in new: append | ['4', '2', '-', 'd', 'l', 'o', 'a'] |
| l | already in new: pop last | ['4', '2', '-', 'd', 'l', 'o'] |
['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.| given | new % 2 | Value added | new | Printed |
|---|---|---|---|---|
| 123456 | 0 (even) | 123456 % 5 = 1 | 1 | 1- |
| 12345 | 1 (odd) | 12345 % 10 = 5 | 6 | 6- |
| 1234 | 0 (even) | 1234 % 5 = 4 | 10 | 10- |
| 123 | 0 (even) | 123 % 5 = 3 | 13 | 13- |
| 12 | 1 (odd) | 12 % 10 = 2 | 15 | 15- |
| 1 | 1 (odd) | 1 % 10 = 1 | 16 | 16- |
1-6-10-13-15-16-
© examsnet.com
Go to Question: