Back to Practice C Programming

File Handling - Practice MCQs for CCAT

50 Questions Section B: Programming C Programming

File Handling Question Bank for C-CAT

Topic-wise File Handling MCQs for CDAC C-CAT preparation with answers and explanations.

Q1.
Which function is used to open a file in C?
Afile_open()
Bopen()
Cfopen()
Dopenfile()
Show Answer & Explanation

Correct Answer: C - fopen()

fopen() is the standard library function used to open a file in C. It returns a FILE pointer.

Q2.
What does "r+" mode do in fopen()?
ARead only
BWrite only
CAppend
DRead and write
Show Answer & Explanation

Correct Answer: D - Read and write

The "r+" mode opens a file for both reading and writing. The file must exist.

Q3.
Which function is used to close a file?
Afclose()
Bclose()
Cfileclose()
Dend()
Show Answer & Explanation

Correct Answer: A - fclose()

fclose() is used to close a file that was opened with fopen().

Q4.
What does feof() function check?
AFile open status
BEnd of file
CFile error
DFile size
Show Answer & Explanation

Correct Answer: B - End of file

feof() returns non-zero if the end-of-file indicator is set for the stream.

Q5.
Which function is used to write a character to a file?
Aputc()
Bfputc()
Cwritechar()
DBoth a and b
Show Answer & Explanation

Correct Answer: D - Both a and b

Both putc() and fputc() can be used to write a character to a file. They are essentially the same, with putc() possibly being a macro.

Q6.
What does "w" mode do in fopen()?
AOpens for reading
BOpens for writing (creates/truncates)
COpens for appending
DOpens for reading and writing
Show Answer & Explanation

Correct Answer: B - Opens for writing (creates/truncates)

The "w" mode opens a file for writing. If the file exists, it is truncated. If it does not exist, a new file is created.

Q7.
Which function reads a line from a file?
Afgets()
Bfgetc()
Cfscanf()
Dfread()
Show Answer & Explanation

Correct Answer: A - fgets()

fgets() reads a line (up to specified number of characters or newline) from a file into a string.

Q8.
What is returned by fopen() when it fails to open a file?
A0
B-1
CEOF
DNULL
Show Answer & Explanation

Correct Answer: D - NULL

fopen() returns NULL if it fails to open the file, which should always be checked before file operations.

Q9.
What does the "a" mode in fopen() do?
ARead only
BAppend mode
CWrite only
DRead and write
Show Answer & Explanation

Correct Answer: B - Append mode

The "a" mode opens a file for appending. Data is written at the end of the file without truncating existing content.

Q10.
Which function is used to move the file pointer to a specific location?
Afmove()
Bftell()
Crewind()
Dfseek()
Show Answer & Explanation

Correct Answer: D - fseek()

fseek() is used to move the file pointer to a specific position. It takes file pointer, offset, and origin as arguments.

Q11.
What does ftell() return?
ASize of file
BCurrent position of file pointer
CNumber of records
DEOF status
Show Answer & Explanation

Correct Answer: B - Current position of file pointer

ftell() returns the current position of the file pointer, which indicates how many bytes from the beginning of the file.

Q12.
What does rewind() function do?
ACloses the file
BMoves pointer to beginning
CMoves pointer to end
DDeletes file content
Show Answer & Explanation

Correct Answer: B - Moves pointer to beginning

rewind() moves the file pointer back to the beginning of the file. It is equivalent to fseek(fp, 0, SEEK_SET).

Q13.
Which constant is used with fseek() to seek from the beginning of file?
ASEEK_BEGIN
BSEEK_CUR
CSEEK_END
DSEEK_SET
Show Answer & Explanation

Correct Answer: D - SEEK_SET

SEEK_SET is used to seek from the beginning of the file. SEEK_CUR is from current position, SEEK_END is from end.

Q14.
What is the difference between fprintf() and fputs()?
ANo difference
Bfputs() is formatted
Cfprintf() is formatted, fputs() is unformatted
Dfprintf() only writes numbers
Show Answer & Explanation

Correct Answer: C - fprintf() is formatted, fputs() is unformatted

fprintf() allows formatted output (like printf) to a file, while fputs() writes a string as-is without formatting.

Q15.
What does fread() function do?
AReads a character
BReads a block of data
CReads a string
DReads a line
Show Answer & Explanation

Correct Answer: B - Reads a block of data

