File Handling Quiz 1 Introduction to files, types of files relative and absolute paths

πŸ“ File Handling Quiz 1 β€” Introduction to Files (Class 12 CS, Code 083)

Practice 20 CBSE Class 12 Computer Science (Code 083) MCQs on introduction to file handling β€” text vs binary vs CSV files, opening files in different modes (r, w, a, r+, rb, wb), relative vs absolute paths, the with statement, the os module, read()/readline()/readlines(), write()/writelines(), and the csv module.

This set includes 2 board-style predict-the-output questions with full working code. Every result below has been verified by actually running the code in Python. Tap any question to reveal the answer with a clear explanation.

πŸ“ File BasicsπŸ“„ File TypesπŸ”§ File ModesπŸ—ΊοΈ File Pathsβœ… Best PracticesπŸ–₯️ os ModuleπŸ“Š CSV Files⭐ Board-Style Programs
Q1
πŸ“ File Basics

What is the primary purpose of using files in programming?

  1. To slow down program execution
  2. To store data permanently
  3. To create intentional errors
  4. To display information on the console
Show Answer & Explanation

βœ… Answer: (b) To store data permanently

Files let a program store data permanently β€” unlike variables, which disappear once the program ends, data written to a file survives and can be read again later.

Q2
πŸ“„ File Types

Which type of file contains human-readable text and can be opened with a text editor?

  1. Binary file
  2. CSV file
  3. Text file
  4. Compressed file
Show Answer & Explanation

βœ… Answer: (c) Text file

A text file stores data as plain, human-readable characters β€” you can open it in Notepad or any text editor and read it directly.

Q3
πŸ“„ File Types

What is the main characteristic of a binary file?

  1. Human-readable text
  2. Compressed format
  3. Machine-readable format
  4. Tabular data
Show Answer & Explanation

βœ… Answer: (c) Machine-readable format

A binary file stores data in a machine-readable format (raw bytes) rather than plain text β€” images, audio, and executables are all binary files, and they look like gibberish if opened in a text editor.

Q4
πŸ“‚ Opening Files

In Python, how do you open a file for reading?

  1. file = open(“filename.txt”, “w”)
  2. file = open(“filename.txt”, “r”)
  3. file = open(“filename.txt”, “a”)
  4. file = open(“filename.txt”, “rb”)
Show Answer & Explanation

βœ… Answer: (b) file = open(“filename.txt”, “r”)

Mode “r” opens a file for reading in text mode β€” the default and most common mode for reading. ("w" is for writing/overwriting, "a" for appending, and "rb" for reading in binary mode.)

Q5
πŸ”§ File Modes

What is the purpose of the “wb” mode when opening a file in Python?

  1. Write and append mode
  2. Binary write mode
  3. Binary read mode
  4. Write mode (text)
Show Answer & Explanation

βœ… Answer: (b) Binary write mode

“wb” opens a file for writing in binary mode β€” used for non-text data like images or serialized objects, as opposed to "w" which writes plain text.

Q6
πŸ“„ File Types

Which type of file is commonly used for storing tabular data, such as spreadsheets?

  1. Text file
  2. Binary file
  3. CSV file
  4. XML file
Show Answer & Explanation

βœ… Answer: (c) CSV file

A CSV (Comma-Separated Values) file is the standard, simple format for storing tabular/row-column data, and can be opened directly in spreadsheet software like Excel.

Q7
πŸ“„ File Types

What does CSV stand for in the context of file formats?

  1. Comma-Separated Values
  2. Compressed Storage Variant
  3. Common System Variables
  4. Centralized Storage Vector
Show Answer & Explanation

βœ… Answer: (a) Comma-Separated Values

CSV stands for Comma-Separated Values β€” each line is a row of data, with individual values separated by commas.

Q8
πŸ—ΊοΈ File Paths

What is the purpose of an absolute file path?

  1. To specify the file’s location relative to the current working directory
  2. To specify the file’s location irrespective of the current working directory
  3. To create intentional errors
  4. To slow down program execution
Show Answer & Explanation

βœ… Answer: (b) To specify the file’s location irrespective of the current working directory

An absolute path gives the complete location of a file starting from the root/drive (e.g. C:/Users/Riya/file.txt), so it works no matter what the program’s current working directory is.

Q9
πŸ—ΊοΈ File Paths

Which of the following is an example of a relative file path?

  1. “C:/Documents/file.txt”
  2. “/home/user/file.txt”
  3. “data/file.txt”
  4. Both “data/file.txt” and just “file.txt” are relative paths
Show Answer & Explanation

βœ… Answer: (d) Both “data/file.txt” and just “file.txt” are relative paths

A relative path is defined with respect to the current working directory rather than starting from a root or drive. Both "data/file.txt" (relative to a subfolder) and a bare "file.txt" (relative to the current folder) qualify as relative paths β€” verified with os.path.isabs(), which returns False for both. (This question is corrected from the original, whose intended single answer overlooked that a bare filename is also technically a relative path.)

Q10
πŸ”’ Closing Files

In Python, how can you close an open file?

  1. close(file)
  2. file.close()
  3. close(“file”)
  4. file.close(“file”)
Show Answer & Explanation

βœ… Answer: (b) file.close()

file.close() is the correct syntax β€” close() is a method called on the file object, not a standalone function.

Q11
πŸ”§ File Modes

