Partial preview of the text
Download PYTHON FILE HANDLING, EXCEPTIONS & OOP EXAM 2025-2026 | COMPLETE SCRIPTING & PROGRAMMING F and more Exams Programming Languages in PDF only on Docsity!
Page 1 of 154 File Handling, Exceptions, and Object- Oriented Programming Full Objective Assessment (2025 Edition) | Comprehensive Real Question Final Exam | Python Scripting & Programming Foundations | Verified Answers with Detailed Rationales Q1. Which built-in Python function is used to open a file? a) open() b) file() c) read() d) access() Page 2 of 154 Answer: a Rationale: open() is the standard Python function to open a file; it returns a file object. file() was removed in Python 3. Q2. What does the mode 'r' do when opening a file? a) Write (overwrite) b) Read (default) — raises error if file does not exist c) Append d) Read and write Answer: b Rationale: 'r' opens the file for reading. It raises FileNotFoundError if the file does not exist. Q3. Which mode opens a file for writing and creates the file if it doesn’t exist, but overwrites existing content? a) 'w Page 4 of 154 Q5. What is the purpose of mode 'x'? a) Read and write b) Exclusive creation — fails if file already exists c) Execute file d) Binary read Answer: b Rationale: 'x' creates a new file; if the file already exists, FileExistsError is raised. It is useful for preventing accidental overwriting. Q6. How do you open a file for both reading and writing without truncating? a) 'rw' b) ‘+! Page 5 of 154 c) wt d)'at' Answer: b Rationale: 'r+' opens the file for reading and writing, and does not truncate it. The file must exist; otherwise, an error is raised. Q7. What does the following code do? python with open('data.txt’, 'w') as f: f.write('Hello') a) Appends “Hello” to the file. b) Writes “Hello” to the file, overwriting previous content. c) Reads the file content. d) Raises an error if the file exists. Page 7 of 154 Q9. Which method reads the entire content of a file as a single string? a) readline() b) readlines() c) read() d) seek() Answer: ¢ Rationale: read() returns the whole file content as a string (or bytes if binary mode). readline() reads one line, readlines() returns a list of lines. Q10. What does f.readlines() return? a) A single string b) A list of strings, each representing a line (including newline characters) Page 8 of 154 c) An integer indicating the number of lines d) A generator yielding lines Answer: b Rationale: readlines() reads all lines and returns them as a list. Each line includes its trailing newline character. Q11. Which method is used to write a string to a file? a) write() b) writeline() c) writelines() d) print() Answer: a Rationale: write(string) writes the string to the file. writelines() writes a list of strings without adding newlines automatically. Page 10 of 154 c) f.write() automatically flushes. d) f.commit() Answer: b Rationale: flush() forces the internal buffer to be written to disk. It does not close the file. Q14. What does the tell() method of a file object return? a) The current file size b) The current position within the file (in bytes from the beginning) c) The number of lines read d) The file name Answer: b Rationale: tell() returns the file pointer’s current byte offset from the start of the file. Page 11 of 154 Q15. What does seek(O) do? a) Moves the file pointer to the end of the file b) Moves the file pointer to the beginning of the file c) Deletes the file content d) Closes the file Answer: b Rationale: seek(offset, whence) changes the file position. seek(O) moves to the start. Q16. Which method moves the file pointer to the end of the file? a) seek(O, 2) b) seek(O, 0) c) seek(0, 1) d) seek(-1, 2) Page 13 of 154 a) 'r+' b) ‘w+! c) ‘at d) Both b and c Answer: d Rationale: 'w+' truncates existing files or creates new ones; 'a+' creates if not exists and writes at the end. 'r+' does not create. Q19. What is the output of the following code? python with open('test.txt', 'w') as f: f.write(‘abc’) with open('test.txt', 'a') as f: f.write('def') Page 14 of 154 with open('test.txt', 'r') as f: print(f.read()) a) abcdef b) abc c) def d) Error Answer: a Rationale: First write writes “abc”, then append writes “def” at the end, resulting in “abcdef”. Q20. How can you read a file line by line without loading the entire file into memory? a) Use f.read().splitlines() b) Use a for line in f: loop c) Use f.readlines() d) Use f.read().split("\n') Page 16 of 154 Q22. What type of data does read() return when a file is opened in binary mode? a) String b) List of strings c) Bytes object d) Integer Answer: ¢ Rationale: In binary mode ('rb'), read() returns a bytes object, not a string. Q23. How do you write a bytes object to a file? a) f.write(bytes_obj) works if opened in binary mode. b) Use f.write_text(bytes_obj). c) Convert to string first. d) Cannot write bytes directly. Page 17 of 154 Answer: a Rationale: In binary mode, write() accepts a bytes-like object. Example: f.write(b'Hello’). Q24. What is the purpose of the os module in relation to file handling? a) It provides functions for operating system-dependent functionality, including file operations (rename, remove, etc.). b) It replaces the built-in open(). c) It is used only for networking. d) It handles exceptions. Answer: a Rationale: os includes functions like os.remove(), os.rename(), os.path.exists(), etc. Page 19 of 154 directory). os.path.isfile() checks specifically for a file. Both are useful. Q27. Which exception is raised when you try to open a file for writing in mode 'x' and the file already exists? a) FileNotFoundError b) FileExistsError c) PermissionError d) lOError Answer: b Rationale: Mode 'x' (exclusive creation) raises FileExistsError if the file already exists. Q28. What does the following code output? python Page 20 of 154 with open('sample.txt', 'w') as f: f.write('Line1 \nLine2\n’) with open('sample.txt’, 'r') as f: print(f.read(5)) a) Line b) Line1\n c) Line1 \nL d) Line] (without newline) Answer: a Rationale: read(5) reads the first 5 characters: ‘L’,‘i’,‘n’,‘e’,1’. The newline character after ‘1’ is not included because only 5 bytes are read. Q29. What is the purpose of f.close()? a) To delete the file