fread() reads a specified number of blocks of a given size from a file into a buffer. Used for binary file reading.

Q16.
Which mode opens a file for both reading and writing, creating if it does not exist?
Ar+
Bw+
Ca+
Drw
Show Answer & Explanation

Correct Answer: B - w+

w+ opens for reading and writing. If file exists, it is truncated. If not, a new file is created.

Q17.
What is the purpose of fflush()?
AClose the file
BFlush the output buffer
CDelete file content
DRead remaining data
Show Answer & Explanation

Correct Answer: B - Flush the output buffer

fflush() forces any buffered data to be written to the file immediately, without waiting for the buffer to fill.

Q18.
What is a binary file mode in fopen()?
Abinary
Bbin
Ctext
Db
Show Answer & Explanation

Correct Answer: D - b

Adding b to the mode string, as in rb or wb, opens a file in binary mode and avoids text newline translation.

Q19.
Which function is used to write a block of data to a file?
Afputs()
Bfprintf()
Cfwrite()
Dfputc()
Show Answer & Explanation

Correct Answer: C - fwrite()

fwrite() writes a block of data (specified number of elements of specified size) to a file. Used for binary files.

Q20.
What happens if you try to read a file opened in "w" mode?
AUndefined behavior/error
BFile is read normally
CFile is created
DReturns empty
Show Answer & Explanation

Correct Answer: A - Undefined behavior/error

"w" mode is write-only. Trying to read from a file opened in "w" mode results in undefined behavior or error.

Q21.
Which function is used to open a file in C?
Aopen()
Bcreate()
Cfile_open()
Dfopen()
Show Answer & Explanation

Correct Answer: D - fopen()

fopen() is the standard C library function used to open files. It takes a filename and mode string, returning a FILE pointer.

Q22.
What does the file mode "r" do in fopen()?
ACreates a new file for writing
BOpens an existing file for reading
COpens a file for reading and writing
DAppends to a file
Show Answer & Explanation

Correct Answer: B - Opens an existing file for reading

The mode "r" opens an existing file for reading only. If the file does not exist, fopen() returns NULL.

Q23.
What is the return value of fopen() when a file cannot be opened?
A-1
B0
CNULL
DEOF
Show Answer & Explanation

Correct Answer: C - NULL

fopen() returns NULL if the file cannot be opened. It is good practice to always check the return value before proceeding with file operations.

Q24.
What does the "w" mode do if the file already exists?
AOpens for reading
BTruncates the file to zero length
CAppends to the file
DReturns an error
Show Answer & Explanation

Correct Answer: B - Truncates the file to zero length

The "w" mode creates a new file or truncates an existing file to zero length. All previous contents are lost.

Q25.

What is the output?

FILE *fp = fopen("test.txt", "w");
fprintf(fp, "Score: %d", 95);
fclose(fp);

What is written to test.txt?
AScore: 95
BScore: %d
C95
DNothing
Show Answer & Explanation

Correct Answer: A - Score: 95

fprintf() works like printf() but writes to a file. It formats the output, replacing %d with 95, writing "Score: 95" to the file.

Q26.
Which function reads a single character from a file?
Afgets()
Bfgetc()
Cfscanf()
Dfread()
Show Answer & Explanation

Correct Answer: B - fgetc()

fgetc() reads and returns a single character from the file. It returns EOF when the end of file is reached.

Q27.
What does fclose() return on success?
A1
BNULL
C0
DThe file pointer
Show Answer & Explanation

Correct Answer: C - 0

fclose() returns 0 on success and EOF on failure. It flushes the buffer and releases the file resource.

Q28.
What is the difference between "a" and "w" file modes?
ANo difference
B"w" appends data, "a" overwrites
C"a" is for text, "w" is for binary
D"a" appends data, "w" overwrites the file
Show Answer & Explanation

Correct Answer: D - "a" appends data, "w" overwrites the file

Mode "a" opens a file for appending; data is added to the end without erasing existing content. Mode "w" truncates the file, destroying existing content.

Q29.

What does this code do?

FILE *fp = fopen("data.bin", "rb");
int arr[5];
fread(arr, sizeof(int), 5, fp);
fclose(fp);
AWrites 5 integers to a binary file
BCreates a binary file
CReads 5 integers from a binary file into arr
DReads 5 characters from a file
Show Answer & Explanation

Correct Answer: C - Reads 5 integers from a binary file into arr

