Humanities/Arts Exam  >  Humanities/Arts Tests  >  Test: File Handling - Humanities/Arts MCQ

Test: File Handling - Humanities/Arts MCQ


Test Description

15 Questions MCQ Test - Test: File Handling

Test: File Handling for Humanities/Arts 2024 is part of Humanities/Arts preparation. The Test: File Handling questions and answers have been prepared according to the Humanities/Arts exam syllabus.The Test: File Handling MCQs are made for Humanities/Arts 2024 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests for Test: File Handling below.
Solutions of Test: File Handling questions in English are available as part of our course for Humanities/Arts & Test: File Handling solutions in Hindi for Humanities/Arts course. Download more important topics, notes, lectures and mock test series for Humanities/Arts Exam by signing up for free. Attempt Test: File Handling | 15 questions in 15 minutes | Mock test for Humanities/Arts preparation | Free important questions MCQ to study for Humanities/Arts Exam | Download free PDF with solutions
Test: File Handling - Question 1

What happens when the following statement is executed and file1.txt is not present in the current directory?
file_obj = open("file1.txt", "r")
(1) Python creates a new file ‘file1.txt’
(2) Raises a FileNotFoundError
(3) Raises an IOError
(4) Does nothing

Detailed Solution for Test: File Handling - Question 1
  • The code raises a FileNotFoundError because the file ‘file1.txt’ is not present in the current directory.
  • Since the file is being opened in read mode, the file must be available to be opened. If the file would be opened in write mode, then Python would have created a new file with the given name. But that does not happen for the read mode.
  • The IOError occurs when a file exists but it cannot be opened due to some reason.
Test: File Handling - Question 2

Select the statement that is not true for r+ mode.
(1) If the file does not exist, it gives an error.
(2) Both reading and writing can be performed.
(3) If the file does not exist, it creates a new file.
(4) Existing data is not truncated.

Detailed Solution for Test: File Handling - Question 2
  • The r+ mode opens a file in both reading and writing mode.
  • In r+ mode, the existing data is not deleted because this mode also allows the user to read the file.
  • The r+ mode does not create a file in case it does not exist. Even though the write mode allows this, the r+ mode is also used for reading. A non-existent file cannot be read. So, the r+ mode raises an error in a file name is provided which is not present in the current directory.
1 Crore+ students have signed up on EduRev. Have you? Download the App
Test: File Handling - Question 3

What is the full form of CSV file?
1) Colon Separated Values
2) Context Separated Values
3) Comma Separated Values
4) Caps Separated Values

Detailed Solution for Test: File Handling - Question 3

Concept
CSV Full Form:

  • The Comma Separated Value (CSV) format is a text file that contains data. It makes it easier to store data in a table-like format. The CSV extension is used to identify CSV files.
  • CSV files are used to transfer huge databases between applications while adhering to a precise format. CSV files may be opened with any text editor, such as Notepad or Excel.

Characteristics:

  • Each data item is entered on a different line. If the record is too long, it may span numerous lines.
  • The data fields are separated by commas.
  • A set of double quotes contains the fields that contain commas and are separated by double-quotes.
  • The fields containing double quotes are contained in a set of double-quotes.
  • The space characters that appear adjacent to built-in commas are ignored.

Hence the correct answer is Comma Separated Values

Test: File Handling - Question 4

Which of the following option is true?
fp.seek(10, 1)
(1) Move file pointer ten characters behind from the current position.
(2) Move file pointer ten characters ahead from the current position.
(3) Move file pointer ten characters ahead from the beginning of a file.
(4) Move file pointer ten characters behind ahead from the end of a file.

Detailed Solution for Test: File Handling - Question 4

The seek() method :
This method is used to position the file object at a particular position in a file.
The syntax of seek() is:
file_object.seek(offset [ , reference_point])
In the above syntax, offset is the number of bytes by which the file object is to be moved. reference_point indicates the starting position of the file object. That is, with reference to which position, the offset has to be counted.

  • It can have any of the following values:
    • 0 - beginning of the file
    • 1 - current position of the file
    • 2 - end of file
  • By default, the value of reference_point is 0,
    • i.e. the offset is counted from the beginning of the file.

Explanation:
The given sentences,
fp.seek(10, 1)
Here fp is the file object.

  • fp.seek(10, 1) Move file pointer ten characters ahead from the current position.
  • fp.seek(10, 0) Move file pointer ten characters ahead from the beginning of the file.
  • fp.seek(10, 2) Move file pointer ten characters ahead from the end of the file.

​Hence the correct answer is to Move the file pointer ten characters ahead from the current position.

Test: File Handling - Question 5

What is the full form of CSV file?

Detailed Solution for Test: File Handling - Question 5

