Unit 6: File Handling - Practice Quiz
1 Which library function is used to open a file in C?
open()
openfile()
fopen()
fileopen()
2
What does fopen() return if it fails to open a file?
EOF
0
-1
NULL
3 Which mode string opens a file only for reading?
"rw"
"r"
"w"
"a"
4 Which function is used to close a file in C?
fclose()
fileclose()
endfile()
close()
5 What is the correct data type of the pointer used to handle files in C?
char *
FILE *
int *
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?
"r"
"w"
"a"
"x"
7 Which mode is used to add data at the end of an existing file without deleting its contents?
"a"
"t"
"r"
"w"
8 Which character is added to a mode string to open a file in binary mode?
x
b
n
t
9 In which type of file is data stored as human-readable characters?
10 Which type of file stores data in the same format as it is held in memory?
11 Which function writes a single character to a file?
fputc()
fgetc()
fscanf()
putchar()
12 Which function reads a single character from a file?
getchar()
fgetc()
fputc()
fprintf()
13 Which function is used to write formatted output to a file?
fprintf()
fscanf()
fwrite()
printf()
14 Which function reads formatted input from a file?
scanf()
fscanf()
fprintf()
fgets()
15 Which function is used to write a whole string to a file?
puts()
fputc()
fgets()
fputs()
16 Which function reads a line or string from a file?
gets()
fgets()
fgetc()
fputs()
17 Which pair of functions is typically used for reading and writing blocks of data in binary files?
fgets() and fputs()
fscanf() and fprintf()
fread() and fwrite()
fgetc() and fputc()
18
What does EOF stand for in C file handling?
19 Based on how their data is stored, files in C are broadly categorized into which two types?
20 Which of the following is a valid reason for using files in a C program?
21
What does the fopen() function return if it fails to open the specified file?
0
-1
EOF
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?
fopen returns NULL
23
Why is calling fclose() on a file opened for writing important?
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?
"w+"
"rb"
"r+"
"a+"
25 On a Windows system, what is the main effect of opening a file in text mode versus binary mode?
\n ↔ \r\n) while binary mode does not
26
Why is fwrite() generally preferred over fprintf() for storing a large array of float values?
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?
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?
stderr
stdlog
stddbg
stdaux
29 What distinguishes a sequential-access file from a random-access file in C?
fseek
fclose
FILE pointer
30
Consider the code below. What is written to out.txt?
FILE *fp = fopen("out.txt", "w");
fprintf(fp, "%d-%d", 5, 3);
fclose(fp);
5-3
%d-%d
53
5 - 3
31 Which function is most appropriate for reading a full line of text (including spaces) from a file into a buffer?
getchar()
fread() of a single char
fgets()
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?
EOF
NULL
0
33
Given a binary file of 10 int values, which call correctly moves the file position to the 5th integer for reading?
fseek(fp, 4, SEEK_CUR)
fseek(fp, 4 * sizeof(int), SEEK_SET)
fseek(fp, 5 * sizeof(int), SEEK_SET)
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?
6
4
10
24
35
Which statement about fputc() and fgetc() is correct?
fgetc() returns an int so it can represent EOF distinct from all valid bytes
fgetc() returns a char and never reports EOF
fputc() writes an entire string at once
36
A file is opened in mode "a". Where is data written regardless of any fseek calls before writing?
fseek position
37
Reading an integer stored via fprintf(fp, "%d", 300) requires which function to recover the value 300?
fgetc(fp)
fread(&n, sizeof(int), 1, fp)
fscanf(fp, "%d", &n)
fseek(fp, 0, SEEK_END)
38
What is the effect of rewind(fp) on an open file?
39 Which pair of standard streams is buffered and unbuffered respectively by default in most C implementations?
stderr (buffered) and stdout (unbuffered)
stdin (buffered) and stderr (buffered)
stdout (buffered when redirected) and stderr (unbuffered)
stdin (unbuffered) and stdout (unbuffered)
40
A loop uses while (fscanf(fp, "%d", &x) == 1) to read integers. Why compare against 1?
1 is the ASCII code for a valid digit
fscanf returns the number of items successfully assigned, so 1 means one integer was read
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?
hello to the file because the pointer still holds the address
fp now refers to a closed stream
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?
0x0A 0x00
0x0D 0x0A
0x0D
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)?
0x01 0x2C 0x00 0x00
0x2C 0x00 0x00 0x01
0x00 0x00 0x01 0x2C
0x2C 0x01 0x00 0x00
44
Which statement about fseek(fp, 0, SEEK_END) followed by ftell(fp) on a text-mode file is correct?
fseek
ftell always returns the exact number of characters in the file
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?
AB
46
What does feof(fp) return immediately after fopen succeeds but before any read operation?
fgetc
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?
fwrite compresses data that fread cannot decompress
fread ignores the size argument across platforms
48
In the loop while ((c = fgetc(fp)) != EOF) { ... }, why must c be declared as int rather than char?
fgetc requires the argument to be int
char cannot hold ASCII characters above 127
EOF and valid byte 0xFF become indistinguishable if stored in a char
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?
fopen cannot be called more than 1024 times total
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?
"r" mode
fflush(stdin) before reading
fseek, fsetpos, rewind) between the write and the read
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?
0x0D and 0x0A
\n
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?
53 In C's stream model, which statement best distinguishes a text stream from a binary stream?
fread or fwrite
54
What happens when fopen("log.txt", "w") is called on an existing non-empty file?
fopen returns NULL to protect existing data
55
Given the input line 42abc in a file, what does fscanf(fp, "%d", &n) do to the stream and n?
n = 42 and consumes the entire line including abc
n = 42 and leaves abc\n unread in the stream
n = 0 and discards the whole line
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()?
abort deletes the output file
abort bypasses normal fclose/flush at exit
fprintf never actually writes to disk
57
A program writes an int array using fwrite in mode "w" (text) instead of "wb" on Windows. What is the primary risk?
fwrite refuses to run in text mode
0x0A inside the data get expanded to 0x0D 0x0A, corrupting the binary layout
58
Using fgets(buf, 5, fp) on a line Hello\n, how many characters are stored and what is buf?
"Hello\0" reading exactly 5 chars
"Hell\n" truncated after the newline
"Hell" plus a null terminator (4 chars + \0)
"Hello" with the newline included
59
What does freopen("out.txt", "w", stdout) accomplish?
stdout output to out.txt by reusing the stream
stdout unchanged
stdout permanently with no replacement
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?
fseek(fp, 4 * 20, SEEK_SET)
fseek(fp, 4 * 20, SEEK_END)
fseek(fp, 5 * 20, SEEK_SET)
fseek(fp, 5 * 20, SEEK_CUR)
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →