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
-
C —
AssignFileonly associates the variable with a filename. It does not open, create, or check the file. -
D —
Rewritecreates a new empty file, destroying any existing file with that name. -
C —
Appendopens an existing text file and positions the write cursor at the end. Note:Seek(F, FileSize(F))works for typed files, not text files. -
C — The syntax
file of Tdeclares a typed file. The type must be a fixed-size type. -
B — A dynamic
stringis internally a pointer. Writing a pointer to disk stores a memory address, which is meaningless when the program restarts. Fixed-lengthstring[N]stores actual characters. -
B —
FileSizereturns the number of records. With 5 records written (indices 0-4), it returns 5. -
B —
Seekuses 0-based indexing. The 4th record is at index 3. -
C —
Readadvances the file pointer by one record. After reading at position 5, the pointer is at position 6. -
B — Open with
Reset, seek to the end withSeek(F, FileSize(F)), then write.Appendworks only for text files.Rewritewould destroy existing data. -
A — An untyped file is declared as
filewithout specifying a component type. -
C — Untyped files use
BlockReadandBlockWritefor I/O operations. -
B —
{$I-}turns off automatic I/O checking, requiring manualIOResultcalls. -
C —
WriteLn(even to the screen) is an I/O operation that resetsIOResult. You must callIOResultimmediately after the operation you want to check. -
B — IOResult code 2 corresponds to "File not found."
-
C —
FileExists(FileName)from theSysUtilsunit checks for file existence. -
B —
Truncateremoves all records from the current file position to the end of the file. -
C — Text files produce human-readable output that can be opened in any text editor.
-
B — Open the file before the
tryblock so thatCloseFileinfinallyonly runs if the file was successfully opened. Option A putsAssignFile/Resetinsidetry, which means ifResetfails,CloseFilewould be called on an unopened file. -
B — The second parameter to
Reset(andRewrite) for untyped files specifies the record size in bytes. -
B — Typed files with
Seekprovide direct random access to any record by index, making them ideal for record-number lookups.