Page 232 - Computer Science Class 11 Without Functions
P. 232

(xiii) num = 0
                 total = 0
                 upperLimit = 20
                 while num <= upperLimit:
                     total = total + num
                     num += 2
                 print(num, total)
         Ans:  22 110
              (xiv) while(3>2):
                    print('Yes', end='')
                else:
                    print('Tried', end = '')
         Ans:  Infinite loop YesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYes …
              (xv) for i in range(1, 5):
                    for col in range(6, 10):
                        print(i*col, end=' ')
                print()
         Ans:  6 7 8 9 12 14 16 18 18 21 24 27 24 28 32 36
           6.  What will be the output produced on the execution of the following code snippet:
              for i in range(1,5):
                  pass
                  print(i, end='')
              print('NO OUTPUT')
         Ans:  1234NO OUTPUT
          7.  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)
           8.  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 remains True.
           9.  What will be the output produced on execution of the following program?
              '''
              Objective: To count the number of spaces in a input sentence
              Inputs:
                  sentence : a string
              Output:
                  count : Number of spaces
              '''
              sentence = input('Enter a sentence: ')
              count = 0
              for char in sentence:
                  if char == ' ':
                      count += 1
              else:
                  print('The number of spaces: ', count)
         230   Touchpad Computer Science-XI
   227   228   229   230   231   232   233   234   235   236   237