Unit 6: File Handling - Practice Quiz

CAP1008 — C Programming 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 Which library function is used to open a file in C?

opening and closing files Easy
A. open()
B. openfile()
C. fopen()
D. fileopen()

2 What does fopen() return if it fails to open a file?

opening and closing files Easy
A. EOF
B. 0
C. -1
D. NULL

3 Which mode string opens a file only for reading?

opening and closing files Easy
A. "rw"
B. "r"
C. "w"
D. "a"

4 Which function is used to close a file in C?

opening and closing files Easy
A. fclose()
B. fileclose()
C. endfile()
D. close()

5 What is the correct data type of the pointer used to handle files in C?

opening and closing files Easy
A. char *
B. FILE *
C. int *
D. void *

6 Which mode opens a file for writing and creates it if it does not exist, erasing any existing content when it does exist?

opening and closing files Easy
A. "r"
B. "w"
C. "a"
D. "x"

7 Which mode is used to add data at the end of an existing file without deleting its contents?

opening and closing files Easy
A. "a"
B. "t"
C. "r"
D. "w"

8 Which character is added to a mode string to open a file in binary mode?

text and binary files Easy
A. x
B. b
C. n
D. t

9 In which type of file is data stored as human-readable characters?

text and binary files Easy
A. Binary file
B. Text file
C. Object file
D. Executable file

10 Which type of file stores data in the same format as it is held in memory?

text and binary files Easy
A. Binary file
B. Source file
C. Header file
D. Text file

11 Which function writes a single character to a file?

reading and writing in files Easy
A. fputc()
B. fgetc()
C. fscanf()
D. putchar()

12 Which function reads a single character from a file?

reading and writing in files Easy
A. getchar()
B. fgetc()
C. fputc()
D. fprintf()

13 Which function is used to write formatted output to a file?

reading and writing in files Easy
A. fprintf()
B. fscanf()
C. fwrite()
D. printf()

14 Which function reads formatted input from a file?

reading and writing in files Easy
A. scanf()
B. fscanf()
C. fprintf()
D. fgets()

15 Which function is used to write a whole string to a file?

reading and writing in files Easy
A. puts()
B. fputc()
C. fgets()
D. fputs()

16 Which function reads a line or string from a file?

reading and writing in files Easy
A. gets()
B. fgets()
C. fgetc()
D. fputs()

17 Which pair of functions is typically used for reading and writing blocks of data in binary files?

reading and writing in files Easy
A. fgets() and fputs()
B. fscanf() and fprintf()
C. fread() and fwrite()
D. fgetc() and fputc()

18 What does EOF stand for in C file handling?

reading and writing in files Easy
A. End Of Format
B. Extra Output Format
C. Error On File
D. End Of File

19 Based on how their data is stored, files in C are broadly categorized into which two types?

categories of files Easy
A. Source files and object files
B. Static files and dynamic files
C. Text files and binary files
D. Input files and output files

20 Which of the following is a valid reason for using files in a C program?

categories of files Easy
A. To make the program compile faster
B. To increase the CPU clock speed
C. To reduce the number of variables in the program
D. To store data permanently so it is available even after the program ends and can be reloaded on the next run

21 What does the fopen() function return if it fails to open the specified file?

opening and closing files Medium
A. 0
B. -1
C. EOF
D. NULL

22 A program opens a file using fopen("data.txt", "w") when data.txt already exists with content. What happens to the existing content?

opening and closing files Medium
A. The file remains unchanged and fopen returns NULL
B. New content is appended after the existing content
C. The existing content is erased and the file is truncated to zero length
D. Writing begins at the current file position without altering data

23 Why is calling fclose() on a file opened for writing important?

opening and closing files Medium
A. It converts the file from binary to text format
B. It resets the file position indicator to the beginning
C. It automatically deletes the file after use
D. It flushes buffered data to disk and releases the file resource

24 Which mode string opens a file for both reading and writing, creating it if it does not exist and truncating it if it does?

opening and closing files Medium
A. "w+"
B. "rb"
C. "r+"
D. "a+"

25 On a Windows system, what is the main effect of opening a file in text mode versus binary mode?

text and binary files Medium
A. Binary mode is slower because it validates each byte
B. Text mode compresses data while binary mode stores it raw
C. Text mode translates newline characters (\n ↔ \r\n) while binary mode does not
D. Binary mode restricts the file to ASCII characters only

26 Why is fwrite() generally preferred over fprintf() for storing a large array of float values?

text and binary files Medium
A. It compresses the floats using a lossless algorithm
B. It automatically sorts the values before writing
C. It guarantees the file is human-readable in any editor
D. It stores the raw byte representation, saving space and avoiding text conversion

27 A structure written to a binary file with fwrite on one machine may not read correctly on another. What is the most common reason?

text and binary files Medium
A. Differences in byte order (endianness) and structure padding between machines
B. Binary mode encrypts the data automatically
C. Binary files cannot be transferred across networks
D. The sizeof operator returns different values each run

28 Which of the following streams is automatically opened when a C program starts and is associated with error output?

