C Programming

File Handling — Practice MCQs for CCAT

20 Questions Section B: Programming C Programming

Practice 20 File Handling multiple-choice questions designed for CDAC CCAT exam preparation. Click "Show Answer" to reveal the correct option with detailed explanation.

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

Correct Answer: A — 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
CRead and write
DAppend
Show Answer & Explanation

Correct Answer: C — 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()
CBoth a and b
Dwritechar()
Show Answer & Explanation

Correct Answer: C — 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?
Afgetc()
Bfgets()
Cfscanf()
Dfread()
Show Answer & Explanation

Correct Answer: B — 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
CNULL
DEOF
Show Answer & Explanation

Correct Answer: C — 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
BWrite only
CAppend mode
DRead and write
Show Answer & Explanation

Correct Answer: C — 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?
Afseek()
Bftell()
Crewind()
Dfmove()
Show Answer & Explanation

Correct Answer: A — 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 end
CMoves pointer to beginning
DDeletes file content
Show Answer & Explanation

Correct Answer: C — 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_SET
BSEEK_CUR
CSEEK_END
DSEEK_BEGIN
Show Answer & Explanation

Correct Answer: A — 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
Bfprintf() is formatted, fputs() is unformatted
Cfputs() is formatted
Dfprintf() only writes numbers
Show Answer & Explanation

Correct Answer: B — 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 string
CReads a block of data
DReads a line
Show Answer & Explanation

Correct Answer: C — 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()?
Ab
Bbin
Cbinary
DB
Show Answer & Explanation

Correct Answer: A — b

Adding "b" to the mode (like "rb", "wb") opens the file in binary mode, which does not translate newline characters.

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

Correct Answer: A — 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?
AFile is read normally
BUndefined behavior/error
CFile is created
DReturns empty
Show Answer & Explanation

Correct Answer: B — Undefined behavior/error

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