🎓 BookMCQ
← Back to 11. File System Interface

📝 File operations concepts in Operating System (45 MCQs)

System-Level Operations for Managing Files

File operations are services provided by the operating system that allow applications to create, access, modify, organise, and remove persistent information. The file-system interface exposes operations through system calls or library functions. Common operations include creating, opening, reading, writing, repositioning, closing, deleting, and truncating files. The kernel coordinates these requests with directory structures, open-file tables, storage allocation, and device drivers.

Why Applications Require Controlled File Access

Applications should not directly manipulate physical disk locations because storage devices and file systems use different internal organisations. Standard operations provide a consistent interface while allowing the kernel to enforce permissions, maintain metadata, and coordinate concurrent access. This abstraction also hides device-specific details, making applications less dependent on particular storage hardware or file-system implementations.

Open-File Descriptors and Operation Processing

An application first requests creation or opening of a file. The operating system checks permissions, locates the file metadata, and establishes an open-file description. Subsequent read and write requests use a file descriptor or handle. The kernel maintains the current file position, transfers data, updates relevant metadata, and releases resources when the application closes the file.

Performing a Write and Subsequent Read

Suppose an application creates a file and writes 100 bytes beginning at its current offset of zero. The file position advances to byte 100 after a successful complete write. A subsequent repositioning operation sets the offset to zero, allowing the application to read the same 100 bytes. Closing the file releases its associated open resources.

File-System Reliability and Operation Overhead

Standard file operations simplify application development and enable controlled access to persistent data. Their performance depends on storage latency, caching, buffering, metadata operations, and synchronization. Concurrent access can introduce consistency problems when applications modify shared files. The operating system must also handle failures, insufficient storage, invalid descriptors, and permission violations without exposing internal storage structures.

Common Misconceptions About File Operations

'Opening a file always loads its entire contents into RAM' is incorrect because reading normally retrieves requested data. 'Closing a file always deletes it' is incorrect because closing releases an open reference. 'A successful write always guarantees immediate physical persistence' is incorrect because data may remain in caches until synchronization or write-back completes.

Exam Focus: File System Calls and Their Effects

Compare create, open, read, write, seek, close, unlink, and truncate operations. Trace how file positions change after reading and writing. Identify the roles of file descriptors, open-file tables, permissions, and metadata. Questions may also examine error conditions, concurrent access, and the difference between buffered writes and explicitly synchronized storage.

Key Terms of File Operations

  • Create - Establishes a new file.
  • Open - Obtains a file reference.
  • Read - Retrieves stored bytes.
  • Write - Modifies file contents.
  • Seek - Changes file position.
  • Close - Releases an open reference.
9
Easy
36
Medium
0
Hard

📝 All File operations concepts in Operating System MCQs

Q1. What is the primary purpose of a file in an operating system?

A.To store temporary data only
B.To provide a logical view of storage media ✅
C.To execute programs directly
D.To manage network connections
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: A file gives users and programs a logical, named container for data, abstracting away the physical storage details. This allows programs to read and write data without needing to know where on the disk the bits reside. Options A, C, and D describe unrelated functions such as temporary storage, direct execution, or networking, which are not the core purpose of a file.

Q2. Which attribute uniquely identifies a file to a user?

A.File size
B.File name ✅
C.Creation timestamp
D.Access permissions
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: The file name is the human‑readable identifier that users use to locate and refer to a file. Size, timestamps, and permissions describe properties of the file but do not serve as the primary identifier. Therefore, option B correctly reflects the attribute that uniquely identifies a file from the user's perspective.

Q3. In most operating systems, what ensures that file data persists after a reboot?

A.Volatile RAM
B.Non‑volatile secondary storage ✅
C.CPU cache
D.Temporary buffers
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: Non‑volatile secondary storage such as magnetic disks or SSDs retains data without power, allowing files to survive reboots. Volatile RAM and CPU caches lose their contents when power is removed, and temporary buffers are usually cleared, making those options incorrect.

Q4. Which of the following best describes a text file?

A.A sequence of binary instructions
B.A collection of executable code sections
C.A series of characters organized into lines ✅
D.A set of encrypted records
💡 Difficulty: easy | ✅ Correct: C

📖 Explanation: A text file consists of readable characters arranged in lines, making option C the correct description. Binary instructions, executable code, and encrypted records refer to other file types, not to the typical structure of a text file.

