Computer Science - Class 12
Solutions to CBSE Sample Paper - Computer Science Class 12

(a) Predict the output of the code given below:

s="welcome2cs"

n = len(s)

m=""

for i in range(0, n):

if (s[i] >= 'a' and s[i] <= 'm'):

m = m +s[i].upper()

elif (s[i] >= 'n' and s[i] <= 'z'):

m = m +s[i-1]

elif (s[i].isupper()):

m = m + s[i].lower()

else:

m = m +'&'

print(m)

 

Answer:

Question 32 Choice 2 a -Teachoo.png

Output: sELCcME&Cc

Explanation:

The above code takes the string s and iterates through each character of the string and:

  • If the character is between ‘a’ and ‘m’, it is changed to uppercase and appended to the string m.
  • Else if the character is between ‘n’ and ‘z’, its preceding character in the string is appended to the string m.
  • Else if the character is in upper case, it is changed to smaller case and appended to the string m.
  • Else, if the character does not satisfy any of the above conditions, an ‘&’ symbol is appended to the string m.

The final string ‘m’ is then printed as output. 

Learn in your speed, with individual attention - Teachoo Maths 1-on-1 Class

Ask a doubt
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 13 years. He provides courses for Maths, Science, Social Science, Physics, Chemistry, Computer Science at Teachoo.