PYTHON File Handling MCQs

  1. Which function is used to open a file in Python?
    a) file.open()
    b) open()
    c) fopen()
    d) read()
    View Answer

    Correct answer: B — open()

  2. What will be printed?
    1 f = open("eduinq.txt","w")
    2 print(f.mode)
    a) w
    b) r
    c) a
    d) Error
    View Answer

    Correct answer: A — w

  3. Which of the following is used to read file contents?
    a) f.read()
    b) f.write()
    c) f.open()
    d) f.close()
    View Answer

    Correct answer: A — f.read()

  4. What is the output of print(open("eduinq.txt").closed)?
    a) True
    b) False
    c) Error
    d) None
    View Answer

    Correct answer: B — False

  5. Which of the following is used to close a file?
    a) f.end()
    b) f.close()
    c) f.stop()
    d) f.exit()
    View Answer

    Correct answer: B — f.close()

  6. What will be printed?
    1 f = open("eduinq.txt","w")
    2 f.write("Eduinq")
    3 f.close()
    4 f = open("eduinq.txt","r")
    5 print(f.read())
    a) Eduinq
    b) Error
    c) None
    d) Blank
    View Answer

    Correct answer: A — Eduinq

  7. Which of the following is used to read one line at a time?
    a) f.readline()
    b) f.read()
    c) f.readlines()
    d) f.readall()
    View Answer

    Correct answer: A — f.readline()

  8. What is the output of print(open("eduinq.txt","r").mode)?
    a) r
    b) w
    c) a
    d) Error
    View Answer

    Correct answer: A — r

  9. Which of the following is used to append data to file?
    a) "a" mode
    b) "w" mode
    c) "r" mode
    d) "x" mode
    View Answer

    Correct answer: A — "a" mode

  10. What will be printed?
    1 f = open("eduinq.txt","a")
    2 f.write("Python")
    3 f.close()
    4 f = open("eduinq.txt","r")
    5 print(f.read())
    a) EduinqPython
    b) Python
    c) Error
    d) None
    View Answer

    Correct answer: A — EduinqPython

  11. Which of the following is used to read all lines into list?
    a) f.readlines()
    b) f.read()
    c) f.readline()
    d) f.readall()
    View Answer

    Correct answer: A — f.readlines()

  12. What is the output of print(open("eduinq.txt","rb").mode)?
    a) rb
    b) r
    c) b
    d) Error
    View Answer

    Correct answer: A — rb

  13. Which of the following is used to write binary data?
    a) "wb" mode
    b) "rb" mode
    c) "ab" mode
    d) "xb" mode
    View Answer

    Correct answer: A — "wb" mode

  14. What will be printed?
    1 f = open("eduinq.txt","w")
    2 f.write("Eduinq\nPython")
    3 f.close()
    4 f = open("eduinq.txt","r")
    5 print(f.readlines())
    a) ['Eduinq\n','Python']
    b) ['Eduinq','Python']
    c) Error
    d) None
    View Answer

    Correct answer: A — ['Eduinq\n','Python']

  15. Which of the following is used to check file name?
    a) f.name
    b) f.filename
    c) f.file
    d) f.path
    View Answer

    Correct answer: A — f.name

  16. What is the output of print(open("eduinq.txt","r").read(3))?
    a) Edu
    b) Eduinq
    c) Error
    d) None
    View Answer

    Correct answer: A — Edu

  17. Which of the following is used to move file pointer?
    a) f.seek()
    b) f.move()
    c) f.goto()
    d) f.jump()
    View Answer

    Correct answer: A — f.seek()

  18. What will be printed?
    1 f = open("eduinq.txt","w")
    2 f.write("Eduinq")
    3 f.seek(0)
    4 f.write("Py")
    5 f.close()
    6 f = open("eduinq.txt","r")
    7 print(f.read())
    a) Pyinq
    b) Pyduinq
    c) Eduinq
    d) Error
    View Answer

    Correct answer: A — Pyinq

  19. Which of the following is used to get current file pointer position?
    a) f.tell()
    b) f.pos()
    c) f.loc()
    d) f.index()
    View Answer

    Correct answer: A — f.tell()

  20. What is the output of print(open("eduinq.txt","r").readline()) if file contains "Eduinq"?
    a) Eduinq
    b) Error
    c) None
    d) Blank
    View Answer

    Correct answer: A — Eduinq

  21. Which of the following is used to open file in exclusive creation mode?
    a) "x"
    b) "w"
    c) "r"
    d) "a"
    View Answer

    Correct answer: A — "x"

  22. What will be printed?
    1 f = open("eduinq.txt","x")
    2 f.write("Eduinq")
    3 f.close()
    4 f = open("eduinq.txt","r")
    5 print(f.read())
    a) Eduinq
    b) Error if file exists
    c) None
    d) Blank
    View Answer

    Correct answer: A — Eduinq

  23. Which of the following is used to open file in read/write mode?
    a) "r+"
    b) "w+"
    c) "a+"
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  24. What is the output of print(open("eduinq.txt","r+").mode)?
    a) r+
    b) w+
    c) a+
    d) Error
    View Answer

    Correct answer: A — r+

  25. Which of the following is used to open file in text mode?
    a) "t"
    b) "b"
    c) "x"
    d) "r"
    View Answer

    Correct answer: A — "t"

  26. What will be printed?
    1 f = open("eduinq.txt","wt")
    2 f.write("Eduinq")
    3 f.close()
    4 f = open("eduinq.txt","rt")
    5 print(f.read())
    a) Eduinq
    b) Error
    c) None
    d) Blank
    View Answer

    Correct answer: A — Eduinq

  27. Which of the following is used to open file in binary mode?
    a) "b"
    b) "t"
    c) "x"
    d) "r"
    View Answer

    Correct answer: A — "b"

  28. What is the output of print(open("eduinq.txt","rb").read()) if file contains "Eduinq"?
    a) b'Eduinq'
    b) Eduinq
    c) Error
    d) None
    View Answer

    Correct answer: A — b'Eduinq'

  29. Which of the following is used to write multiple lines?
    a) f.writelines()
    b) f.write()
    c) f.writeline()
    d) f.writeall()
    View Answer

    Correct answer: A — f.writelines()

  30. What will be printed?
    1 f = open("eduinq.txt","w")
    2 f.writelines(["Eduinq\n","Python\n"])
    3 f.close()
    4 f = open("eduinq.txt","r")
    5 print(f.read())
    a) Eduinq\nPython\n
    b) Eduinq Python
    c) Error
    d) None
    View Answer

    Correct answer: A — Eduinq\nPython\n

  31. Which of the following is used to automatically close file?
    a) with open() as f
    b) auto.close()
    c) file.close()
    d) None
    View Answer

    Correct answer: A — with open() as f

  32. What is the output of print(f.closed) after using with open()?
    a) True
    b) False
    c) Error
    d) None
    View Answer

    Correct answer: A — True

  33. Which of the following is used to check file existence?
    a) os.path.exists()
    b) os.exists()
    c) file.exists()
    d) f.exists()
    View Answer

    Correct answer: A — os.path.exists()

  34. What will be printed?
    1 import os
    2 print(os.path.exists("eduinq.txt"))
    a) True or False
    b) Error
    c) None
    d) Blank
    View Answer

    Correct answer: A — True or False

  35. Which of the following is used to delete file?
    a) os.remove()
    b) os.delete()
    c) file.delete()
    d) f.remove()
    View Answer

    Correct answer: A — os.remove()

  36. What is the output of print(os.remove("eduinq.txt"))?
    a) None
    b) True
    c) Error
    d) Blank
    View Answer

    Correct answer: A — None

  37. Which of the following is used to rename file?
    a) os.rename()
    b) os.move()
    c) file.rename()
    d) f.rename()
    View Answer

    Correct answer: A — os.rename()

  38. What will be printed?
    1 import os
    2 os.rename("eduinq.txt","python.txt")
    3 print(os.path.exists("python.txt"))
    a) True
    b) False
    c) Error
    d) None
    View Answer

    Correct answer: A — True

  39. Which of the following is used to get file size?
    a) os.path.getsize()
    b) os.size()
    c) file.size()
    d) f.size()
    View Answer

    Correct answer: A — os.path.getsize()

  40. What is the output of print(os.path.getsize("eduinq.txt")) if file contains "Eduinq"?
    a) 6
    b) 7
    c) Error
    d) None
    View Answer

    Correct answer: A — 6

  41. Which of the following is used to copy file?
    a) shutil.copy()
    b) os.copy()
    c) file.copy()
    d) f.copy()
    View Answer

    Correct answer: A — shutil.copy()

  42. What will be printed?
    1 import shutil
    2 shutil.copy("eduinq.txt","copy.txt")
    3 print(open("copy.txt","r").read())
    a) Eduinq
    b) Error
    c) None
    d) Blank
    View Answer

    Correct answer: A — Eduinq

  43. Which of the following is used to move file?
    a) shutil.move()
    b) os.move()
    c) file.move()
    d) f.move()
    View Answer

    Correct answer: A — shutil.move()

  44. What is the output of print(open("eduinq.txt","r").readline()) if file contains "Eduinq\nPython"?
    a) Eduinq
    b) Python
    c) Error
    d) None
    View Answer

    Correct answer: A — Eduinq

  45. Which of the following is used to truncate file?
    a) f.truncate()
    b) f.cut()
    c) f.clear()
    d) f.remove()
    View Answer

    Correct answer: A — f.truncate()

  46. What will be printed?
    1 f = open("eduinq.txt","w")
    2 f.write("EduinqPython")
    3 f.truncate(6)
    4 f.close()
    5 f = open("eduinq.txt","r")
    6 print(f.read())
    a) Eduinq
    b) EduinqPython
    c) Error
    d) None
    View Answer

    Correct answer: A — Eduinq

  47. Which of the following is used to flush file buffer?
    a) f.flush()
    b) f.clear()
    c) f.empty()
    d) f.buffer()
    View Answer

    Correct answer: A — f.flush()

  48. What is the output of print(open("eduinq.txt","r").read()) if file is empty?
    a) ""
    b) None
    c) Error
    d) Blank
    View Answer

    Correct answer: A — ""

  49. Which of the following is used to open file in append+read mode?
    a) "a+"
    b) "w+"
    c) "r+"
    d) "x+"
    View Answer

    Correct answer: A — "a+"

  50. What will be printed?
    1 f = open("eduinq.txt","a+")
    2 f.write("Eduinq")
    3 f.seek(0)
    4 print(f.read())
    a) Eduinq
    b) Error
    c) None
    d) Blank
    View Answer

    Correct answer: A — Eduinq

Quick Links to Explore