Self Studies

File Handling Test - 1

Result Self Studies

File Handling Test - 1
  • Score

    -

    out of -
  • Rank

    -

    out of -
TIME Taken - -
Self Studies

SHARING IS CARING

If our Website helped you a little, then kindly spread our voice using Social Networks. Spread our word to your readers, friends, teachers, students & all those close ones who deserve to know what you know now.

Self Studies Self Studies
Weekly Quiz Competition
  • Question 1
    1 / -0.25

    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

    Solution

    • 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.

  • Question 2
    1 / -0.25

    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.

    Solution

    • 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.

  • Question 3
    1 / -0.25

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

    Solution

    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

  • Question 4
    1 / -0.25

    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.

    Solution

    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.

  • Question 5
    1 / -0.25

    What is the full form of CSV file?

    Solution

    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.

  • Question 6
    1 / -0.25

    Match the following Set 1 to set 2.

    Solution

    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.

  • Question 7
    1 / -0.25

    The following functions are used to access data randomly.

    Solution

    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().

  • Question 8
    1 / -0.25

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

    Solution

    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 ”

  • Question 9
    1 / -0.25

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

    Solution

    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().

  • Question 10
    1 / -0.25

    Which method is used to rename the file or folder?

    Solution

    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()

  • Question 11
    1 / -0.25

    Which of the following is an invalid file mode?

    Solution

    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.

  • Question 12
    1 / -0.25

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

    Solution

    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.

  • Question 13
    1 / -0.25

    Serialisation is also known as____________.

    Solution

    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.

  • Question 14
    1 / -0.25

    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()

    Solution

    • 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.

  • Question 15
    1 / -0.25

    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.

    Solution

    • 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.

Self Studies
User
Question Analysis
  • Correct -

  • Wrong -

  • Skipped -

My Perfomance
  • Score

    -

    out of -
  • Rank

    -

    out of -
Re-Attempt Weekly Quiz Competition
Self Studies Get latest Exam Updates
& Study Material Alerts!
No, Thanks
Self Studies
Click on Allow to receive notifications
Allow Notification
Self Studies
Self Studies Self Studies
To enable notifications follow this 2 steps:
  • First Click on Secure Icon Self Studies
  • Second click on the toggle icon
Allow Notification
Get latest Exam Updates & FREE Study Material Alerts!
Self Studies ×
Open Now