Programming and Problem Solving Through Python Language Theory Question

File Processing - Set 3

  1. Which is the function to read the remaining lines of the file from a file object infile ?
    1. infile.read(2)
    2. infile.read()
    3. infile.readlines()
    Answer : B
    Explain : infile.read() - reads the remaing line of file.
  2. Which function is used to read all the characters?
    1. readall()
    2. read()
    3. readcharacters()
    4. readchar()
    Answer : B
    Explain : read() is used to read the entire document.
  3. A function used for writing data in the binary format :
    1. write
    2. output
    3. send
    4. dump
    Answer : D
    Explain : dump is used to write in binary file where as load is use to display binary file.
  4. Which of the following is an invalid mode ?
    1. a
    2. ar+
    3. r+
    4. w
    Answer : B
    Explain : a mode - open an existing file for append operation, r+ - open the file reading and writing the pointer at begin, and w - write the file.
  5. Ravi opened a file in python using open( ) function but forgot to specify the mode. In which mode the file will open ?
    1. write
    2. append
    3. read
    4. Both read and write
    Answer : C
    Explain : The default file access mode is read mode.
  6. Fill in the blank.
    Import pickle
    f=open(“data.dat”, „rb‟)
    d=_____________.load(f)
    f.close()
    1. unpickle
    2. pickling
    3. pickle
    4. pick
    Answer : C
    Explain : pickle mdodule is used to store data in binary file
  7. Which statement will move file pointer 10 bytes backward from current position ?
    1. f.seek(-10,0)
    2. f.seek(-10,1)
    3. f.seek(10,0)
    4. None of the above
    Answer : B
    Explain : In this f.seek(-10,1) f is file name , -10 mean backward 10 bytes and 1 is use for current position
  8. In f.seek(offset, whence), A whence value of 0 means :
    1. from the beginning of the file.
    2. current file position
    3. the end of the file as the reference point
    4. None of these
    Answer : A
    Explain : Whence value 0 means - starting point , 1 values means current position of cursor and 2 means the end of file.
  9. Which statement will return one line from a file (file object is „f‟) ?
    1. f.readlines()
    2. f.readline()
    3. f.read()
    4. f.line()
    Answer :readline read only one line of a file. B
    Explain :
  10. Which mode creates a new file if the file does not exist ?
    1. write mode
    2. append mode
    3. both (A) & (B)
    4. none of the above
    Answer : C
    Explain : If a file does not exist, append mode and write creates the file. The key difference between write and append modes is that append does not clear a file's contents.
  11. Which function opens file in python ?
    1. open()
    2. Open()
    3. new()
    4. None of the above
    Answer :A
    Explain : open() function opens a file, and returns it as a file object.
  12. Which statement will read 5 characters from a file(file object „f‟) ?
    1. f.read()
    2. f.read(5)
    3. f.reads(5)
    4. None of above
    Answer : B
    Explain : read(5) is use to read only 5 characters.
  13. To read all contents from file object FILE at once we may use
    1. FILE.read(*)
    2. FILE.readlines()
    3. FILE.read()
    4. D.FILE.read()
    Answer : C
    Explain : read() method is used to read file at once.
  14. readlines () will return
    1. list of characteras
    2. list of strings
    3. list of lines
    4. list of tuple
    Answer : B

Next Set

1 2