Q34
3 marksShort AnswerSection C

(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)

``

Functions
Tracing Function Output — String Slicing, List append/pop/reverse, Modulo Arithmetic
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-

``

dry runtracing python codelist.reverse()list.pop()string slicing [1:-1]modulo operatorfloor divisionisupper()

Marking Scheme

  • 1Part (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'].
  • 2Part (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-.
  • 3Either 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.

Analysis & 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

  • new starts even (0), so the first digit is added using %5; whenever new becomes odd, the next digit is added using %10 instead, alternating the rule every step as given is stripped one digit at a time from the right using //=10.

Exam trap

  • Forgetting that new%2 is checked BEFORE updating new each iteration, and mixing up isupper() with isalpha(), are the most common tracing errors.

Common Mistakes

  1. 1Including the first and last characters of the string instead of correctly excluding them via given[1:-1].
  2. 2Treating 'elif ch in new: new.pop()' as removing the character ch itself, rather than always removing the LAST element of the list.
  3. 3In part (b), updating new before checking new % 2, instead of checking parity based on the value of new from the previous iteration.

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.

Spotted a mistake or something unclear?

Tell us — we fix reported answers fast.

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.