Q5. A programmer needs to store configuration settings that can be edited by end users. Which file type is most appropriate?

A.Executable file
B.Source file
C.Text file ✅
D.Binary image
💡 Difficulty: easy | ✅ Correct: C

📖 Explanation: Configuration files are usually plain text so users can modify them with a simple editor. Executable files run code, source files contain program code, and binary images store non‑readable data, making those choices unsuitable for editable settings.

Q6. What does the term “non‑volatile” mean in the context of storage devices?

A.Data is encrypted
B.Data cannot be overwritten
C.Data remains after power loss ✅
D.Data is compressed
💡 Difficulty: easy | ✅ Correct: C

📖 Explanation: Non‑volatile storage retains its contents without power, which is essential for persisting files. Encryption, overwriting restrictions, and compression are unrelated characteristics, so option C accurately captures the definition.

Q7. If a file is opened in write‑only mode and the program writes 200 bytes, what happens to the previous contents?

A.They are appended after the new data
B.They are overwritten from the start ✅
C.They remain unchanged
D.They are duplicated
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: Write‑only mode typically starts at the file’s beginning unless the file pointer is moved, so writing 200 bytes replaces the existing first 200 bytes, making option B correct. Appending, leaving unchanged, or duplicating do not describe standard write‑only behavior.

Q8. Which operation would you use to list all files in a directory?

A.open()
B.read()
C.ls (or dir) command ✅
D.close()
💡 Difficulty: easy | ✅ Correct: C

📖 Explanation: The ls (or dir) command displays directory contents, showing file names. open(), read(), and close() are system calls for handling individual files, not for listing directory entries.

Q9. A file system uses a hierarchical structure. What advantage does this provide?

A.Faster CPU processing
B.Simplified user navigation and organization ✅
C.Increased storage capacity
D.Reduced power consumption
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: A hierarchy lets users locate files through paths like /home/user/doc, making navigation intuitive. CPU speed, storage size, and power usage are not directly affected by hierarchy, so option B is the correct advantage.

Q10. When a program reads a file sequentially from start to end, which access pattern is it using?

A.Random access
B.Sequential access ✅
C.Indexed access
D.Parallel access
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: Sequential access means the program processes data in order, moving the file pointer forward after each read. Random or indexed access would involve jumping to arbitrary positions, and parallel access would involve multiple simultaneous reads, which are not described here.

Q11. A developer wants to ensure that a log file never exceeds 5 MB. Which technique should be employed?

A.Use a fixed‑size array
B.Implement file truncation after each write
C.Rotate the log file when size limit is reached ✅
D.Compress the file after each write
💡 Difficulty: medium | ✅ Correct: C

📖 Explanation: Log rotation creates a new file once the size limit is hit, keeping each file within the desired bound. Fixed arrays, immediate truncation, or constant compression either do not manage size limits effectively or add unnecessary overhead.

Q12. Which file attribute determines who can read a file?

A.File owner
B.File size
C.Access permissions ✅
D.Creation date
💡 Difficulty: medium | ✅ Correct: C

📖 Explanation: Access permissions specify read, write, and execute rights for owners, groups, and others. The owner identifies who created the file, size indicates storage usage, and creation date is unrelated to permission control.

Q13. If a program attempts to open a file that does not exist with mode \r\, what is the expected result?

A.File is created automatically
B.Program receives an error ✅
C.File is opened in read‑write mode
D.File is opened in append mode
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: Opening in read‑only mode requires the file to exist; otherwise the system returns an error. Automatic creation occurs with write or append modes, not with read‑only.

Q14. A system administrator wants to prevent any user from deleting a critical configuration file while still allowing reads. Which permission setting is appropriate?

A.Read‑only for everyone ✅
B.Read‑write for owner only
C.Execute for owner only
D.No permissions for anyone
💡 Difficulty: medium | ✅ Correct: A

📖 Explanation: Setting the file to read‑only for all users blocks deletion (which requires write permission) while still permitting reads. Granting write to the owner would allow deletion, and execute or no permissions would not satisfy the read requirement.

Q15. When a file is opened with the flag \a+\, what behavior does this enable?