Concept:
CSV Full Form:

  • The Comma Separated Value (CSV) format is a text file that contains data. It makes it easier to store data in a table-like format. The CSV extension is used to identify CSV files.
  • CSV files are used to transfer huge databases between applications while adhering to a precise format. CSV files may be opened with any text editor, such as Notepad or Excel.

Characteristics:

  • Each data item is entered on a different line. If the record is too long, it may span numerous lines.
  • The data fields are separated by commas.
  • A set of double quotes contains the fields that contain commas and are separated by double-quotes.
  • The fields containing double quotes are contained in a set of double-quotes.
  • The space characters that appear adjacent to built-in commas are ignored.

Hence the correct answer is Comma Separated Values.

Test: File Handling - Question 6

Match the following Set 1 to set 2.

Detailed Solution for Test: File Handling - Question 6

Concept:
The seek() method :
This method is used to position the file object at a particular position in a file.
The syntax of seek() is:
file_object.seek(offset [ , reference_point])
In the above syntax, offset is the number of bytes by which the file object is to be moved. reference_point indicates the starting position of the file object. That is, with reference to which position, the offset has to be counted.

  • It can have any of the following values:
    • 0 - beginning of the file
    • 1 - current position of the file
    • 2 - end of file
  • By default, the value of reference_point is 0,
    • i.e. the offset is counted from the beginning of the file.

Explanation:

  • f.seek(0) Move file pointer to the beginning of a File
  • f.seek(5) Move file pointer five characters ahead from the beginning of a file.
  • f.seek(0, 2) Move file pointer to the end of a File
  • f.seek(5, 1) Move file pointer five characters ahead from the current position.
  • f.seek(-5, 1) Move the file pointer five characters behind from the current position.
  • f.seek(-5, 2) Move the file pointer in the reverse direction. Move it to the 5th character from the end of the file.

Hence the correct answer is a-iii, b-i, c-ii , d-iv.

Test: File Handling - Question 7

The following functions are used to access data randomly.

Detailed Solution for Test: File Handling - Question 7

Concept:
Setting Offsets in a File:
The functions we've learned so far are utilized to access data sequentially from a file. However, if we wish to retrieve data at random, Python has the seek() and tell() methods.
The tell() method:
This function returns a number indicating the file object's current location in the file. The supplied position is the byte location from the start of the file to the current position of the file object.
syntax:
file_object.tell()
The seek() method:
This function is used to place a file object at a certain location within a file. 
syntax:
file_object.seek(offset [, reference_point])
Here offset is the number of bytes by which the file object is to be moved. reference_point indicates the starting position of the file object.
Hence the correct answer is seek() and tell().

Test: File Handling - Question 8

Which one is the following advantage of using with clause while opening a file.

Detailed Solution for Test: File Handling - Question 8

CONCEPT:
To read or write a file, first we need to open it.
Python provides a function open(), which returns a file object with which we can read and write in the file. But in the end, we need to close the file using close() function call.
If we don’t call the close() function, the file will remain open, and its object will be consuming the memory of our process.
Therefore it is a standard practice to close an open file as a closed file reduces the risk of being unwarrantedly modified or read.
The “with statement” creates an execution block and the object created in the with statement will be destroyed when the execution block ends.
Advantages of calling open() using “with statement”:

  • Files gets closed automatically.
  • Fewer chances of bugs due to coding error
  • Open multiple files in a single “with statement”
Test: File Handling - Question 9

A ___________ method is used to position the file object at a particular position in a file.

Detailed Solution for Test: File Handling - Question 9

Concept:
Setting Offsets in a File:
The functions we've learned so far are utilized to access data sequentially from a file. However, if we wish to retrieve data at random, Python has the seek() and tell() methods.
The seek() method:
This function is used to place a file object at a certain location within a file.  seek() method is used to position the file object at a particular position in a file.
syntax:
file_object.seek(offset [, reference_point])
Here offset is the number of bytes by which the file object is to be moved. reference_point indicates the starting position of the file object.
Hence the correct answer is seek().

Test: File Handling - Question 10

Which method is used to rename the file or folder?

Detailed Solution for Test: File Handling - Question 10

Concept:
Python rename() file is a function in Python programming that is used to rename a file or a directory. The Python rename() file function can be used by giving two parameters, src (Source) and dst (Destination) (Destination).
Syntax :
This is the syntax for os.rename() method.
os.rename(src, dst)
Parameters

  • src: The source is the name of the file or directory. It has to exist already.
  • dst: The destination is the new name of the file or directory to be renamed.

Example:
import os os.rename('lmstestbook.txt','lmstestbook.txt')
Hence the correct answer is rename()

Test: File Handling - Question 11