categories of files Medium
A. stderr
B. stdlog
C. stddbg
D. stdaux

29 What distinguishes a sequential-access file from a random-access file in C?

categories of files Medium
A. Sequential access reads data in order, while random access can jump to any position using fseek
B. Random files cannot be closed with fclose
C. Sequential files do not require a FILE pointer
D. Sequential files can only store text, while random files store binary

30 Consider the code below. What is written to out.txt?

C
FILE *fp = fopen("out.txt", "w");
fprintf(fp, "%d-%d", 5, 3);
fclose(fp);

reading and writing in files Medium
A. 5-3
B. %d-%d
C. 53
D. 5 - 3

31 Which function is most appropriate for reading a full line of text (including spaces) from a file into a buffer?

reading and writing in files Medium
A. getchar()
B. fread() of a single char
C. fgets()
D. fscanf() with %s

32 What value does feof(fp) return immediately after a successful read that has NOT yet reached the end of the file?

reading and writing in files Medium
A. EOF
B. A non-zero value
C. NULL
D. 0

33 Given a binary file of 10 int values, which call correctly moves the file position to the 5th integer for reading?

reading and writing in files Medium
A. fseek(fp, 4, SEEK_CUR)
B. fseek(fp, 4 * sizeof(int), SEEK_SET)
C. fseek(fp, 5 * sizeof(int), SEEK_SET)
D. fseek(fp, 5, SEEK_SET)

34 What does fread(buf, 4, 10, fp) return when only 6 complete elements are available before end of file?

reading and writing in files Medium
A. 6
B. 4
C. 10
D. 24

35 Which statement about fputc() and fgetc() is correct?

reading and writing in files Medium
A. fgetc() returns an int so it can represent EOF distinct from all valid bytes
B. fgetc() returns a char and never reports EOF
C. fputc() writes an entire string at once
D. Both operate only on files opened in binary mode

36 A file is opened in mode "a". Where is data written regardless of any fseek calls before writing?

opening and closing files Medium
A. At the beginning of the file
B. At the end of the file
C. At the last fseek position
D. At a random position chosen by the OS

37 Reading an integer stored via fprintf(fp, "%d", 300) requires which function to recover the value 300?

text and binary files Medium
A. fgetc(fp)
B. fread(&n, sizeof(int), 1, fp)
C. fscanf(fp, "%d", &n)
D. fseek(fp, 0, SEEK_END)

38 What is the effect of rewind(fp) on an open file?

reading and writing in files Medium
A. It flushes the buffer but keeps the current position
B. It closes and reopens the file
C. It moves the file position to the beginning and clears the end-of-file indicator
D. It deletes the file contents and starts over

39 Which pair of standard streams is buffered and unbuffered respectively by default in most C implementations?

categories of files Medium
A. stderr (buffered) and stdout (unbuffered)
B. stdin (buffered) and stderr (buffered)
C. stdout (buffered when redirected) and stderr (unbuffered)
D. stdin (unbuffered) and stdout (unbuffered)

40 A loop uses while (fscanf(fp, "%d", &x) == 1) to read integers. Why compare against 1?

reading and writing in files Medium
A. 1 is the ASCII code for a valid digit
B. fscanf returns the number of items successfully assigned, so 1 means one integer was read
C. It ensures the loop runs exactly one time
D. fscanf always returns 1 on error

41 Consider the code:
c
FILE *fp = fopen("data.txt", "r");
if (fp == NULL) return 1;
fclose(fp);
fprintf(fp, "hello");

What is the most accurate description of the fprintf call after fclose?

opening and closing files Hard
A. It writes hello to the file because the pointer still holds the address
B. It safely fails and returns a negative value without side effects
C. It causes undefined behavior because fp now refers to a closed stream
D. It reopens the stream automatically in write mode

42 On a Windows system, a program writes the single byte 0x0A (LF) to a file opened in text mode "w". What does the file most likely contain on disk?

text and binary files Hard
A. Two bytes 0x0A 0x00
B. Two bytes 0x0D 0x0A
C. A single byte 0x0D
D. A single byte 0x0A

43 Given int x = 300; written with fwrite(&x, sizeof(int), 1, fp) on a little-endian machine, which byte sequence is stored (assuming 4-byte int)?

reading and writing in files Hard
A. 0x01 0x2C 0x00 0x00
B. 0x2C 0x00 0x00 0x01
C. 0x00 0x00 0x01 0x2C
D. 0x2C 0x01 0x00 0x00

44 Which statement about fseek(fp, 0, SEEK_END) followed by ftell(fp) on a text-mode file is correct?

reading and writing in files Hard
A. The value may not correspond to a simple character count and is only guaranteed meaningful for use with fseek
B. ftell always returns the exact number of characters in the file
C. The returned value is portable and equals the byte count on all systems
D. ftell returns for any text-mode file after SEEK_END

45 A file opened with mode "a+" has content ABC. After fread reads nothing (EOF just reached), the program calls fwrite("XY", 1, 2, fp). Where is XY written?

