CBSE CS

cbse cs logo

Home

Data files in Python

Topic Covered

  • Introduction to files, types of files (Text file, Binary file, CSV file),

  • relative and absolute paths

  • Text file: opening a text file, text file open modes (r, r+, w, w+, a, a+), closing a text file,

  • opening afile using with clause, writing/appending data to a text file using write() and writelines(),

  • reading from a text file using read(), readline() and readlines(),

  • seek and tell methods, manipulation of data in a text file

MCQs on Data File Handling

1 Every file has its own identity associated with it. Which is known as –

  1. icon
  2. extension
  3. format
  4. file type

2 Which of the following is not a known file type?

  1. .pdf
  2. jpg
  3. mp3
  4. txp

3 In f=open(“data.txt”, “r”), r refers to __________.

  1. File handle
  2. File object
  3. File Mode
  4. Buffer

EOL stands for

  1. End Of Line
  2. End Of List
  3. End of Lines
  4. End Of Location

Which of the following file types allows to store large data files in the computer memory?

  1. Text Files
  2. Binary Files
  3. CSV Files
  4. None of these

Which of the following file types can be opened with notepad as well as ms excel?

  1. Text Files
  2. Binary Files
  3. CSV Files
  4. None of these

Which of the following is nor a proper file access mode?

  1. close
  2. read
  3. write
  4. append

 

To read 4th line from text file, which of the following statement is true?

  1. dt = f.readlines();print(dt[3])
  2. dt=f.read(4) ;print(dt[3])
  3. dt=f.readline(4);print(dt[3])
  4. All of these

9 Which of the following function flushes the files implicitly?

  1. flush()
  2. close()
  3. open()
  4. fflush()

Which of the following functions flushes the data before closing the file?

  1. flush()
  2. close()
  3. open()
  4. fflush()

In F=open(“MyFile.txt”) , name of file object is

a.open

b.MyFile.txt

c.F

d.F=open()

Default EOL character in Python.

  1. ‘\n’
  2. ‘\r’
  3. ‘’
  4. ‘\t’

Which of the following is not a file extension for text files?

  1. .txt
  2. .ini
  3. .rtf
  4. .DAT

What is the first thing to do before performing any functions on a text file?

  1. Import modules
  2. Open file
  3. Read file
  4. Print the name of the file

What is a file object?

  1. It serves as a link to the file.
  2. It is a file present in a computer.
  3. A keyword
  4. A module in python

Which is not a correct file mode for text files?

  1. a
  2. ar
  3. a+
  4. r+

What does the prefix r in front of a string do?

  1. It makes the string a raw string
  2. It opens the file in read mode
  3. It converts the file into text file
  4. It creates the file if it doesn’t exist

A file object is also known as

  1. File handle
  2. File copy
  3. File directory
  4. File link

How to open a text file in read mode only?

  1. r
  2. r+
  3. rb+
  4. rw+

How to open a text file in write and read mode?

  1. r+
  2. a+
  3. wr
  4. wb

Syntax for closing a file:

  1. closefile(<file object>)
  2. <fileobject>.close()
  3. <filename>.closer()
  4. closefile.<fileobject>

22.Which method can not be used to read from files?

  1. read()
  2. readlines()
  3. readlines(<filename>)
  4. readline()

What does strip() function do?

  1. Removes the trailing or leading spaces, if any.
  2. Deletes the file
  3. Remove the file object
  4. Removes all the spaces between words

readlines() gives the output as

  1. List
  2. Tuple
  3. String
  4. Sets

When reading a file using the file object, what method is best for reading the entire file into a single string?

  1. readline()
  2. read_file_to_str()
  3. read()
  4. readlines()

Which file can open in any text editor and is in human readable form?

  1. Binary files
  2. Text files
  3. Data files
  4. Video files

Which function breaks the link of file-object and the file on the disk?

  1. close( )
  2. open( )
  3. tell( )
  4. readline( )

Which function reads the leading and trailing spaces along with trailing newline character (‘\n’) also while reading the line?

  1. readlines( )
  2. readline( )
  3. read( )
  4. flush( )