A.Overwrite existing content from start
B.Append new data and allow reading ✅
C.Read only from the beginning
D.Write only at random positions
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: The \a+\ flag opens a file for both appending and reading; writes always occur at the end, while reads can occur anywhere. Overwrite from start, read‑only, or random writes are not characteristic of this mode.

Q16. Why might a programmer choose to store data in binary format rather than plain text?

A.Binary files are easier to edit manually
B.Binary files consume less disk space and load faster ✅
C.Binary files are more portable across platforms
D.Binary files are human readable
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: Binary representation eliminates textual overhead, reducing file size and improving I/O performance. Manual editing is harder, portability can be an issue due to endianness, and human readability is limited, making option B the correct rationale.

Q17. A file system supports both absolute and relative paths. Which scenario best illustrates the use of a relative path?

A./usr/local/bin/program
B.C:\\Windows\\system32\\cmd.exe
C.../logs/error.log ✅
D.http://example.com/file.txt
💡 Difficulty: medium | ✅ Correct: C

📖 Explanation: The path \../logs/error.log\ depends on the current working directory, making it relative. Absolute paths start from the root (e.g., /usr/local/bin/program) or specify a full URL, which are not relative.

Q18. If two processes open the same file simultaneously, one for reading and one for writing, what must the OS ensure?

A.Both processes get identical file descriptors
B.Data written by one is invisible to the other
C.Write operations are serialized to prevent corruption ✅
D.Reading process must wait until writing finishes
💡 Difficulty: medium | ✅ Correct: C

📖 Explanation: The OS must serialize writes or coordinate access so that concurrent modifications do not corrupt the file. Identical descriptors, invisibility, or forced waiting are not typical requirements for safe concurrent access.

Q19. Which of the following best describes a record-oriented file?

A.A file where data is stored as a continuous stream of bytes
B.A file divided into fixed‑size blocks
C.A file composed of variable‑length lines of text
D.A file consisting of structured entries like rows in a table ✅
💡 Difficulty: medium | ✅ Correct: D

📖 Explanation: Record‑oriented files store data in discrete, often fixed‑size structures (e.g., database rows), making option D accurate. Continuous streams, block division, or plain text lines describe other file organizations.

Q20. A developer needs to read every third line of a large log file without loading the entire file into memory. Which technique is most efficient?

A.Read the whole file into a string then split
B.Use a memory‑mapped file and skip lines
C.Read line by line and process only needed lines ✅
D.Load the file into a database first
💡 Difficulty: medium | ✅ Correct: C

📖 Explanation: Reading line by line allows the program to process each line sequentially, discarding unneeded ones, which conserves memory. Memory‑mapped files still load large portions, and loading entire content or a database adds unnecessary overhead.

Q21. What is the effect of setting the sticky bit on a directory?

A.Prevents any file creation in the directory
B.Allows only the owner to delete files within ✅
C.Ensures files cannot be renamed
D.Blocks execution of files inside the directory
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: The sticky bit (e.g., /tmp) permits only the file’s owner, the directory owner, or root to delete or rename files, enhancing shared‑directory security. It does not block creation, renaming, or execution.

Q22. When a file is opened with the mode \w+\, what happens to its existing content?

A.It is preserved and read from the start
B.It is truncated to zero length ✅
C.It is appended to the end
D.It is duplicated
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: The \w+\ mode opens a file for reading and writing but first truncates it, erasing any previous data. Preservation, appending, or duplication are not characteristics of this mode.

Q23. A program writes data to a file but does not call close() before terminating. What is the likely outcome?

A.All data is guaranteed to be saved
B.Data may be lost because buffers are not flushed ✅
C.The file becomes read‑only
D.The operating system crashes
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: If the program exits without closing, the OS typically flushes buffers, but there is a risk that some unwritten data is lost. Therefore, option B reflects the most common outcome, while the other choices are inaccurate.

Q24. Why is it important for a file system to maintain metadata such as timestamps and permissions?

A.Metadata speeds up data transfer
B.Metadata provides context for security and auditing ✅
C.Metadata reduces storage space usage
D.Metadata enables compression algorithms
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: Timestamps and permissions give administrators information about when a file was accessed and who can manipulate it, supporting security policies and audit trails. Speed, space savings, and compression are unrelated to metadata’s primary role.

Q25. A user needs to copy a large directory tree from one drive to another while preserving symbolic links. Which command option should be used?