opening and closing files Hard
A. At the current read position, overwriting nothing
B. At the beginning of the file, overwriting AB
C. Always appended at the end, regardless of the file position indicator
D. Undefined; append mode does not permit writing

46 What does feof(fp) return immediately after fopen succeeds but before any read operation?

reading and writing in files Hard
A. It is undefined until the first fgetc
B. Zero, because the end-of-file indicator is not yet set
C. Nonzero, because the position is at the start
D. , indicating an unknown state

47 Why can reading a struct with fread that was written by fwrite on a different platform produce incorrect values, even with identical source code?

text and binary files Hard
A. Because fwrite compresses data that fread cannot decompress
B. Because fread ignores the size argument across platforms
C. Because binary files always corrupt on transfer
D. Because of differences in endianness, struct padding, and type sizes

48 In the loop while ((c = fgetc(fp)) != EOF) { ... }, why must c be declared as int rather than char?

reading and writing in files Hard
A. Because fgetc requires the argument to be int
B. Because char cannot hold ASCII characters above 127
C. Because EOF and valid byte 0xFF become indistinguishable if stored in a char
D. Because fgetc returns a pointer stored in an int

49 A program opens 2000 files in a loop without closing them and eventually fopen returns NULL. What is the most likely cause?

opening and closing files Hard
A. The disk is full of file data
B. fopen cannot be called more than 1024 times total
C. The process exhausted its limit on open file descriptors
D. The C standard forbids more than 20 simultaneous streams

50 After a successful fwrite to a stream opened "r+", a program immediately calls fread without any intervening call. What does the C standard require?

reading and writing in files Hard
A. The stream to be reopened in "r" mode
B. A call to fflush(stdin) before reading
C. A file-positioning function (e.g., fseek, fsetpos, rewind) between the write and the read
D. Nothing; switching from write to read is always safe

51 A file created in binary mode "wb" stores the two bytes 0x0D 0x0A. It is later opened "rt" (text) on Windows and read with fgetc. How many characters does the program typically read before EOF?

text and binary files Hard
A. Two characters: 0x0D and 0x0A
B. One character: \n
C. Zero characters; the file is treated as empty
D. Three characters due to an added terminator

52 What is the return value of fwrite(buf, 4, 5, fp) if it successfully writes 3 complete elements and part of a 4th before the disk fills?

reading and writing in files Hard
A. , counting the partially written element
B. , the total bytes attempted
C. , because the write did not complete
D. , the number of complete elements written

53 In C's stream model, which statement best distinguishes a text stream from a binary stream?

categories of files Hard
A. Text streams store only ASCII while binary streams store Unicode
B. Text streams cannot be used with fread or fwrite
C. Binary streams are always smaller than text streams
D. A text stream is an ordered sequence of lines subject to character translation, while a binary stream records bytes as-is

54 What happens when fopen("log.txt", "w") is called on an existing non-empty file?

opening and closing files Hard
A. The file position is set to the end but content is kept
B. fopen returns NULL to protect existing data
C. The existing content is preserved and writes append to the end
D. The file is truncated to zero length before any write

55 Given the input line 42abc in a file, what does fscanf(fp, "%d", &n) do to the stream and n?

reading and writing in files Hard
A. Sets n = 42 and consumes the entire line including abc
B. Sets n = 42 and leaves abc\n unread in the stream
C. Sets n = 0 and discards the whole line
D. Returns EOF because the token is mixed

56 Why might data written with fprintf fail to appear in a file even though the program printed no errors, if the program terminates via abort()?

reading and writing in files Hard
A. abort deletes the output file
B. Buffered data in the stream is not flushed because abort bypasses normal fclose/flush at exit
C. fprintf never actually writes to disk
D. The data appears only after the OS reboots

57 A program writes an int array using fwrite in mode "w" (text) instead of "wb" on Windows. What is the primary risk?

text and binary files Hard
A. The integers are converted to their decimal ASCII form
B. fwrite refuses to run in text mode
C. Bytes equal to 0x0A inside the data get expanded to 0x0D 0x0A, corrupting the binary layout
D. Only the first element is written

58 Using fgets(buf, 5, fp) on a line Hello\n, how many characters are stored and what is buf?

reading and writing in files Hard
A. "Hello\0" reading exactly 5 chars
B. "Hell\n" truncated after the newline
C. "Hell" plus a null terminator (4 chars + \0)
D. "Hello" with the newline included

59 What does freopen("out.txt", "w", stdout) accomplish?

opening and closing files Hard
A. Redirects subsequent stdout output to out.txt by reusing the stream
B. Creates a second independent stream leaving stdout unchanged
C. Closes stdout permanently with no replacement
D. Reads out.txt into stdout for display

60 A record file stores fixed-size 20-byte records. To seek directly to the 5th record (1-based) for reading with fread, which call is correct?

reading and writing in files Hard
A. fseek(fp, 4 * 20, SEEK_SET)
B. fseek(fp, 4 * 20, SEEK_END)
C. fseek(fp, 5 * 20, SEEK_SET)
D. fseek(fp, 5 * 20, SEEK_CUR)