Practice File Operations in CS Thinking
Use these practice problems to test your method after reviewing the concept explanation and worked examples.
Quick Recap
The operations of reading data from files and writing data to files on a storage device, allowing programs to persist information beyond a single run. The standard pattern is: open the file, read from or write to it, then close the file to ensure data is saved properly.
File operations let your program save and load information โ like writing notes in a notebook and reading them later.
Showing a random 20 of 50 problems.
Example 1
challengeA file contains '10\n20\n30'. The program reads all lines, converts each to int, and sums them. What is the sum, and what subtle bug could a trailing newline cause?
Example 2
mediumName the mode that would CREATE the file if missing AND preserve existing content if present.
Example 3
mediumWhich mode creates the file if it does not exist AND lets you write: 'r' or 'w'?
Example 4
mediumIf you open a file with 'w', write nothing, and close it, what happens to a previously non-empty file?
Example 5
easyA file 'data.txt' contains 'hello'. It is opened with mode 'a' and 'bye' is written. Contents now?
Example 6
mediumTrace: 'log.txt' is empty. Code: open 'a', write 'A'; close. open 'a', write 'B'; close. Final contents?
Example 7
mediumA program opens 'config.json' but never closes it, in a long-running loop. What resource problem can develop?
Example 8
mediumA CSV line reads 'Bob,42,USA'. After splitting on ',', what is the index of '42' and what is its type?
Example 9
easyWhich mode would you use to simply read a config file without changing it: 'r', 'w', or 'a'?
Example 10
mediumWrite pseudocode that copies 'src.txt' to 'dst.txt'. What is the I/O pattern?
Example 11
mediumA program opens the same file 'r' twice without closing between, on a system with a low open-file limit, repeatedly in a loop. What failure mode appears?
Example 12
easyWhich of these is the safest way to make sure a file gets closed even if an error occurs: (A) call close at end, (B) use a with-block, (C) never close it?
Example 13
challengeTrace across runs: program does open 'a' and writes a line 'run' each run. After running the program 4 times, how many lines does the file contain?
Example 14
easyWhich mode ERASES existing file content when opened: 'r', 'w', or 'a'?
Example 15
mediumTrace: 'x.txt' contains 'OLD'. open 'w', write 'NEW'; close. open 'r', read all; close. What was read?
Example 16
challengeTwo programs open 'counter.txt' simultaneously, each reads the value 5, increments, and writes 6. What well-known concurrency bug occurred, and how would you prevent it?
Example 17
easyWhat are the three steps of the standard file-operation pattern (in order)?
Example 18
mediumUsing `with open('f.txt','w') as fh: fh.write('hi')`, is fh closed after the block, and does it create the file if missing?
Example 19
hardTrace: 'students.csv' has lines 'Ana,90' and 'Bo,75'. Code reads each line, splits on ',', tracks the highest score, and outputs the name. What is output?
Example 20
mediumTrace: 'n.txt' contains '5'. Code reads the text, converts to int, adds 3, writes the result back with 'w'. Final contents?