Notice: Due to an ongoing migration of our storage and publishing system, material requested since last month will not be uploaded for the time being, and new requests are paused. All existing notes remain available.
Question 1 of 60 · opening and closing files · Easy
Which library function is used to open a file in C?
Not quite. Try another option.
Answer: fopen()
The standard C library function fopen() opens a file and returns a FILE pointer that is used for subsequent file operations.
Question 2 of 60 · opening and closing files · Easy
What does fopen() return if it fails to open a file?
Not quite. Try another option.
Answer: NULL
If the file cannot be opened, fopen() returns NULL, so its return value should always be checked before use.
Question 3 of 60 · opening and closing files · Easy
Which mode string opens a file only for reading?
Not quite. Try another option.
Answer: "r"
The mode "r" opens an existing file for reading only. If the file does not exist, fopen() returns NULL.
Question 4 of 60 · opening and closing files · Easy
Which function is used to close a file in C?
Not quite. Try another option.
Answer: fclose()
fclose() closes an open file, flushes any buffered data to disk, and releases the associated resources.
Question 5 of 60 · opening and closing files · Easy
What is the correct data type of the pointer used to handle files in C?
Not quite. Try another option.
Answer: FILE *
File operations use a pointer of type FILE *, which stores information about the file stream being accessed.
Question 6 of 60 · opening and closing files · Easy
Which mode opens a file for writing and creates it if it does not exist, erasing any existing content when it does exist?
Not quite. Try another option.
Answer: "w"
The "w" mode creates a new file for writing or truncates an existing file to zero length before writing.
Question 7 of 60 · opening and closing files · Easy
Which mode is used to add data at the end of an existing file without deleting its contents?
Not quite. Try another option.
Answer: "a"
The append mode "a" opens a file for writing at its end, preserving existing content and creating the file if it does not exist.
Question 8 of 60 · text and binary files · Easy
Which character is added to a mode string to open a file in binary mode?
Not quite. Try another option.
Answer: b
Adding b to a mode string, such as "rb" or "wb", opens the file in binary mode instead of the default text mode.
Question 9 of 60 · text and binary files · Easy
In which type of file is data stored as human-readable characters?
Not quite. Try another option.
Answer: Text file
A text file stores data as readable characters, where each value is represented using its character encoding such as ASCII.
Question 10 of 60 · text and binary files · Easy
Which type of file stores data in the same format as it is held in memory?
Not quite. Try another option.
Answer: Binary file
A binary file stores data in raw byte form, matching the memory representation, without converting values to readable characters.
Question 11 of 60 · reading and writing in files · Easy
Which function writes a single character to a file?
Not quite. Try another option.
Answer: fputc()
fputc() writes one character to the specified file stream at the current position.
Question 12 of 60 · reading and writing in files · Easy
Which function reads a single character from a file?
Not quite. Try another option.
Answer: fgetc()
fgetc() reads and returns one character from the file, or returns EOF when the end of the file is reached.
Question 13 of 60 · reading and writing in files · Easy
Which function is used to write formatted output to a file?
Not quite. Try another option.
Answer: fprintf()
fprintf() works like printf() but sends its formatted output to a file stream instead of the screen.
Question 14 of 60 · reading and writing in files · Easy
Which function reads formatted input from a file?
Not quite. Try another option.
Answer: fscanf()
fscanf() reads formatted data from a file stream, similar to how scanf() reads from the keyboard.
Question 15 of 60 · reading and writing in files · Easy
Which function is used to write a whole string to a file?
Not quite. Try another option.
Answer: fputs()
fputs() writes a null-terminated string to the specified file stream.
Question 16 of 60 · reading and writing in files · Easy
Which function reads a line or string from a file?
Not quite. Try another option.
Answer: fgets()
fgets() reads a string from a file up to a specified number of characters or until a newline is encountered.
Question 17 of 60 · reading and writing in files · Easy
Which pair of functions is typically used for reading and writing blocks of data in binary files?
Not quite. Try another option.
Answer: fread() and fwrite()
fread() and fwrite() transfer blocks of raw bytes to and from a file, making them well suited for binary file operations.
Question 18 of 60 · reading and writing in files · Easy
What does EOF stand for in C file handling?
Not quite. Try another option.
Answer: End Of File
EOF stands for End Of File and is a constant returned by input functions when there is no more data to read.
Question 19 of 60 · categories of files · Easy
Based on how their data is stored, files in C are broadly categorized into which two types?
Not quite. Try another option.
Answer: Text files and binary files
Files in C are broadly classified as text files, which store readable characters, and binary files, which store raw bytes.
Question 20 of 60 · categories of files · Easy
Which of the following is a valid reason for using files in a C program?
Not quite. Try another option.
Answer: To store data permanently so it is available even after the program ends and can be reloaded on the next run
Files provide persistent storage, allowing data to be saved on disk and retrieved later, unlike variables that lose their values when the program ends.
Question 21 of 60 · opening and closing files · Medium
What does the fopen() function return if it fails to open the specified file?
Not quite. Try another option.
Answer: NULL
fopen() returns a FILE * pointer on success. On failure (file missing, no permission, etc.), it returns NULL, which should always be checked before using the pointer.
Question 22 of 60 · opening and closing files · Medium
A program opens a file using fopen("data.txt", "w") when data.txt already exists with content. What happens to the existing content?
Not quite. Try another option.
Answer: The existing content is erased and the file is truncated to zero length
Mode "w" opens a file for writing and truncates it to zero length if it exists. To preserve and add data, mode "a" (append) should be used instead.
Question 23 of 60 · opening and closing files · Medium
Why is calling fclose() on a file opened for writing important?
Not quite. Try another option.
Answer: It flushes buffered data to disk and releases the file resource
Output is often buffered in memory. fclose() flushes any remaining buffered data to disk and frees the associated resources. Forgetting it can cause data loss or resource leaks.
Question 24 of 60 · opening and closing files · Medium
Which mode string opens a file for both reading and writing, creating it if it does not exist and truncating it if it does?
Not quite. Try another option.
Answer: "w+"
"w+" opens for read and write, creates the file if absent, and truncates it if present. "r+" requires the file to exist, and "a+" appends without truncating.
Question 25 of 60 · text and binary files · Medium
On a Windows system, what is the main effect of opening a file in text mode versus binary mode?
Not quite. Try another option.
Answer: Text mode translates newline characters (\n ↔ \r\n) while binary mode does not
In text mode on Windows, newline translation occurs between \n and \r\n. Binary mode reads and writes bytes exactly as they are, with no translation.
Question 26 of 60 · text and binary files · Medium
Why is fwrite() generally preferred over fprintf() for storing a large array of float values?
Not quite. Try another option.
Answer: It stores the raw byte representation, saving space and avoiding text conversion
fwrite() writes binary data directly, avoiding the overhead and precision loss of converting floats to text. It is usually more compact and faster than fprintf() for numeric data.
Question 27 of 60 · text and binary files · Medium
A structure written to a binary file with fwrite on one machine may not read correctly on another. What is the most common reason?
Not quite. Try another option.
Answer: Differences in byte order (endianness) and structure padding between machines
Raw binary dumps depend on the platform's endianness and how the compiler pads structure members. These can differ across architectures, making the file non-portable.
Question 28 of 60 · categories of files · Medium
Which of the following streams is automatically opened when a C program starts and is associated with error output?
Not quite. Try another option.
Answer: stderr
The three standard streams opened at startup are stdin, stdout, and stderr. stderr is used for error messages and is typically unbuffered.
Question 29 of 60 · categories of files · Medium
What distinguishes a sequential-access file from a random-access file in C?
Not quite. Try another option.
Answer: Sequential access reads data in order, while random access can jump to any position using fseek
Sequential access processes data from start to end in order. Random access uses functions like fseek() and ftell() to move directly to any byte offset in the file.
Question 30 of 60 · reading and writing in files · Medium
Consider the code below. What is written to out.txt?
fprintf formats output just like printf but writes to the file. The format "%d-%d" produces the two integers separated by a hyphen, giving 5-3.
Question 31 of 60 · reading and writing in files · Medium
Which function is most appropriate for reading a full line of text (including spaces) from a file into a buffer?
Not quite. Try another option.
Answer: fgets()
fgets() reads characters until a newline or the buffer limit, preserving spaces within the line. fscanf("%s") stops at the first whitespace, so it cannot read a whole line.
Question 32 of 60 · reading and writing in files · Medium
What value does feof(fp) return immediately after a successful read that has NOT yet reached the end of the file?
Not quite. Try another option.
Answer: 0
feof() returns non-zero only after a read operation has attempted to read past the end of the file. Before that, it returns 0. This is why looping on feof() alone can read the last item twice.
Question 33 of 60 · reading and writing in files · Medium
Given a binary file of 10 int values, which call correctly moves the file position to the 5th integer for reading?
Not quite. Try another option.
Answer: fseek(fp, 4 * sizeof(int), SEEK_SET)
Positions are zero-based, so the 5th integer is at index 4. Offset must be in bytes: from the start (SEEK_SET).
Question 34 of 60 · reading and writing in files · Medium
What does fread(buf, 4, 10, fp) return when only 6 complete elements are available before end of file?
Not quite. Try another option.
Answer: 6
fread returns the number of complete elements successfully read, not bytes. With element size 4 and 6 full elements available, it returns 6.
Question 35 of 60 · reading and writing in files · Medium
Which statement about fputc() and fgetc() is correct?
Not quite. Try another option.
Answer: fgetc() returns an int so it can represent EOF distinct from all valid bytes
fgetc() returns an int (not char) precisely so that EOF (usually -1) can be distinguished from every valid unsigned byte value. Storing its result in a char can cause bugs.
Question 36 of 60 · opening and closing files · Medium
A file is opened in mode "a". Where is data written regardless of any fseek calls before writing?
Not quite. Try another option.
Answer: At the end of the file
In append mode, all writes go to the end of the file. Even if fseek repositions for reading, output is forced to the end before each write.
Question 37 of 60 · text and binary files · Medium
Reading an integer stored via fprintf(fp, "%d", 300) requires which function to recover the value 300?
Not quite. Try another option.
Answer: fscanf(fp, "%d", &n)
fprintf wrote the value as text characters ('3','0','0'). To parse text back into an integer, use fscanf with %d. fread would misinterpret the raw characters.
Question 38 of 60 · reading and writing in files · Medium
What is the effect of rewind(fp) on an open file?
Not quite. Try another option.
Answer: It moves the file position to the beginning and clears the end-of-file indicator
rewind(fp) sets the file position to the start and clears both the error and EOF indicators, equivalent to fseek(fp, 0, SEEK_SET) plus clearerr.
Question 39 of 60 · categories of files · Medium
Which pair of standard streams is buffered and unbuffered respectively by default in most C implementations?
Not quite. Try another option.
Answer: stdout (buffered when redirected) and stderr (unbuffered)
stderr is unbuffered so error messages appear immediately. stdout is typically line-buffered to a terminal and fully buffered when redirected to a file.
Question 40 of 60 · reading and writing in files · Medium
A loop uses while (fscanf(fp, "%d", &x) == 1) to read integers. Why compare against 1?
Not quite. Try another option.
Answer: fscanf returns the number of items successfully assigned, so 1 means one integer was read
fscanf returns the count of input items successfully matched and assigned. Requesting one %d means a return of 1 indicates success; EOF or a mismatch ends the loop.
Question 41 of 60 · opening and closing files · Hard
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?
Not quite. Try another option.
Answer: It causes undefined behavior because fp now refers to a closed stream
After fclose, the FILE object is deallocated. Using fp in any I/O call is undefined behavior; the pointer is dangling even though it still holds an address.
Question 42 of 60 · text and binary files · Hard
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?
Not quite. Try another option.
Answer: Two bytes 0x0D 0x0A
In text mode on Windows, each \n (LF, 0x0A) written is translated to the CRLF pair 0x0D 0x0A on disk. Binary mode would avoid this translation.
Question 43 of 60 · reading and writing in files · Hard
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)?
Not quite. Try another option.
Answer: 0x2C 0x01 0x00 0x00
. On little-endian storage, the least significant byte comes first: 0x2C 0x01 0x00 0x00.
Question 44 of 60 · reading and writing in files · Hard
Which statement about fseek(fp, 0, SEEK_END) followed by ftell(fp) on a text-mode file is correct?
Not quite. Try another option.
Answer: The value may not correspond to a simple character count and is only guaranteed meaningful for use with fseek
For text streams, ftell returns an implementation-defined value (due to newline translation) that is only guaranteed usable as a later fseek offset, not as a raw character count.
Question 45 of 60 · opening and closing files · Hard
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?
Not quite. Try another option.
Answer: Always appended at the end, regardless of the file position indicator
In append modes ("a"/"a+"), all writes go to the end of the file regardless of the current position, because the write position is repositioned to EOF before each write.
Question 46 of 60 · reading and writing in files · Hard
What does feof(fp) return immediately after fopen succeeds but before any read operation?
Not quite. Try another option.
Answer: Zero, because the end-of-file indicator is not yet set
The EOF indicator is only set after a read attempt fails at end of file. Right after opening, no read has occurred, so feof returns 0 (false).
Question 47 of 60 · text and binary files · Hard
Why can reading a struct with fread that was written by fwrite on a different platform produce incorrect values, even with identical source code?
Not quite. Try another option.
Answer: Because of differences in endianness, struct padding, and type sizes
Binary layouts depend on the platform's byte order, member alignment/padding, and integer sizes. Cross-platform reads of raw struct bytes are not portable unless a fixed serialization format is used.
Question 48 of 60 · reading and writing in files · Hard
In the loop while ((c = fgetc(fp)) != EOF) { ... }, why must c be declared as int rather than char?
Not quite. Try another option.
Answer: Because EOF and valid byte 0xFF become indistinguishable if stored in a char
fgetc returns an unsigned char value promoted to int, or EOF (). A signed char holding 0xFF also equals , so the loop could stop early or never on valid data. int avoids this.
Question 49 of 60 · opening and closing files · Hard
A program opens 2000 files in a loop without closing them and eventually fopen returns NULL. What is the most likely cause?
Not quite. Try another option.
Answer: The process exhausted its limit on open file descriptors
Each open stream consumes a file descriptor. Without fclose, the per-process descriptor limit is reached and fopen fails, returning NULL. FOPEN_MAX is only a guaranteed minimum.
Question 50 of 60 · reading and writing in files · Hard
After a successful fwrite to a stream opened "r+", a program immediately calls fread without any intervening call. What does the C standard require?
Not quite. Try another option.
Answer: A file-positioning function (e.g., fseek, fsetpos, rewind) between the write and the read
For update modes, output must be followed by a positioning operation (or fflush) before input, and vice versa. Skipping it gives undefined behavior.
Question 51 of 60 · text and binary files · Hard
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?
Not quite. Try another option.
Answer: One character: \n
In text mode on Windows, the CRLF pair 0x0D 0x0A is translated back to a single \n on input. So two stored bytes read as one character.
Question 52 of 60 · reading and writing in files · Hard
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?
Not quite. Try another option.
Answer: , the number of complete elements written
fwrite returns the number of complete elements successfully written, not bytes. A partially written element is not counted, so it returns 3.
Question 53 of 60 · categories of files · Hard
In C's stream model, which statement best distinguishes a text stream from a binary stream?
Not quite. Try another option.
Answer: A text stream is an ordered sequence of lines subject to character translation, while a binary stream records bytes as-is
The C standard defines a text stream as lines that may undergo mapping (e.g., newline translation), whereas a binary stream reads/writes bytes unchanged (aside from possible trailing null padding).
Question 54 of 60 · opening and closing files · Hard
What happens when fopen("log.txt", "w") is called on an existing non-empty file?
Not quite. Try another option.
Answer: The file is truncated to zero length before any write
Mode "w" opens for writing and truncates an existing file to zero length (or creates it). To preserve content and append, use "a".
Question 55 of 60 · reading and writing in files · Hard
Given the input line 42abc in a file, what does fscanf(fp, "%d", &n) do to the stream and n?
Not quite. Try another option.
Answer: Sets n = 42 and leaves abc\n unread in the stream
%d reads the numeric prefix 42, stops at the non-numeric a, and pushes it back. abc remains in the buffer for subsequent reads, returning 1.
Question 56 of 60 · reading and writing in files · Hard
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()?
Not quite. Try another option.
Answer: Buffered data in the stream is not flushed because abort bypasses normal fclose/flush at exit
Stream output is buffered. Normal termination flushes and closes streams, but abort() performs abnormal termination and buffers may not be flushed, losing pending data.
Question 57 of 60 · text and binary files · Hard
A program writes an int array using fwrite in mode "w" (text) instead of "wb" on Windows. What is the primary risk?
Not quite. Try another option.
Answer: Bytes equal to 0x0A inside the data get expanded to 0x0D 0x0A, corrupting the binary layout
In text mode any data byte that equals LF (0x0A) is translated to CRLF on output, inserting an extra byte and misaligning the binary data on read-back.
Question 58 of 60 · reading and writing in files · Hard
Using fgets(buf, 5, fp) on a line Hello\n, how many characters are stored and what is buf?
Not quite. Try another option.
Answer: "Hell" plus a null terminator (4 chars + \0)
fgets reads at most characters, then appends \0. So buf holds Hell and the rest (o\n) stays in the stream.
Question 59 of 60 · opening and closing files · Hard
What does freopen("out.txt", "w", stdout) accomplish?
Not quite. Try another option.
Answer: Redirects subsequent stdout output to out.txt by reusing the stream
freopen closes the existing stream and associates the same FILE* (here stdout) with the new file/mode. It is commonly used to redirect standard streams to files.
Question 60 of 60 · reading and writing in files · Hard
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?
Not quite. Try another option.
Answer: fseek(fp, 4 * 20, SEEK_SET)
Records are 0-indexed by offset. The 5th record starts after 4 preceding records: offset bytes from the beginning (SEEK_SET).
That's unit 6
Answer a few questions and your score shows up here.
Nearly through. Did unit 6 of CAP1008 save you a night before the exam?
LPU Notes is free. A student pays for the servers.
Did unit 6 of CAP1008 save you a night before the exam?
Then you can help keep it free for the next batch. 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.