Page 311 - C++
P. 311

CBSE AISSCE 2016-2017 Marking Scheme for Computer Science
                                      (Sub Code: 083 Paper Code 91 Outside Delhi)

             4     (a)    Differentiate between file modes ​r+​ and ​rb+​ with respect to Python.              1

                   Ans    r+ Opens a file for both reading and writing. The file pointer placed at
                          the beginning of the file.
                          rb+ Opens a file for both reading and writing in binary format. The file
                          pointer placed at the beginning of the file.

                          (1 mark for correct difference )
                          OR
                          (½ Mark for each correct use of r+ and rb+)

                   (b)    Write a method in python to read lines from a text file MYNOTES.TXT, and display                                         2
                          those lines, which are starting with an alphabet ‘K’.

                   Ans    def display():
                            file=open('MYNOTES.TXT','r')
                            line=file.readline()
                            while line:
                              if line[0]=='K' :
                                print line
                              line=file.readline()
                            file.close()  #IGNORE

                          (½ Mark for opening the file)
                          (½ Mark for reading all lines)
                          (½ Mark for checking condition for line starting with K)
                          (½ Mark for displaying line)
                   (c)    Considering the following definition of class FACTORY, write a method in                             3
                          Python to search and display the content in a pickled file FACTORY.DAT,
                          where FCTID is matching with the value ‘105’.

                          class Factory:
                            def __init__(self,FID,FNAM):
                                 self.FCTID = FID             # FCTID  Factory ID
                                 self.FCTNM = FNAM            # FCTNM  Factory Name
                                 self.PROD  = 1000            # PROD   Production
                            def Display(self):
                                 print self.FCTID,":",self.FCTNM,":",self.PROD

                   Ans    import pickle
                          def ques4c( ):
                              f=Factory( )
                              file=open('FACTORY.DAT','rb')
                              try:
                                  while True:
                                      f=pickle.load(file)
                                      if f.FCTID==105:
                                          f.Display()
                              except EOF Error:
                                  pass
                              file.close()   #IGNORE
                          (½ Mark for correct method header)
                          (½ Mark for opening the file FACTORY.DAT correctly)

                                                     Page #21 of 28
   306   307   308   309   310   311   312   313   314   315   316