A./s (copy subdirectories)
B./p (preserve attributes)
C./l (create links) ✅
D./k (keep read‑only)
💡 Difficulty: medium | ✅ Correct: C

📖 Explanation: The option that creates links rather than copying the linked files preserves symbolic links. Copying subdirectories, preserving attributes, or keeping read‑only flags do not maintain the link structure.

Q26. In a file system that uses a bitmap to track free blocks, what does a '0' bit typically represent?

A.A block is allocated
B.A block is free ✅
C.A block is corrupted
D.A block is reserved for the OS
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: In bitmap allocation, a cleared bit (0) marks a free block that can be assigned, while a set bit (1) indicates an allocated block. Corruption or reservation are not standard interpretations of a zero bit.

Q27. Which scenario illustrates the need for file buffering?

A.Reading a single byte from a file
B.Writing a single character to a file
C.Copying a large file in 4 KB chunks ✅
D.Opening a file in exclusive mode
💡 Difficulty: medium | ✅ Correct: C

📖 Explanation: Copying a large file benefits from buffering because reading and writing in moderate-sized chunks reduces system call overhead. Single‑byte operations are inefficient without buffering, but the scenario emphasizes the performance gain of buffering.

Q28. A program must ensure that a file is not partially written if a crash occurs mid‑write. Which strategy is most reliable?

A.Write directly to the target file
B.Write to a temporary file then rename ✅
C.Append data continuously
D.Use unbuffered I/O
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: Writing to a temporary file and atomically renaming it ensures that either the old file remains unchanged or the new file appears fully formed, protecting against partial writes. Direct writes, continuous appends, or unbuffered I/O do not provide the same atomic guarantee.

Q29. What is the purpose of a file descriptor in Unix‑like systems?

A.It stores the file’s content
B.It identifies an open file for the kernel ✅
C.It defines the file’s size
D.It encrypts the file data
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: A file descriptor is an integer handle that the kernel uses to track an open file, enabling subsequent read/write operations. It does not contain the data, size, or encryption information.

Q30. Which of the following best explains why files are considered the smallest unit of logical secondary storage?

A.Files cannot be divided into smaller parts
B.Files are always stored in a single block
C.Data must be organized within a file to be written to storage ✅
D.Files are the smallest physical sector on a disk
💡 Difficulty: medium | ✅ Correct: C

📖 Explanation: To write data to secondary storage, the operating system organizes it into files; thus, a file is the minimal logical container for persistent data. Files can span multiple blocks, and physical sectors are smaller than files.

Q31. A developer needs to store a sequence of fixed‑length records that will be accessed randomly. Which file organization is most suitable?

A.Sequential file
B.Record‑oriented file with fixed‑size blocks ✅
C.Variable‑length text file
D.Compressed archive
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: Fixed‑length records benefit from a record‑oriented layout with uniform block sizes, enabling direct calculation of offsets for random access. Sequential files require linear scanning, and variable‑length or compressed formats hinder predictable positioning.

Q32. Why might an operating system abstract physical storage devices into a logical file system?

A.To increase hardware speed
B.To hide hardware differences from applications ✅
C.To reduce power consumption
D.To eliminate the need for drivers
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: Abstraction provides a uniform interface so programs can work with files without needing to know the specific characteristics of disks, tapes, or optical media. Speed, power, and driver elimination are not primary reasons for this abstraction.

Q33. A student writes a program that opens a file, writes data, and then immediately reads from the same file without closing it. What must the program do to ensure the read returns the newly written data?

A.Seek to the beginning of the file
B.Flush the output buffer or use a file sync operation ✅
C.Close and reopen the file
D.Ignore the read operation
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: Flushing or syncing forces buffered data to be written to the storage device, making it visible for subsequent reads. Seeking alone does not guarantee data is persisted, and closing/reopening adds unnecessary overhead.

Q34. Which command would you use to change the owner of a file to user \alice\?

A.chmod alice filename
B.chown alice filename ✅
C.setowner alice filename
D.useradd alice filename
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: The chown command changes file ownership, taking the new owner name followed by the target file. chmod changes permissions, setowner is not a standard command, and useradd creates a new user rather than altering file ownership.

Q35. A system uses journaling to protect file system integrity. What does journaling primarily record?

