0% found this document useful (0 votes)
3 views5 pages

Chapter2 - File Handling in Python

This document covers file handling in Python, including various file types, modes, and methods for reading and writing data. It includes multiple-choice questions and fill-in-the-blank exercises to test knowledge on file operations, such as opening, closing, and manipulating files. Key concepts include the use of the Pickle module for serialization and deserialization of Python objects.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Chapter2 - File Handling in Python

This document covers file handling in Python, including various file types, modes, and methods for reading and writing data. It includes multiple-choice questions and fill-in-the-blank exercises to test knowledge on file operations, such as opening, closing, and manipulating files. Key concepts include the use of the Pickle module for serialization and deserialization of Python objects.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CHAPTER-2

FILE HANDLING IN PYTHON

I Select the correct answer from the choices given:


1) The files are stored permanently in a computer system on (E)

a) Random Access Memory b) Cache Memory


c) CPU d)Secondary storage media
2) Which of the following file extensions is used for plain text files? (A)
a) .exe b) .txt c) .jpg d) .mp3
3) The following device is used to store data permanently entered in Python program (E)
a) Primary storage b) Non-volatile Memory
c) Temporary storage d) Secondary storage devices
4) Which of the following is data file ? (E)
a) computer file b) meta file c)image file d) text file and binary file
5) Computers store every file as a collection of (E)
a) 0’s and 1’s b) 0’s and 2’s c) 0’s and 9’s d) ASCII
6) Each line of a text file is terminated by a special character (D)
a) EOS b) EOP
c) EOF d) EOL
7) Document created in notepad is an example of (A)
a) Text file b) Binary file c) Word file d) system file
8) The default EOL character in Python is (A)
a) \k b) \v c) \b d) \n
9) What is the primary purpose of file handling in Python? (E)
a) To process data faster
b) To store data temporarily
c) To permanently store data and output for reuse
d) To display output on the screen
10) The following file can be understood as a sequence of characters consisting of alphabets, numbers and
other special symbols. (E)
a) Text file b) binary file c) numeric file d) alphabets file
11) What happens if you try to open a binary file in a text editor? (E)
a) The text editor will display the file’s content in ASCII format.
b) The text editor will show the file as a series of 0s and 1s.
c) The text editor will display garbage values.
d) The text editor will read the file successfully if it is an image.

SUBJECT: 41 COMPUTER SCIENCE


12) Which of the following is the correct syntax to open a file in Python? (D)
a) fobj=open(“filename”, accessmode) : b) fileobj = open(filename, accessmode)
c) open=file(“[Link]”, “accessmode”) d) [Link](“filename”)
13) What type of software is required to view or modify the contents of a binary file? (D)
a) Text editor b) Any word processor
c) Spread sheet application d) Specific software designed for handling binary files
14) Binary files are also stored in terms of (A)
a) 0’s and 1’s b) 0’s and 2’s c) 0’s and 9’s d) ASCII
15) Which function in Python is used to open a file for writing data? (A)
a) [Link]() b) open() c) openfile() d) write()
16) How are binary files stored in a computer? (E)
a) As sequences of words. b) As sequences of bytes.
c) As sequences of characters. d) As a sequence of symbols.
17) What does the <file>.mode attribute return in Python? (E)
a) The name of the file
b) The current file pointer position
c) The access mode in which the file was opened
d) The contents of the file
18) Which of the following access modes opens a file for reading only? (E)
a) r b) w b) r+ d) a
19) Which mode is used to Opens the file in read-only mode in python ? (E)
a) <r+> b) <w> c) <a> d) <r>
20) Which mode is used to Opens the file in binary and read-only mode ? (A)
a) <rb> b) <wb> c) <ab> d) <rb+>
21) Which of the following modes allows you to open a file for both reading and writing? (A)
a) <r+> b) <a+) c) <wb+> d) <a+>
22) In Python, what happens when the file you are trying to open in write mode (‘w’) does not exist? (E)
a) An error is thrown. b) A new empty file is created.
c) A new empty file is not created. d) Nothing happens.
23) In Python, when you open a file in ‘w’ mode, what happens to the file if it already exists? (E)
a) The file will be overwritten with the new data
b) The file will remain unchanged
c) A new file will be created and the data will be written into it
d) Python will raise an error

SUBJECT: 41 COMPUTER SCIENCE


