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

6.  Rewrite the following program after removing the errors. Underline each correction made.

              txt = 'Good Morning'
              for s in range(txt):
              print(s)
         Ans.  txt='Good Morning'

              for s in (txt):
                  print(s)
           7.  Differentiate between for loop and while loop.
         Ans.  for loops: The number of iterations is known in advance.

              while loops: The body of the while loop is executed until the test condition evaluates to False.
           8.  What will be the output produced on execution of the following program?

         Ans. def countSpace(txt):
                  """
                  Objective: To count the number of spaces
                  Inputs:
                         txt : a string
                  Return value:
                      count : Number of spaces
                  """


                  count = 0
                  for ch in txt:
                      if ch == ' ':
                          count += 1
                  else:
                      return count
              sentence = input('Enter a sentence: ')
              number = countSpace(sentence)
              print('The number of spaces:', number)
              Output:
              Enter a sentence: Hello, How are you?
              The number of spaces: 3
           9.  Write a program to accept 10 numbers and display the largest number.
         Ans.  # To accept 10 numbers and find the largest number
              num = int(input('Enter a number :  '))
              large = num
              for ctr in range(1, 10):
                  num = int(input('Enter a number: '))
                  if large < num:
                      large = num
              else:
                  print('The Largest number is: ', large)
          10.  Write a program to display the following sequence of numbers:
                   -5  10  -15  20  -25, …, n (excluding  n)


         266   Touchpad Computer Science-XI
   263   264   265   266   267   268   269   270   271   272   273