A.File contents
B.Metadata changes such as allocations and deletions ✅
C.User passwords
D.Network traffic
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: Journaling logs metadata operations (e.g., block allocations, directory updates) before they are applied, allowing recovery after a crash. It does not store full file contents, passwords, or network data.

Q36. When a file is opened with the mode \r+\, which actions are permitted?

A.Read only
B.Write only
C.Read and write ✅
D.Append only
💡 Difficulty: medium | ✅ Correct: C

📖 Explanation: The \r+\ mode allows both reading and writing from the current file position. It does not restrict to read‑only, write‑only, or append‑only operations.

Q37. A programmer wants to ensure that a configuration file cannot be accidentally overwritten. Which file permission setting should be applied?

A.Read‑write for owner, no permissions for others
B.Read‑only for everyone ✅
C.Execute for owner only
D.Write‑only for owner
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: Setting the file to read‑only for all users blocks write attempts while still allowing reading. Granting write permission or execute permission would not prevent overwriting, and write‑only would disallow reading.

Q38. Why is it advantageous for a file system to support both absolute and relative paths?

A.It reduces storage requirements
B.It allows flexible navigation and script portability ✅
C.It speeds up file I/O operations
D.It encrypts file names
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: Absolute paths give a fixed location, while relative paths enable scripts and users to work regardless of the current directory, enhancing flexibility and portability. Storage, speed, and encryption are unrelated benefits.

Q39. A developer needs to store large multimedia data that will be streamed continuously. Which file type is most appropriate?

A.Text file
B.Source code file
C.Binary file ✅
D.Log file
💡 Difficulty: medium | ✅ Correct: C

📖 Explanation: Multimedia data such as audio or video is typically stored in binary format to preserve exact byte sequences needed for streaming. Text, source code, and log files are unsuitable for raw media streams.

Q40. Which of the following best describes the role of a file handle?

A.It contains the file’s data
B.It is a pointer used by the program to reference an open file ✅
C.It encrypts the file contents
D.It determines the file’s size
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: A file handle (or descriptor) is a reference that the program uses to perform operations on the open file. It does not hold the data itself, encrypt it, or define size.

Q41. A system administrator wants to prevent users from creating new files in a directory but still allow them to read existing ones. Which permission should be set?

A.Read‑only for the directory ✅
B.Write‑only for the directory
C.Execute‑only for the directory
D.No permissions at all
💡 Difficulty: medium | ✅ Correct: A

📖 Explanation: Read permission on a directory allows listing and opening files, while write permission is required to create new entries. Setting read‑only therefore blocks creation but permits reading.

Q42. Why might a file system impose a maximum filename length?

A.To limit the amount of data stored
B.To simplify directory indexing and ensure compatibility ✅
C.To reduce the file’s size
D.To increase read speed
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: Limiting filename length helps keep directory structures manageable, improves lookup efficiency, and maintains compatibility across different platforms. It does not directly limit stored data volume, file size, or read speed.

Q43. A program needs to read a large file in reverse order (from end to start). Which method is most suitable?

A.Read the entire file into memory then reverse
B.Use seek() to move the file pointer backward and read chunks ✅
C.Read line by line forward and store in a list
D.Use a streaming API that reads backwards automatically
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: Using seek() to position the file pointer near the end and then reading backwards in chunks avoids loading the whole file, making it memory‑efficient. The other options require full loading or unsupported APIs.

Q44. Which of the following best explains why file systems use block allocation instead of byte‑by‑byte allocation?

A.Blocks are easier for users to understand
B.Blocks reduce fragmentation and improve I/O efficiency ✅
C.Blocks increase the total storage capacity
D.Blocks allow for encryption
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: Allocating whole blocks minimizes fragmentation, aligns I/O operations with hardware sector sizes, and speeds up reads/writes. User comprehension, capacity increase, and encryption are not primary reasons for block allocation.

Q45. A developer writes a program that creates a temporary file, writes data, and then deletes it. Which function sequence is most appropriate in POSIX?

A.open(), write(), close(), unlink() ✅
B.open(), read(), close(), rename()
C.create(), write(), close(), delete()
D.open(), write(), delete(), close()
💡 Difficulty: medium | ✅ Correct: A

📖 Explanation: The typical POSIX sequence is open() to obtain a descriptor, write() to add data, close() to release the descriptor, and unlink() to remove the file name. The other sequences misuse functions or order.

🔗 Related Topics (MCQs)