Which of the following is an invalid file mode?

Detailed Solution for Test: File Handling - Question 11

CONCEPT:
To read or write a file in python, we first need to open it using the open() function and the open function will return a file object.
Syntax:
>>>open(file_name, mode)
The "mode" attribute is used to specify the mode in which we require to open the file.
There are several access modes available for the open() function:
"r+" opens the file in read-write mode.
"rb" opens the file as read-only in binary format.
"ab" opens the file for appending in binary mode.
"rw" is an invalid mode.

Test: File Handling - Question 12

The following code will perform which operation?
object.readline(10)

Detailed Solution for Test: File Handling - Question 12

Concept:
Python File readline() Method:
The readlines() are used to read all of the lines at once and return them as string elements in a list. This method is useful for tiny files since it reads the entire file content to memory and then splits it into individual lines. We may go through the list and use the strip() method to remove the newline 'n' character.
Syntax:
file.readline(size)
Here size is Optional. The number of bytes from the line to return. Default -1, which means the whole line.
Explanation:
object.readline(10)
readline reads a line of the file and returns it in the form of the string. It takes a parameter 10, which specifies the maximum number of bytes that will be read. However, does not reads more than one line, even if 10 exceeds the length of the line.
So it read the first 10 characters of line.
Hence the correct answer is to read the first 10 characters of the line.

Test: File Handling - Question 13

Serialisation is also known as____________.

Detailed Solution for Test: File Handling - Question 13

The pickle module implements binary protocols for serializing and de-serializing Python object structures.
Pickling:

  • Serialization is also known as Pickling.
  • Pickling is the process of converting a Python object hierarchy into a byte stream.
  • Serialization is the process of converting an object in memory to a byte stream that can be stored on a disk or sent over a network.
  • The pickling process is also called marshaling.

Example:

The following example serializes data into a binary file.

#!/usr/bin/python

import pickle

data = {
    'a': [1, 4.0, 3, 4+6j],
    'b': ("a red fox", b"and old falcon"),
    'c': {None, True, False}
}

with open('data.bin', 'wb') as f:
    pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)

Unpickling:

  • Deserialization is also known as unpickling.
  • Unpickling is the process of converting a byte stream into an object hierarchy, which is the inverse of Pickling.
  • Deserialization is the process of converting a byte stream to a Python object.
  • The unpickling process is also called unmarshalling.

Example:

In the next example, we unpickle data from a binary file.

#!/usr/bin/python

import pickle

with open('data.bin', 'rb') as f:

    data = pickle.load(f)

    print(data)

Hence the correct answer is pickling.

Test: File Handling - Question 14

You are given the text file, file1.txt whose contents are:

hello everyone

today is a monday

all the best for your exams

 

What will be the output of the following code?

myfile = open("file1.txt", "r")

str = myfile.readline(20)

print(str)

myfile.close()

Detailed Solution for Test: File Handling - Question 14
  • The readline() function reads the number of bytes specified, up to the end of the first line only.
  • That is, if the number of character exceeds the first line, the readline() function does not read them.
  • The readline() function does not read the \n escape sequence.
  • So, the given code only returns the first line even though it is less than 20 bytes or characters. The \n character is not included within the return value.

Important points:

  • The read() function reads exactly as many bytes as specified.
  • The readlines() function always reads complete lines. If the specified number of characters ends in the middle of a line, the function still reads that complete line up to the \n character. Also, the readlines() function returns the \n character.
Test: File Handling - Question 15

With reference to file handling in Python, which of the following statement(s) is/are true?
a. The file seek points are 0, 1, and 2.
b. The seek() function works differently for text and binary files.
c. When a file is opened in binary mode, the seek() method can have a negative offset value.
d. The offset in the seek() function specifies the number of bits by which the file pointer is to be moved.

Detailed Solution for Test: File Handling - Question 15
  • The seek() function can take reference points from where it starts counting the offset value. The reference points are start of file, current position, and end of file. The values to specify each of these positions are 0, 1, and 2 respectively.
  • The seek() function sets the file pointer at the specified position in a file. It performs the same task for a text file as well as a binary file.
  • Usually, the offset value in the seek() function is a greater than or equal to 0. However, if the file is opened in binary mode, then the offset value can be negative.
  • The offset in the seek() function specifies the number of bytes by which the file pointer is to be moved. Each character in a file is of 1 byte. So, the seek() function specifies how many characters to move.
Information about Test: File Handling Page
In this test you can find the Exam questions for Test: File Handling solved & explained in the simplest way possible. Besides giving Questions and answers for Test: File Handling, EduRev gives you an ample number of Online tests for practice

Top Courses for Humanities/Arts

Download as PDF

Top Courses for Humanities/Arts