fread() reads binary data. Here it reads 5 blocks of sizeof(int) bytes each from the file into the array arr. Mode "rb" opens the file for binary reading.

Q30.
What is the correct prototype of fwrite()?
Asize_t fwrite(const void *ptr, size_t size, size_t count, FILE *fp)
Bint fwrite(char *str, FILE *fp)
Cvoid fwrite(void *ptr, int size, FILE *fp)
DFILE *fwrite(const char *str, const char *mode)
Show Answer & Explanation

Correct Answer: A - size_t fwrite(const void *ptr, size_t size, size_t count, FILE *fp)

fwrite() takes a pointer to data, the size of each element, the number of elements, and the file pointer. It returns the number of elements successfully written.

Q31.
What does ftell() return?
AThe size of the file
BThe file mode
CThe number of characters read
DThe current position of the file pointer
Show Answer & Explanation

Correct Answer: D - The current position of the file pointer

ftell() returns the current position of the file pointer as a long integer, measured in bytes from the beginning of the file.

Q32.
What is the purpose of rewind() in file handling?
AMoves the file pointer to the beginning
BMoves the file pointer to the end
CCloses the file
DDeletes the file
Show Answer & Explanation

Correct Answer: A - Moves the file pointer to the beginning

rewind() sets the file position indicator to the beginning of the file. It is equivalent to fseek(fp, 0, SEEK_SET) but also clears the error indicator.

Q33.
What does fseek(fp, 0, SEEK_END) do?
AMoves to the beginning of the file
BMoves 0 bytes from the current position
CMoves to the end of the file
DCloses the file
Show Answer & Explanation

Correct Answer: C - Moves to the end of the file

fseek(fp, 0, SEEK_END) moves the file pointer 0 bytes from the end of the file, effectively positioning it at the end.

Q34.

What is the output?

FILE *fp = fopen("test.txt", "w");
fputs("Hello\n", fp);
fputs("World\n", fp);
fclose(fp);

What does test.txt contain?
AHello and World on separate lines
BWorld
CHello\nWorld\n
DHello
Show Answer & Explanation

Correct Answer: A - Hello and World on separate lines

fputs() writes strings to the file. The \n characters create newlines, so "Hello" and "World" appear on separate lines.

Q35.
Which file mode opens a file for both reading and writing without truncating it?
Aw+
Br+
Ca+
Drw
Show Answer & Explanation

Correct Answer: B - r+

Mode "r+" opens an existing file for both reading and writing without truncating. "w+" would truncate. "a+" opens for reading and appending.

Q36.
What does feof() check?
AIf the end of file has been reached
BIf the file exists
CIf the file is empty
DIf the file pointer is valid
Show Answer & Explanation

Correct Answer: A - If the end of file has been reached

feof() returns a non-zero value if the end-of-file indicator is set for the stream. It is often used in loops to detect when all data has been read.

Q37.

What is the problem with this code?

FILE *fp = fopen("data.txt", "r");
char line[100];
while(!feof(fp)) {
    fgets(line, 100, fp);
    printf("%s", line);
}
fclose(fp);
AThe last line may be printed twice
BNo problem
Cfgets cannot be used in a loop
Dfclose should come before the loop
Show Answer & Explanation

Correct Answer: A - The last line may be printed twice

feof() returns true only after a read attempt fails. After reading the last line, feof() is still false, so fgets() is called again and fails, but the previous line is printed again.

Q38.
What does the "b" suffix in file modes like "rb" or "wb" indicate?
ABuffered mode
BBackup mode
CBinary mode
DBlocked mode
Show Answer & Explanation

Correct Answer: C - Binary mode

The "b" suffix indicates binary mode. In binary mode, no character translations (like newline conversion) are performed, which is important for non-text files.

Q39.

What is the correct way to read a line from a file?

FILE *fp = fopen("data.txt", "r");
char buf[256];
Afread(buf, fp);
Bgets(buf, fp);
Cfgets(buf, 256, fp);
Dreadline(buf, fp);
Show Answer & Explanation

Correct Answer: C - fgets(buf, 256, fp);

fgets(buf, 256, fp) reads up to 255 characters or until a newline or EOF, whichever comes first, and stores the result in buf with a null terminator.

Q40.
What does fscanf() return?
AThe number of bytes read
BThe file position
C0 or 1
DThe number of items successfully matched and assigned
Show Answer & Explanation

Correct Answer: D - The number of items successfully matched and assigned