Which mode is used to retain its previous data and allowing to add new data?

  1. write mode
  2. read mode
  3. open mode
  4. append mode

Which function forces the writing of data on disc still pending in output buffer?

  1. seek( )
  2. tell( )
  3. flush( )
  4. write( )

Syntax for flush( ) function is:

  1. <fileOobject>(flush( ))
  2. flush( ).<fileobject>
  3. <fileObject>.flush( )
  4. flush( ).<file-object>

 

Which function returns the entire file content in a list where each line is one item of the list?

  1. readlines( )
  2. readline( )
  3. output( )
  4. Input( )

Which function is used to remove the given character from trailing end i.e. right end?

  1. strip( )
  2. remove( )
  3. Istrip( )
  4. rstrip( )

Sometimes the last lap of data remains in buffer and is not pushed onto disk until a __________ operation is performed.

  1. dump( )
  2. close( )
  3. load( )
  4. open( )

The position of a file-pointer is governed by the_________.

  1. File mode b. append mode
  2. write mode d. open mode

In which mode the file must exist already, otherwise python raises an error? a. read mode

  1. write mode
  2. binary mode
  3. None of these

 

What is the prefix r stands for in file path?

  1. raw string
  2. read
  3. write
  4. append

In which mode______ if the file does not exist, then the file is created?

  1. read write mode
  2. read mode
  3. write mode
  4. All of these

Which option is correct about this program?

f=open(“ss.txt”,”wb”)

print(“Name of the file:”,f.name)

f.flush()

f.close()

  1. Compilation error
  2. Runtime error
  3. No output
  4. Flushes the file when closing them

What is the output of the following?

import sys

sys.stdout.write(‘Hello\n’)

sys.stdout.write(‘Python\n’)

  1. error
  2. Runtime error
  3. Hello Python
  4. Hello

 

Which function is used to read all the characters in text files?

  1. read( )
  2. readcharacters( )
  3. readall( )
  4. readchar( )

Which function is used to read all the lines?

  1. read( )
  2. readall( )
  3. readlines( )
  4. readline( )

In which format does the readlines( ) function give the output?

  1. Integer type
  2. list type
  3. string type
  4. tuple type

In which format does the read( ) function give the output?

  1. Integer type
  2. string type
  3. list type
  4. tuple type

Which function is used to write a list of strings in a file?

  1. writestatement()
  2. writelines()
  3. writefulline()
  4. writeline()

Which function is used to write all the characters?

  1. writechar()
  2. writecharacters()
  3. write()
  4. writeall()

What is the correct syntax of open() function?

  1. file=open(file_name[,access_mode][,buffering])
  2. fileobject=open(file_name[,access_model][,buffering])
  3. fileobject=filename.open()
  4. none of the mentioned

48.In file handling, what does means “r”, “a”?

  1. append, read
  2. read, append
  3. read, add
  4. None of the mentioned

49.The default file open mode is….

  1. w
  2. r+
  3. w+
  4. r

What is the difference between r+ and w+ modes?

  1. In r+ mode, file length truncates to zero.
  2. In w+ mode, file length truncates to zero either file exists or not.
  3. No difference
  4. Depends on the operating system

A file maintains a __________ which tells the current position in the file where writing or reading will take place.

  1. line
  2. file pointer
  3. list
  4. order

 

Which of the following statements is true regarding the opening modes of a file?

a. While opening a file for reading, if the file does not exist, an error occurs.

b. While opening a file for writing ,if the file does not exist, an error occurs.

c. While opening a file for reading, if the file does not exist, a new file is created.

d.None of the above.

53.To force python to write the contents of file buffer on to storage file,……..method may be used.

  1. buffer()
  2. flush()
  3. close()
  4. write()

Which of the following statements are true?

a) When you open a file for reading, if the file does not exist, an error occurs.

b) When you open a file for writing, if the file does not exist, a new file is created.

c) When you open a file for writing, if the file exists, the existing file content is overwritten with the new content.

d) All of the these

55.To read the next line of the file from a file object f1, we use:

a) f1.read(2)

b) f1.read()

c) f1.readline()

d) f1.readlines()

error: Content is protected !!