Page 328 - Computer Science Class 11 With Functions
P. 328

Assertion and Reasoning Based Questions


               The following questions are assertion(A) and reasoning(R) based. Mark the correct choice as
              a.  Both A and R are true and R is the correct explanation of A
              b.  Both A and R are true and R is not the correct explanation of A
              c.  A is true but R is false
              d.  A is false but R is true
           1.  Assertion(A):  A string in Python must be enclosed in double quotes, for example,"hello world"
              Reasoning (R):  A string cannot be concatenated with a number.
           2.  Assertion(A):  Strings are immutable.
              Reasoning(R):  Once a string object is created, it cannot be changed.
         Ans.  1. b  2. a


               Case Based Questions


           1.  Shilpi finds programs on strings interesting. Her most recent programming challenge requires her to take a string that
              may contain several words (possibly zero) and return a new string with the first letter of each word in the original text
              capitalized. Assist Shilpi in finishing the job.
         Ans.  To develop the desired function, Shilpi should follow the following approach:
              a.  Split the input text into a list of individual words. Use the method split().
              b.  Capitalise the first letter of each word in the list and append it to an initially empty list. Use the method capitalize().
              c.  Join the capitalised words back into a single string. Use the method  join() with a space as the separator.
              Now the function can be developed as follows:
              def capitalise(text):
                  '''
                  Objective: To capitalise the first letter of each word in text.
                  Input Parameters: text - string
                  Return Value: capitalisedText - string
                  '''
                  words = text.split()
                  capitalisedWords= []
                  for word in words:
                      capitalisedWords.append(word.capitalize())
                  capitalisedText = ' '.join(capitalisedWords)
                  return capitalisedText


              text = input("Enter a string of alphabets:  ")
              capitalisedText = capitalise(text)
              print("The new sentence is ", capitalisedText)
              Sample execution of the function:
              Enter a string: honesty is the best policy
              Modified string: Honesty Is The Best Policy
           2.  Thomas is working on a word processor. He finds the toggle case feature very interesting. Along similar lines, he wants to
              create a program in Python that will accept a string in lowercase as input and capitalize every alternate character of the
              string. Help him complete the task.

         326   Touchpad Computer Science-XI
   323   324   325   326   327   328   329   330   331   332   333