Question:medium

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

Show Hint

Use dry-run (tracing) to predict program behavior.
Check conditions like even/odd logic and concatenation carefully.
Updated On: Jan 14, 2026
Show Solution

Solution and Explanation

Step-by-step trace for input "GenX".
Initial state: newstr = "", count = 0
Iteration 1 (count = 0):
Current character: i = 'G'
Condition met: 0%2 == 0 (even)
newstr updated to: newstr + 'g'newstr = "g"
count incremented to: 1
Iteration 2 (count = 1):
Current character: i = 'e'
Condition not met: 1%2 ≠ 0 (odd). Append count - 1 (0).
newstr updated to: "g" + "0"newstr = "g0"
count incremented to: 2
Iteration 3 (count = 2):
Current character: i = 'n'
Condition met: 2%2 == 0. Append 'n'.
newstr updated to: "g0" + "n"newstr = "g0n"
count incremented to: 3
Iteration 4 (count = 3):
Current character: i = 'X'
Condition not met: 3%2 ≠ 0. Append count - 1 (2).
newstr updated to: "g0n" + "2"newstr = "g0n2"
count incremented to: 4
After loop completion: original string slice mystr[:2] = "Ge" is appended.
newstr updated to: "g0n2" + "Ge"newstr = "g0n2Ge"
Final result: The new string is: g0n2Ge
% Final Answer Output: The new string is: g0n2Ge
Was this answer helpful?
0