CBSE Class 12 Computer Science Question 30 of 42

Solved 2024 Sample Question Paper CBSE Class 12 Computer Science (083) — Question 1

Back to all questions
1
Question

Question 26

Predict the output of the Python code given below:

Text1 = "IND-23"
Text2 = ""
I = 0
while I < len(Text1):
    if Text1[I] >= "0" and Text1[I] <= "9":
        Val = int(Text1[I])
        Val = Val + 1
        Text2 = Text2 + str(Val)
    elif Text1[I] >= "A" and Text1[I] <= "Z":
        Text2 = Text2 + (Text1[I + 1])
    else:
        Text2 = Text2 + "*"
    I += 1
print(Text2)
Answer
Output
ND-*34
Explanation

The provided Python code initializes a variable Text1 with the string value "IND-23". Then it initializes an empty string variable Text2 and an index variable I with the value 0. The while loop continues as long as I is less than the length of Text1. Within the loop, each character of Text1 is checked: if it's a digit (0-9), it increments the digit by 1 and appends it to Text2; if it's an uppercase letter (A-Z), it appends the next character in Text1 to Text2; otherwise, it appends an asterisk "*". After processing all characters, the final value of Text2 is printed.