Chapter 13 Quiz: Files and I/O

Instructions: Choose the single best answer for each question. Answers are provided at the end.


Question 1. What does AssignFile(F, 'data.txt') do?

A) Opens the file data.txt for reading B) Creates a new file called data.txt C) Associates the file variable F with the filename data.txt D) Checks whether data.txt exists


Question 2. What happens when you call Rewrite(F) on a file that already exists?

A) The file is opened for reading B) An error occurs because the file already exists C) The file is opened for appending D) The existing file is destroyed and replaced with a new empty file


Question 3. Which procedure opens an existing text file and positions the cursor at the end for adding new data?

A) Reset(F) B) Rewrite(F) C) Append(F) D) Seek(F, FileSize(F))


Question 4. Which of the following is a valid declaration for a typed file of student records?

A) var F: file of string; B) var F: TextFile of TStudent; C) var F: file of TStudent; D) var F: file(TStudent);


Question 5. Why must records stored in typed files use string[N] rather than string?

A) string types cannot hold record field data B) Dynamic strings are just pointers; writing a pointer to a file is meaningless C) string[N] is faster to process than string D) The compiler does not allow string in record definitions


Question 6. After writing 5 records to a typed file (records at indices 0-4), what does FileSize(F) return?

A) 4 B) 5 C) The total byte count of the file D) The size of one record in bytes


Question 7. What is the correct way to jump to the 4th record (0-based index 3) in a typed file?

A) Seek(F, 4) B) Seek(F, 3) C) FilePos(F, 3) D) Reset(F, 3)


Question 8. You read a record at position 5 with Read(F, Rec). What is FilePos(F) now?

A) 4 B) 5 C) 6 D) Undefined


Question 9. How do you append a new record to the end of an existing typed file?

A) Append(F); Write(F, Rec); B) Reset(F); Seek(F, FileSize(F)); Write(F, Rec); C) Rewrite(F); Seek(F, FileSize(F)); Write(F, Rec); D) Reset(F); Write(F, Rec);


Question 10. What is an untyped file in Pascal?

A) A file declared as file without a type specifier B) A file that has no name C) A file that cannot be read D) A text file with no formatting


Question 11. Which procedures are used for reading and writing untyped files?

A) Read and Write B) ReadLn and WriteLn C) BlockRead and BlockWrite D) ByteRead and ByteWrite


Question 12. What does {$I-} do?

A) Enables integer overflow checking B) Disables automatic I/O error checking C) Includes an external file D) Initializes the I/O subsystem


Question 13. You write this code:

{$I-}
Reset(F);
WriteLn('Opened!');
IOCode := IOResult;
{$I+}

What is wrong?

A) IOResult must be called before WriteLn B) You cannot use {$I+}` after `{$I-} C) WriteLn resets the I/O result, so IOCode will always be 0 D) Reset cannot be used with {$I-}


Question 14. The IOResult value 2 means:

A) Access denied B) File not found C) Disk full D) File already open


Question 15. Which SysUtils function checks if a file exists without opening it?

A) FileCheck(FileName) B) Exists(FileName) C) FileExists(FileName) D) CheckFile(FileName)


Question 16. What does the Truncate(F) procedure do when called on a typed file?

A) Deletes the file from disk B) Removes all records from the current position to the end of the file C) Removes the record at the current position only D) Empties the entire file


Question 17. You want to store log messages that will be reviewed by humans in a text editor. Which file type is most appropriate?

A) Typed file (file of TLogEntry) B) Untyped file C) Text file D) Any file type works equally well


Question 18. Which of the following is a valid try...finally pattern for file I/O?

A)

try
  AssignFile(F, 'data.txt');
  Reset(F);
  ReadLn(F, Line);
finally
  CloseFile(F);
end;

B)

AssignFile(F, 'data.txt');
Reset(F);
try
  ReadLn(F, Line);
finally
  CloseFile(F);
end;

C)

try
  AssignFile(F, 'data.txt');
finally
  Reset(F);
  ReadLn(F, Line);
  CloseFile(F);
end;

D)

AssignFile(F, 'data.txt');
try
  Reset(F);
  ReadLn(F, Line);
  CloseFile(F);
finally
end;

Question 19. What is the second parameter in Reset(F, 1) when used with an untyped file?

A) The number of records to read B) The record size in bytes C) The file access mode (1 = read) D) The buffer size


Question 20. You need to store a collection of fixed-size employee records and frequently look up individual employees by their record number. Which file type is best?

A) Text file — the data should be human-readable B) Typed file — supports random access with Seek C) Untyped file — offers the best performance D) Text file with special delimiters


Answer Key

  1. CAssignFile only associates the variable with a filename. It does not open, create, or check the file.

  2. DRewrite creates a new empty file, destroying any existing file with that name.

  3. CAppend opens an existing text file and positions the write cursor at the end. Note: Seek(F, FileSize(F)) works for typed files, not text files.

  4. C — The syntax file of T declares a typed file. The type must be a fixed-size type.

  5. B — A dynamic string is internally a pointer. Writing a pointer to disk stores a memory address, which is meaningless when the program restarts. Fixed-length string[N] stores actual characters.

  6. BFileSize returns the number of records. With 5 records written (indices 0-4), it returns 5.

  7. BSeek uses 0-based indexing. The 4th record is at index 3.

  8. CRead advances the file pointer by one record. After reading at position 5, the pointer is at position 6.

  9. B — Open with Reset, seek to the end with Seek(F, FileSize(F)), then write. Append works only for text files. Rewrite would destroy existing data.

  10. A — An untyped file is declared as file without specifying a component type.

  11. C — Untyped files use BlockRead and BlockWrite for I/O operations.

  12. B{$I-} turns off automatic I/O checking, requiring manual IOResult calls.

  13. CWriteLn (even to the screen) is an I/O operation that resets IOResult. You must call IOResult immediately after the operation you want to check.

  14. B — IOResult code 2 corresponds to "File not found."

  15. CFileExists(FileName) from the SysUtils unit checks for file existence.

  16. BTruncate removes all records from the current file position to the end of the file.

  17. C — Text files produce human-readable output that can be opened in any text editor.

  18. B — Open the file before the try block so that CloseFile in finally only runs if the file was successfully opened. Option A puts AssignFile/Reset inside try, which means if Reset fails, CloseFile would be called on an unopened file.

  19. B — The second parameter to Reset (and Rewrite) for untyped files specifies the record size in bytes.

  20. B — Typed files with Seek provide direct random access to any record by index, making them ideal for record-number lookups.