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

Select the correct output of the code:

Question 4 CBSE Sample Question Paper 2024 Boards - Teachoo.png

Options:

a. PYTHON-IS-Fun

b. PYTHON-is-Fun

c. Python-is-fun

d. PYTHON-Is -Fun

Answer

Answer by student

b. PYTHON-is-Fun.

Detailed answer by teachoo

To find the output of the code, we need to understand how the string methods work in Python. The string methods are:

  • split() : This method splits a string into a list of words based on a separator. If no separator is given, it splits on whitespace.
  • join() : This method joins a list of strings into one string using a separator. The separator is a string that is placed between each element of the list.
  • upper() : This method returns a copy of a string with all letters converted to uppercase.
  • capitalize() : This method returns a copy of a string with only its first letter capitalized and the rest lowercase.

Evaluating the code step by step:

  • s = “Python is fun”

First, we assign the string “Python is fun” to the variable s.

  • l = s.split()

Next, we split s into a list of words using the split() method and assign it to the variable l. So, l becomes ["Python", "is", "fun"] .

  • s_new = “-”.join([l[0][0].upper(), l[1][0], l[2][0].capitalize()])

Then, we join the first letter of each word in l with a hyphen using the join() method and assign it to the variable s_new. 

To get the first letter of each word, we use indexing with [0]

To capitalize the first and third letters, we use the upper() and capitalize() methods respectively. 

So, s_new becomes "PYTHON-is-Fun" .

  • print(s_new)

Finally, we print s_new using the print() function. 

Let’s analyze each of the options and see why they are correct or incorrect.

  • Option a: PYTHON-IS-Fun. This is incorrect because it capitalizes the second word “IS”, which is not what the code does.
  • Option b: PYTHON-is-Fun . This is correct because it matches the output of the code.
  • Option c: Python-is-fun. This is incorrect because it does not capitalize the first word “Python”, which is not what the code does.
  • Option d: PYTHON-Is -Fun. This is incorrect because it has an extra space before the hyphen, which is not what the code does.

So, the correct answer is b. PYTHON-is-Fun.

Go Ad-free
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 14 years. He provides courses for Maths, Science and Computer Science at Teachoo