What does opening a file in ‘a’ (append) mode do?

  1. Overwrites the entire file with new content
  2. Opens the file only for reading, never writing
  3. Adds new content to the end of the file, keeping existing content intact
  4. Deletes the file immediately
Show Answer & Explanation

βœ… Answer: (c) Adds new content to the end of the file, keeping existing content intact

‘a’ mode opens the file for writing but preserves everything already in it β€” any new data you write gets added at the end, rather than replacing the existing content (which is what 'w' mode would do).

Q12
πŸ”§ File Modes

What does opening a file in ‘r+’ mode allow you to do?

  1. Only read the file, nothing else
  2. Only write to the file, nothing else
  3. Both read AND write to the file, without erasing its existing content
  4. Only append new content
Show Answer & Explanation

βœ… Answer: (c) Both read AND write to the file, without erasing its existing content

‘r+’ opens an existing file for both reading and writing β€” unlike 'w', it does not erase the file’s existing content when opened.

Q13
βœ… Best Practices

What is the advantage of using the ‘with’ statement when working with files in Python?

  1. It makes the file permanently read-only
  2. It automatically closes the file once the block finishes, even if an error occurs
  3. It converts text files into binary files automatically
  4. It has no real advantage over open()/close()
Show Answer & Explanation

βœ… Answer: (b) It automatically closes the file once the block finishes, even if an error occurs

with open(...) as f: is a context manager that automatically closes the file once the indented block ends β€” even if an exception is raised partway through β€” so you never risk forgetting to call close().

Q14
πŸ–₯️ os Module

Which function from the os module returns the current working directory?

  1. os.getcwd()
  2. os.currentdir()
  3. os.pwd()
  4. os.directory()
Show Answer & Explanation

βœ… Answer: (a) os.getcwd()

os.getcwd() (β€œget current working directory”) returns the folder the Python script is currently running from β€” the reference point used to resolve relative paths.

Q15
πŸ–₯️ os Module

Which function is used to check whether a given file or path actually exists?

  1. os.path.check()
  2. os.path.exists()
  3. os.file.found()
  4. os.verify()
Show Answer & Explanation

βœ… Answer: (b) os.path.exists()

os.path.exists(path) returns True or False depending on whether the given file or directory is actually present on disk β€” useful to check before trying to open a file.

Q16
πŸ“– Reading Files

What is the difference between read(), readline(), and readlines()?

  1. They all do exactly the same thing
  2. read() gets the whole file as one string; readline() gets just one line; readlines() gets every line as a list
  3. read() only works on binary files; the other two only work on text files
  4. readlines() can only be used once per program
Show Answer & Explanation

βœ… Answer: (b) read() gets the whole file as one string; readline() gets just one line; readlines() gets every line as a list

read() returns the entire file content as a single string. readline() returns just the next single line each time it’s called. readlines() returns every line as a list of strings, e.g. ['line1\n', 'line2\n'].

Q17
✍️ Writing Files

What is the difference between write() and writelines()?

  1. write() writes a single string; writelines() writes each string from an iterable (like a list) one after another
  2. They are identical in every way
  3. writelines() automatically adds line breaks between items, write() never can
  4. write() only works in binary mode
Show Answer & Explanation

βœ… Answer: (a) write() writes a single string; writelines() writes each string from an iterable (like a list) one after another

write() writes exactly the string you give it. writelines() takes an iterable (like a list of strings) and writes each item in sequence β€” but note it does not automatically insert newlines between them; you must include \n yourself if needed.

Q18
πŸ“Š CSV Files

Which module do you need to import to read and write CSV files conveniently in Python?

  1. file
  2. csv
  3. table
  4. spreadsheet
Show Answer & Explanation

βœ… Answer: (b) csv

The built-in csv module (import csv) provides csv.reader() and csv.writer() to handle comma-separated data properly, including edge cases like commas inside quoted fields.

Q19
⭐ Board-Style Programs

What does the following program print? (Assume the file doesn’t exist yet before this code runs.)

with open("notes.txt", "w") as f:
    f.write("Hello\n")
with open("notes.txt", "a") as f:
    f.write("World\n")
with open("notes.txt", "r") as f:
    print(f.read())
  1. Hello
  2. World
  3. Hello\nWorld\n printed as two lines: Hello then World
  4. Error, since the file didn’t exist initially
Show Answer & Explanation

βœ… Answer: (c) Hello\nWorld\n printed as two lines: Hello then World

The first block creates and writes β€œHello”. The second block, opened in append (‘a’) mode, adds β€œWorld” without erasing the first line. Reading the file back shows both lines: Hello then World.

Q20
⭐ Board-Style Programs

What does the following program print?

with open("data.txt", "w") as f:
    f.writelines(["line1\n", "line2\n", "line3\n"])
with open("data.txt", "r") as f:
    print(f.readline())
  1. The entire file content, all three lines
  2. Just “line1” (with its newline)
  3. Just “line3”
  4. An empty string, since writelines() doesn’t actually write anything
Show Answer & Explanation

βœ… Answer: (b) Just “line1” (with its newline)

writelines() writes all three lines to the file. But readline() reads only the first line each time it’s called β€” so this prints just β€œline1” (including its trailing newline).

Jitendra Singh
βœ” Verified Educator

Jitendra Singh

Founder of CBSEPython.in

I help CBSE Class 9–12 students learn Python, Information Technology, Artificial Intelligence and Computer Science through easy notes, quizzes, MCQs and sample papers.

Read More About Me β†’