Programming and Problem Solving Through Python Language Theory Question

File Processing

  1. Which Python function is used to open a file for reading ?
    1. open("file.txt", "w")
    2. open("file.txt", "r")
    3. read("file.txt")
    4. write("file.txt", "r")
    Answer : B
    Explain : open("filename", "mode") here file.txt is file nama and r is read mode.
  2. Which method is used to read the entire content of a file as a single string ?
    1. read()
    2. readline()
    3. readlines()
    4. readall()
    Answer : A
    Explain : read() function is used to read entire data in a file. readline() is used to read line by line. The readlines() method in Python is used for reading all lines from a file and returning them as a list of strings.
  3. What is the purpose of the "w" mode when opening a file ?
    1. Read mode
    2. Write mode
    3. append mode
    4. Binary mode
    Answer : B
    Explain : w mode is used for write text file.
  4. 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 is used before .load(filename).
  5. Which mode should be used when opening a file for both reading and writing ?
    1. "r"
    2. "w"
    3. "a"
    4. "rw"
    Answer : D
    Explain : rw is used for read and write.
  6. What is the purpose of the seek() method in file handling ?
    1. It moves the file pointer to a specified position.
    2. It opens a file in read mode.
    3. It appends content to the end of the file.
    4. It closes the file.
    Answer : A
    Explain : seek () change and modify the position of pointer.
  7. Which method is used to write a list of strings to a file ?
    1. write_list()
    2. writelines()
    3. write_lines()
    4. appendlines()
    Answer : B
    Explain : writelines() is used to write the sequence of string to a file.
  8. What happens if you try to open a file using the "w" mode that does not exist ?
    1. The file will be created if it doesn't exist.
    2. An error will occur since the file must already exist.
    3. The file will be opened in read mode automatically.
    4. The program will wait until the file is created.
    Answer : A
    Explain : "w" the file will create if not created.
  9. Which function should be used to check if a file exists before trying to open it ?
    1. check_file_exists()
    2. file_exists()
    3. os.path.exists()
    4. isfile()
    Answer : C
    Explain : exists() method is used to check whether the given path points to an existing file or directory or not. It returns boolean value if exist true, if not then false.
  10. which statement will return one line from a file (file object is ‘f’) ?
    1. f.readline()
    2. f.readlines()
    3. f.read()
    4. f.line()
    Answer : A
    Explain : readline() is used to read line by line.
  11. Which of the following function takes two arguments ?
    1. load()
    2. dump()
    3. both of above
    4. None of the above
    Answer : B
    Explain : Note that dump() takes two positional arguments: (1) the data object to be serialized, and (2) the file-like object to which the bytes will be written.
  12. Which mode creates a new file if the file does not exist ?
    1. write mode
    2. append mode
    3. both
    4. None
    Answer : C
    Explain : Both write mode and append mode creates file if not exist.
  13. which statement will read 5 characters from a file ?
    1. f.read()
    2. f.read(5)
    3. f.reads(5)
    4. None of above
    Answer : B
    Explain : f.read(5) only read 5 characters of file.
  14. 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()
    4. infile.readline()
    Answer : B
    Explain : read() is used to read the entire data in a file.
  15. What will be the output of following code ?
    f = open("demo.txt","r")
    print(f.tell())
    f.close()
    
    1. 1
    2. 2
    3. -1
    4. 0
    Answer : D
    Explain : tell() is used to tell the curent position of pointer. When we open the curent position is at starting which is 0.

Next Set

2 3