fscanf() returns the number of input items successfully matched and assigned. It returns EOF if an input failure occurs before any conversion.

Q41.
How do you determine the size of a file in C?
AUsing sizeof(file)
BUsing fsize()
CUsing fseek() to end and ftell()
DUsing stat() only
Show Answer & Explanation

Correct Answer: C - Using fseek() to end and ftell()

A common approach is fseek(fp, 0, SEEK_END) to move to the end, then ftell(fp) to get the position (which equals the file size in bytes).

Q42.
What does remove() do in C file handling?
ADeletes the file from disk
BRemoves a character from a file
CRemoves a line from a file
DCloses and removes the file pointer
Show Answer & Explanation

Correct Answer: A - Deletes the file from disk

remove() deletes the file whose name is specified. It returns 0 on success and non-zero on failure.

Q43.

What does rename() do in C?

rename("old.txt", "new.txt");
ARenames a variable
BCopies a file
CRenames a file on disk
DCreates an alias
Show Answer & Explanation

Correct Answer: C - Renames a file on disk

rename() renames the file from the first argument to the second argument. It returns 0 on success and non-zero on failure.

Q44.

What is the correct way to write a structure to a binary file?

struct Record { int id; char name[20]; };
struct Record r = {1, "Test"};
FILE *fp = fopen("data.bin", "wb");
Afwrite(&r, sizeof(struct Record), 1, fp);
Bfprintf(fp, "%d %s", r.id, r.name);
Cfputs(r, fp);
Dfputc(r, fp);
Show Answer & Explanation

Correct Answer: A - fwrite(&r, sizeof(struct Record), 1, fp);

fwrite() writes binary data. Writing &r with sizeof(struct Record) bytes writes the entire structure to the file in binary format.

Q45.
What is the difference between text mode and binary mode for files?
AText mode is faster
BThere is no difference on modern systems
CBinary mode cannot read text files
DIn text mode, newline characters may be translated; in binary mode they are not
Show Answer & Explanation

Correct Answer: D - In text mode, newline characters may be translated; in binary mode they are not

In text mode on Windows, '\n' is translated to '\r\n' on write and back on read. Binary mode performs no such translations, preserving exact byte content.

Q46.
What does tmpfile() do?
ACreates a temporary filename
BCreates a backup file
CCreates and opens a temporary file that is automatically deleted when closed
DOpens the temp directory
Show Answer & Explanation

Correct Answer: C - Creates and opens a temporary file that is automatically deleted when closed

tmpfile() creates a temporary binary file in "wb+" mode. The file is automatically deleted when it is closed or the program ends.

Q47.

What is the output?

FILE *fp = fopen("nums.txt", "w");
for(int i = 1; i <= 5; i++)
    fprintf(fp, "%d ", i * 10);
fclose(fp);

What is in nums.txt?
A10 20 30 40 50
B1 2 3 4 5
C12345
DCompilation error
Show Answer & Explanation

Correct Answer: A - 10 20 30 40 50

The loop writes i*10 for i from 1 to 5 with spaces, producing "10 20 30 40 50 " in the file.

Q48.
What does fflush() do?
AForces buffered output to be written to the file
BEmpties the file content
CCloses the file
DResets the file pointer
Show Answer & Explanation

Correct Answer: A - Forces buffered output to be written to the file

fflush() forces any buffered but unwritten data to be written to the output file. It is useful to ensure data is saved immediately.

Q49.
What happens if you open a non-existent file with mode "r"?
AA new empty file is created
BThe program crashes
Cfopen() returns a valid pointer to an empty file
Dfopen() returns NULL
Show Answer & Explanation

Correct Answer: D - fopen() returns NULL

Mode "r" requires the file to exist. If it does not exist, fopen() returns NULL. You should always check the return value before using the file pointer.

Q50.

What is the purpose of the third parameter in fseek()?

fseek(fp, offset, whence);
AThe file mode
BThe reference point: SEEK_SET (beginning), SEEK_CUR (current), or SEEK_END (end)
CThe number of bytes to skip
DThe buffer size
Show Answer & Explanation

Correct Answer: B - The reference point: SEEK_SET (beginning), SEEK_CUR (current), or SEEK_END (end)

The third parameter (whence) specifies the reference position for the offset: SEEK_SET (beginning of file), SEEK_CUR (current position), or SEEK_END (end of file).

Showing 1-10 of 50 questions