C File Handling MCQ

In C programming, file handling refers to the series of operations like creating, opening, reading, writing, and closing a file. The C language equips us with various functions, including fopen(), fwrite(), fread(), fseek(), fprintf(), and more, which enable us to execute a range of file operations in our code. In this quiz, we’ll test your knowledge on the basics of file handling in C. Ready? Let’s dive in!

Note that each question is followed by the correct answer and an explanation to help reinforce your knowledge.

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

a) file_open()
b) open_file()
c) fopen()
d) open()

Answer:

c) fopen()

Explanation:

In C, the fopen() function is used to open a file. It returns a pointer to the file if successful or NULL if there’s an error.

2. What does the “r” mode signify when opening a file using fopen()?

a) Write mode
b) Read mode
c) Append mode
d) Exclusive mode

Answer:

b) Read mode

Explanation:

The “r” mode stands for “read” and it opens the file for reading.

3. Which function is used to close an opened file?

a) fclose()
b) file_close()
c) close_file()
d) close()

Answer:

a) fclose()

Explanation:

The fclose() function is used to close an opened file in C.

4. How do you read a character from a file?

a) fscanf()
b) fprint()
c) fputc()
d) fgetc()

Answer:

d) fgetc()

Explanation:

The fgetc() function reads a character from a file.

5. Which function writes a string to a file?

a) fputs()
b) fgets()
c) fread()
d) fwrite()

Answer:

a) fputs()

Explanation:

The fputs() function writes a string to the given file.

6. What is the return value of feof() when the end of file is reached?

a) 0
b) 1
c) -1
d) NULL

Answer:

b) 1

Explanation:

The feof() function returns 1 when the end of the file is reached.

7. Which function is used to seek a specific position in a file?

a) fseek()
b) flocate()
c) fposition()
d) fmove()

Answer:

a) fseek()

Explanation:

The fseek() function is used to change the file position.

8. How can you get the current position of the file pointer?

a) ftell()
b) fshow()
c) fpos()
d) fcurrent()

Answer:

a) ftell()

Explanation:

The ftell() function gives the current position of the file pointer.

9. Which function is used to read blocks of data from a file?

a) fblockread()
b) fread()
c) fgetline()
d) fgetblock()

Answer:

b) fread()

Explanation:

The fread() function is used to read blocks of data from a file.

10. If you want to open a file in both read and write mode without truncating it, which mode should you use?

a) “rw”
b) “w+”
c) “r+”
d) “rw+”

Answer:

c) “r+”

Explanation:

The mode “r+” opens the file for both reading and writing without truncating it.


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top