24) What does the mode ‘w’ do when opening a file in Python? (E)
a) Opens the file for reading only
b) Opens the file for writing and creates the file if it doesn’t exist
c) Opens the file for both reading and writing
d) Opens the file for appending data
25) Which of the following file modes in Python is used to open a file for appending data without
overwriting the existing content? (E)
a) <r> b) <w> c) <a> d) <x>
26) Which of the following file modes in Python is used to open a file for appending data overwriting the
existing content? (A)
a) <r> b) <w> c) <a> d) <x>
27) The following file modes in Python is used to Opens the file in append and read mode. (A)
a) <r> b><w+> c) <r+> d) <a+>
28) What is the purpose of the close() function in Python file handling? (E)
a) To save data to the file
b) To stop reading data from the file
c) To ensure that the file is closed after reading or writing
d) To open a file for further reading or writing
29) When using the with statement to open a file, what happens when the block inside with is exited? (E)
a) The file must be closed manually. b) The file is automatically closed.
c) The file is closed only if there’s an error. d) The file remains open until the program ends.
30) After opening a file with the open() function, how do you close the file to free up resources? (E)
a) file_object.close() b) file_object.end() c) file_object.finish() d) file_object.closefile()
31) The correct syntax of file close is (A)
a) file(close) b) [Link]() c) [Link]( ) d) close(file)
32) Which of the following is the correct syntax to open a file in write mode? (D)
a) obj=open(“[Link]”, ‘r’) b) obj=open(“[Link]”, ‘w’)
c) obj=open(“[Link]”, ‘rw’) d) obj=open(“[Link]”, “x”)
33) What does the write() method return ? (E)
a) Writing is successful b) It does not return the number of characters
c) The number of characters written d) Return file object
34) What does the writelines() method return ? (A)
a) Writtern only binary digits b) The number of characters read
c) The number of characters written d) It does not return the number of characters

35) Which of the following method is used to write multiple strings to a file. (E)
a) write() b) writing() c) writestring() d) writelines()

SUBJECT: 41 COMPUTER SCIENCE


36) What does the read() method do in Python? (E)
a) It reads only the firse line b) Reads a specified number of bytes from a file
c) Reads a single line from a file d) Write a specified number of bytes from a file
37) Which of the following is true about the read() method without any arguments? (E)
a) It reads only the first line b) It reads a fixed number of characters
c) It reads the entire file d) It throws an error
Correct Answer: C. It reads the entire file
38) What does the readlines() method do in Python? (E)
a) Reads the entire file as a single string
b) Reads the file in all the lines and returns a list of lines
c) Reads the first line of the file
d) Reads all lines and prints them
39) Which of the following statements is TRUE regarding the readline() method? (E)
a) It can be used to read a specific number of bytes from the file.
b) It only reads the first line of a file regardless of the value of n.
c) It reads the file until the newline character is reached, then stops.
d) It modifies the content of the file while reading it.
40) Which method returns the current position of the file object in python ? (A)
a) [Link]() b) [Link]() c) [Link]() d) [Link]()
41) If you want to move the file pointer to a specific position, which method should be used in
combination with tell()? (E)
a) seek() b) open() c) read() d) close()
42) What will happen if you call writelines() on an empty list? (A)
a) It will raise an exception. b) It will write an empty line to the file.
c) It will not write anything to the file. d) It will overwrite the file with an empty string.
43) If you want to read a file line by line in Python, which of the following would you use? (E)
a) [Link]() b) [Link]() c) [Link]() d) [Link]()
44) What is the primary purpose of Pickle in Python? (E)
a) To store Python objects in a human-readable format
b) To compress Python objects to save space
c) To serialize and store Python objects for later retrieval
d) To perform mathematical operations on Python objects
45) Which of the following modules is used to serialize and deserialize Python objects? (E)
a) json b) pickle c) os d) sys
46) Which of the following types of objects can be serialized using Pickle? (E)
a) Lists b) Tuples c) Dictionaries d) All of these

SUBJECT: 41 COMPUTER SCIENCE


47) What is the primary purpose of the dump() method in Python? (E)
a) To convert a Python object into a string
b) To convert Python object into a binary format for saving to a file
c) To save the state of a Python object as text data
d) To deserialize a Python object from a binary file
48) Which module provides the dump() method to serialize objects in Python? (E)
a) pickle b) json c) marshal d) os
49) In which case would you need to use the dump() method? (E)
a) When you want to convert a binary file into a text file
b) When you need to store Python objects in a binary file
c) When you want to retrieve Python objects from a file
d) When you are reading a file’s content line by line
50) What is the correct syntax for using the dump() method? (D)
a) dump(data_object, file_object) b) dump(file_object, data_object)
c) file_object.dump(data_object) d) data_object.dump(file_object)
51) In the context of Pickling, what does the term “serializing” mean? (A)
a) Making an object immutable
b) Converting an object into a byte stream for storage
c) Encrypting an object to prevent unauthorized access
d) Changing the type of an object for easier storage
52) Which of the following functions does Pickle provide for saving an object to a file? (A)
a) [Link]() b) [Link]() c) [Link]() d) [Link]()
53) Which of the following functions does Pickle provide for loading a saved object from a file? (E)
a) [Link]() b) [Link] ()
c) [Link]() d) [Link]()
54) In the Pickling process, which term refers to reading a serialized object from a file and restoring it to
its original state? (A)
a) Serialization b) Deserialization c) Encryption d) Compression
55) What does the flush() method do when used with file handling in programming? (E)
a) Closes the file immediately“ b) Clears the buffer and writes the contents to the file
c) Deletes the file contents permanently d) Opens the file for writing

II Fill in the blanks


1) Secondary storage media are used to permanently store data for later access in (Solid State Drive)
2) A file is a named location on storage media where data is permanently stored. (Secondary)
3) A text file consists of readable characters. (human)
4) Text files store data using bytes for schemes. (ASCII)

SUBJECT: 41 COMPUTER SCIENCE

You might also like