C File Handling Quiz - MCQ Questions and Answers

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

a) open()
b) fopen()
c) create()
d) fileopen()

Answer:

b) fopen()

Explanation:

The fopen() function is used to open a file in C. It requires the file name and the mode (like 'r' for read, 'w' for write) as arguments.

2. What does the 'r+' mode do when opening a file with fopen() in C?

a) Opens the file for reading only
b) Opens the file for writing only
c) Opens the file for both reading and writing
d) Opens the file in read-only and append mode

Answer:

c) Opens the file for both reading and writing

Explanation:

The 'r+' mode opens a file for both reading and writing. The file must exist before opening it in this mode.

3. What is the return type of the fopen() function in C?

a) int
b) FILE*
c) char*
d) bool

Answer:

b) FILE*

Explanation:

fopen() returns a pointer to a FILE object. If the file cannot be opened, it returns NULL.

4. Which function is used to close a file in C?

a) close()
b) fclose()
c) endfile()
d) fileclose()

Answer:

b) fclose()

Explanation:

The fclose() function is used to close an open file. It takes a pointer to a FILE object as its argument.

5. What is the purpose of the fprintf() function in C?

a) To print to the console
b) To read from a file
c) To write formatted output to a file
d) To format a string

Answer:

c) To write formatted output to a file

Explanation:

fprintf() is used to write formatted data to a file. It is similar to printf() but outputs to a file instead of the console.

6. How can you read a single character from a file in C?

a) read()
b) getc()
c) scanf()
d) readchar()

Answer:

b) getc()

Explanation:

The getc() function reads a single character from a specified file and returns it. It takes a pointer to a FILE object as its argument.

7. What is the function feof() used for in C?

a) To close a file
b) To write to a file
c) To check if the end of the file has been reached
d) To flush the file buffer

Answer:

c) To check if the end of the file has been reached

Explanation:

The feof() function tests the end-of-file indicator for the given file stream and returns a non-zero value if the end of the file is reached.

8. Which of the following correctly reads a string from a file in C?

a) fscanf(file, "%s", str)
b) fprintf(file, "%s", str)
c) fread(file, "%s", str)
d) fput(file, "%s", str)

Answer:

a) fscanf(file, "%s", str)

Explanation:

fscanf() is used to read formatted input from a file. It works similarly to scanf() but reads from a file instead of standard input.

9. What is the output of the following C code if the file "data.txt" contains the text "Hello"?

   FILE *file = fopen("data.txt", "r");
   char str[10];
   fscanf(file, "%s", str);
   printf("%s", str);
   fclose(file);
a) Hello
b) Error
c) Undefined behavior
d) Nothing is printed

Answer:

a) Hello

Explanation:

The code successfully opens "data.txt", reads the string "Hello" into str, prints it, and then closes the file.

10. How do you write a single character to a file in C?

a) write()
b) putc()
c) fprintf()
d) fputc()

Answer:

d) fputc()

Explanation:

The fputc() function writes a single character to the specified file. It takes the character and a FILE pointer as arguments.

11. Which function is used to read a block of data from a file in C?

a) fscanf()
b) fread()
c) getline()
d) readblock()

Answer:

b) fread()

Explanation:

fread() is used for reading a block of data from a file. It requires pointers to a buffer, the size of each element, the number of elements, and a FILE pointer.

12. What is the purpose of fseek() function in C?

a) To seek a particular position in a file
b) To find a specific character in a file
c) To check the file size
d) To split a file into multiple parts

Answer:

a) To seek a particular position in a file

Explanation:

fseek() is used to move the file pointer to a specific location in a file, defined by a given offset and a reference position.

13. What does the 'w' mode do when opening a file with fopen() in C?

a) Opens the file for writing only
b) Opens the file for reading only
c) Opens the file for both reading and writing
d) Opens the file in write-protected mode

Answer:

a) Opens the file for writing only

Explanation:

The 'w' mode opens a file for writing. If the file exists, it is truncated to zero length; otherwise, a new file is created.

14. How do you check for errors when opening a file in C?

a) Using the error() function
b) Checking if the returned FILE pointer is NULL
c) Using the perror() function
d) By reading the error flag of the file

Answer:

b) Checking if the returned FILE pointer is NULL

Explanation:

When opening a file, you should check if the returned FILE pointer is NULL, which indicates an error occurred during file opening.

15. What is the return value of the fwrite() function in C?

a) The number of elements successfully written
b) The number of bytes successfully written
c) 0 on success, non-zero on error
d) A pointer to the file

Answer:

a) The number of elements successfully written

Explanation:

fwrite() returns the number of elements successfully written to the file, which may be less than the number requested if an error occurs.

16. What is the purpose of the fflush() function in C?

a) To refresh the file contents
b) To flush the output buffer of a stream
c) To reset the file pointer
d) To clean up file errors

Answer:

b) To flush the output buffer of a stream

Explanation:

fflush() is used to flush the output buffer of a file stream, ensuring all buffered data is written to the file.

17. How can you append data to an existing file in C?

a) Using the 'r+' mode in fopen()
b) Using the 'a' mode in fopen()
c) Using the fwrite() function
d) It is not possible to append data

Answer:

b) Using the 'a' mode in fopen()

Explanation:

The 'a' mode in fopen() opens a file for writing at the end of the file without truncating it, allowing data to be appended.

18. What happens if you try to write to a file opened in read-only mode in C?

a) The data is written successfully
b) A runtime error occurs
c) The write operation is ignored
d) A compilation error occurs

Answer:

c) The write operation is ignored

Explanation:

Attempting to write to a file opened in read-only mode will not succeed, and the operation will be ignored without changing the file.

19. What is the return type of the getc() function in C?

a) char
b) int
c) FILE*
d) char*

Answer:

b) int

Explanation:

The getc() function returns the character read as an unsigned char cast to an int or EOF on end of file or error.

20. Which function in C can be used to rename a file?

a) rename()
b) renameFile()
c) move()
d) changeName()

Answer:

a) rename()

Explanation:

The rename() function is used to change the name of a file. It takes two arguments: the current filename and the new filename.

Comments