(a) Write the output of the following code :
``python
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 :
`python
def Exam2026(given):
new = 0
while given:
if new % 2:
new += given % 10
else:
new += given % 5
print(new, end='-')
given //= 10
Exam2026(123456)
``
(a) Write the output of the following code :
``python
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 :
`python
def Exam2026(given):
new = 0
while given:
if new % 2:
new += given % 10
else:
new += given % 5
print(new, end='-')
given //= 10
Exam2026(123456)
``
Official answer
Two independent OR outputs, each obtained by dry-running the function step by step.
Part (a) output:
``
['4', '2', '-', 'd', 'l', 'o']
`
Part (b) output:
`
1-6-10-13-15-16-
``
Marking scheme
- 1.Part (a): 1 mark for correctly identifying given[1:-1]; 1 mark for correct handling of reverse()/append()/pop() logic; 1 mark for the exact final output ['4', '2', '-', 'd', 'l', 'o'].
- 2.Part (b): 1 mark for correctly alternating between num%5 and num%10 based on parity of new; 1 mark for correct digit extraction via given%10 and given//=10; 1 mark for the exact final printed output 1-6-10-13-15-16-.
- 3.Either OR option, fully and correctly traced, earns the full 3 marks; partial credit for correct method with one arithmetic slip.
Hint
Trace variable by variable, one character/digit per line — never guess the output of an iterative function.
Quick oral answer
I trace the loop character by character, updating the list with append, pop, or reverse exactly as the code specifies, and read off the final list or accumulator value only after the loop ends.
Common mistakes
- 1.Including the first and last characters of the string instead of correctly excluding them via given[1:-1].
- 2.Treating 'elif ch in new: new.pop()' as removing the character ch itself, rather than always removing the LAST element of the list.
- 3.In part (b), updating new before checking new % 2, instead of checking parity based on the value of new from the previous iteration.
Analysis and explanation
Both snippets must be traced character-by-character / digit-by-digit — no shortcuts work here.
Part (a) — list as a toggle stack
given[1:-1]strips the first and last character of "Gold-24Medals", leaving the 11 characters o,l,d,-,2,4,M,e,d,a,l.- Every lowercase/non-letter character is appended if new, but if it is already present the list is popped (undo); an uppercase letter reverses the whole list instead of being stored.
- Tracing in order gives
new = ['4','2','-','d','l','o']after 'M' reverses['o','l','d','-','2','4'], then 'e' is added and removed by 'd', and 'a' is added and removed by 'l'.
Part (b) — alternating mod-5/mod-10 accumulator
newstarts even (0), so the first digit is added using%5; whenevernewbecomes odd, the next digit is added using%10instead, alternating the rule every step asgivenis stripped one digit at a time from the right using//=10.
Exam trap
- Forgetting that
new%2is checked BEFORE updating new each iteration, and mixing upisupper()withisalpha(), are the most common tracing errors.
Interesting facts
list.reverse() reverses a list in place and returns None — a very common trap is writing 'new = new.reverse()', which silently sets new to None.
Python's // (floor division) on a positive integer discards the fractional part exactly like the popular technique of stripping the last digit of a number in competitive programming.
The custom function name 'Exam2026' embedded in the code is a stylistic fingerprint CBSE has used in recent papers to make each year's code-tracing question instantly distinguishable.
Question details
Q34 of 48 questions
- Board
- CBSE
- Class
- Class 12
- Subject
- Computer Science
- Year
- 2026
- Section
- Section C
- Marks
- 3
- Type
- Short Answer
- Chapter
- Functions
- Topic
- Tracing Function Output — String Slicing, List append/pop/reverse, Modulo Arithmetic
- Bloom's level
- analyze
- Syllabus topic
- Unit 1: Computational Thinking and Programming – II — Functions: dry-run/tracing of user-defined functions using strings, lists and integer division
- Author
- CBSE
- Reviewed by
- Padhantu Academic Team
Spotted a mistake or something unclear?
Tell us and we will fix it.
Frequently asked questions
Why does 'M' trigger new.reverse() instead of being added to the list?
Because the if-condition checks ch.isupper() first; since 'M' is an uppercase letter, the reverse() branch executes and the elif branches (append/pop) are skipped entirely.
In part (b), why does the rule change from %5 to %10 partway through?
The rule depends on whether the accumulator 'new' is currently odd or even — new%2 is checked fresh every iteration, so as soon as new becomes odd the digit is extracted with %10 instead of %5.
Does given //= 10 remove the first or last digit of the number?
It removes the last digit each time (integer division by 10 drops the units digit), which is why the digits are processed in order from units, tens, hundreds and so on.