Class 12 Computer Science File Handling in Python MCQs Set 9

Python File Handling



File Handling in Python MCQ’s Set – 9

Multiple Choice Questions


Directions (Q.No 161-166) : Solve the question 161 to 166, to complete the function CountRec()

A file “STUDENT.DAT” has structure (admission_number, Name, Percentage).

A function countrec() in Python that would read contents of the file “STUDENT.DAT” and display the details of those students whose percentage is above 75. Also display number of students scoring above 75%.

import pickle
____ CountRec()__                                #statement 1
    fobj=open("STUDENT.DAT","rb")
    num = 0
    rec=[]
    try:
        while True:                            
            rec = __________                    #statement 2
            if __________:                      #statement 3
                print(rec[0],rec[1],rec[2],sep="\t")
                _________                       #statement 4
    except:
        fobj.close()                           
    return num

__________                                      #statement 5

161. Complete the #statement 1, write the statement to complete the function header.

a) CountRec() :

b) def CounteRec :

c) def CountRec( ):

d) None of these

Answer : c) def CountRec() :

162. Complete the #statement 2, Read a record from a binary file.

a) rec = pickle.load(fobj)

b) rec = load()

c) rec = fobj.load()

d) None of these

Answer : a) rec = pickle.load(obj)

163. Complete the #statement 3, Write a condition to check the current record percentage is greater than 75%.

a) if percentage > 75 :

b) if rec[2] > 75 :

c) if rec[‘percentage’] > 75 :

d) None of these

Answer : b) if rec[2] > 75 :

164. Complete the #statement 4, write the statement to increase the record counter by 1.

a) num = num + 1

b) num += 1

c) Both (a) and (b)

d) None of these

Answer : c) Both (a) and (b)

165. Complete the #statement 5, to invoke the function countrec.

a) CountRec()

b) countrec()

c) Countrec()

d) All of these

Answer : a) CountRec()

166. Which of the following command is used to open a file “d:\mycstutorial\sample.txt” for writing as well as reading in binary format only ?

a) fout = open(“d:\\mycstutorial\\sample.txt”, ‘wb’)

b) fout = open(“d:\\mycstutorial\\sample.txt”, ‘w+b’)

c) fout = open(“d:\\mycstutorial\\sample.txt”, ‘w+’)

d) All of these

Answer : b) fout = open(“d:\\mycstutorial\\sample.txt”, ‘w+b’)

167. Which of the following command is used to open a file “d:\mycstutorial\sample.txt” for appending i.e. writing new information and leaving the old one also in the file, and as well as for reading in binary format only ?

a) fout = open(“d:\\mycstutorial\\sample.txt”, ‘ab+’)

b) fout = open(“d:\\mycstutorial\\sample.txt”, ‘wrb+’)

c) fout = open(“d:\\mycstutorial\\sample.txt”, ‘awrb’)

d) All of these

Answer : a) fout = open(“d:\\mycstutorial\\sample.txt”, ‘ab+’)

Directions (Q.No 168-172) : Answer the following questions 168 to 172 on the basis of given instruction and code.

Amritya Seth is a programmer, who has recently been given a task to write a python code to perform the following binary file operations with the help of two user defined functions/modules:   [CBSE QB]

AddStudents() to create a binary file called STUDENT.DAT containing student information – roll number,  name and marks (out of 100) of each student.

GetStudents() to display the name and percentage of those students who have a percentage greater than 75.      In case there is no student having percentage > 75 the function displays an appropriate message. The function should also display the average percent.He has succeeded in writing partial code and has missed out certain statements, so he has left certain queries in comment lines.

You as an expert of Python have to provide the missing statements and other related queries based on the following code of Amritya.

Answer any five questions from the below mentioned questions.

import pickle
def AddStudents():
    ______________ #1 statement to open the binary file to write data
 
while True:
    Rno = int(input("Rno :"))
    Name = input("Name : ")
    Percent = float(input("Percent :"))
    L = [Rno, Name, Percent]
    _______________ #2 statement to write the list L into the file
    Choice = input("enter more (y/n): ")
    if Choice in "nN":
        break
    F.close()

def GetStudents():
    Total=0
    Countrec=0
    Countabove75=0
    with open("STUDENT.DAT","rb") as F:
        while True:
            try:
                _________ #3 statement to read from the file
                Countrec+=1
                Total+=R[2]
                if R[2] > 75:
                    print(R[1], " has percent = ",R[2])
                    Countabove75+=1
            except:
                break
        if Countabove75==0:
            print("There is no student who has percentage more than 75")
        average= Total/Countrec
        print("average percent of class = ",average)

AddStudents()
GetStudents()

168. Complete the #statement 1. Which of the following commands is used to open the file “STUDENT.DAT” for writing only in binary format?

a) F = open(“STUDENT.DAT”, ‘wb’)

b) F = open(“STUDENT.DAT”, ‘w’)

c) F = open(“STUDENT.DAT”, ‘wb+’)

d) F = open(“STUDENT.DAT”, ‘w+’)

Answer : a) F = open(“STUDENT.DAT”, ‘wb’)

169. Complete the #statement 2. Which of the following commands is used to write the list L into the binary file, STUDENT.DAT?

a) pickle.write(L, f)

b) pickle.write(f, L)

c) pickle.dump(L, F)

d) pickle.dump(F, L)

Answer : c) pickle.dump(L, F)

170. Complete the #statement 3. Which of the following commands is used to read each record from the binary file STUDENT.DAT?

a) R = pickle.load(f)

b) R = pickle.load(F)

c) R = pickle.load(L)

d) R = pickle.load(F, L)

Answer : b) R = pickle.load(F)

171. Which of the following statement(s) are correct regarding the file access modes?

a) ‘r+’ opens a file for both reading and writing. File object points to its beginning.

b) ‘w+’ opens a file for both writing and reading. Adds at the end of the existing file if it exists and    creates a new one if it does not exist.

c) ‘wb’ opens a file for reading and writing in binary format. Overwrites the file if it exists and creates a new one if it does not exist.

d) ‘a’ opens a file for appending. The file pointer is at the start of the file if the file exists.

Answer : a) ‘r+’ opens a file for both reading and writing. File object points to its beginning.

172. Which of the following statements correctly explain the function of seek() method?

a) tells the current position within the file.

b) determines if you can move the file position or not.

c) indicates that the next read or write occurs from that position in a file.

d) moves the current file position to a given specified position.

Answer : d) moves the current file position to a given specified position.


More MCQ’s are on the way, Keep visiting 😃 www.mycstutorial.in 😃

Python File Handling (MCQ’s)


Python Revision Tour – Multiple Choice Questions (MCQ’s)


Python Functions – Working with Functions (MCQ’s)


Thanks for visiting. Online Classes are available.

You cannot copy content of this page

Scroll to Top