Friday, February 9, 2024

C File Management MCQs Set 2

C File Management MCQs Set 2

1. What is the correct way to check if a file was opened successfully after using fopen()?
(a) if (fopen(file_name, "mode") == NULL) { ... }
(b) if (file_pointer != NULL) { ... }
(c) if (errno == 0) { ... }
(d) None of the above

2. Which function closes a file in C?
(a) fclose(file_name)
(b) close(file_pointer)
(c) close(file_name)
(d) Both (a) and (b)

3. Which function reads a single character from a file?
(a) getchar()
(b) getc(file_pointer)
(c) fscanf(file_pointer, "%c")
(d) All of the above

4. How can you determine the current position (read/write pointer) within a file?
(a) ftell(file_pointer)
(b) fseek(file_pointer, 0, SEEK_CUR)
(c) fgetc(file_pointer)
(d) Both (a) and (b)

5. Which function closes an open file?
(a) close(file)
(b) fclose(file)
(c) terminate(file)
(d) destroy(file)

6. What does the EOF macro represent?
(a) End of File
(b) End of Stream
(c) End of Text
(d) End of Function

7. How do you check if a file has been opened successfully?
(a) if (file != NULL)
(b) if (file != EOF)
(c) if (file > 0)
(d) if (file < 0)

>>> Here is your answers


C File Management MCQs Set 2 - Answers

C File Management MCQs Set 2 - Answers

1. What is the correct way to check if a file was opened successfully after using fopen()?
(a) if (fopen(file_name, "mode") == NULL) { ... }
(b) if (file_pointer != NULL) { ... }
(c) if (errno == 0) { ... }
(d) None of the above

Answer: (b) if (file_pointer != NULL) { ... }

Explanation:
fopen() returns a FILE pointer, which is NULL on failure.

2. Which function closes a file in C?
(a) fclose(file_name)
(b) close(file_pointer)
(c) close(file_name)
(d) Both (a) and (b)

Answer: (d) Both (a) and (b)

Explanation:
Both fclose(file_pointer) and close(file_pointer) are valid.

3. Which function reads a single character from a file?
(a) getchar()
(b) getc(file_pointer)
(c) fscanf(file_pointer, "%c")
(d) All of the above

Answer: (b) getc(file_pointer)

Explanation:
getchar() reads from standard input, not files.
fscanf() is usually used for formatted input.

4. How can you determine the current position (read/write pointer) within a file?
(a) ftell(file_pointer)
(b) fseek(file_pointer, 0, SEEK_CUR)
(c) fgetc(file_pointer)
(d) Both (a) and (b)

Answer: (d) Both (a) and (b)

Explanation:
ftell(file_pointer) returns the current file position.
fseek(file_pointer, 0, SEEK_CUR) also retrieves the current position.

5. Which function closes an open file?
(a) close(file)
(b) fclose(file)
(c) terminate(file)
(d) destroy(file)

Answer: (b) fclose(file)

Explanation:
fclose(file) is the standard C library function to close an open file.
Failing to close a file can lead to memory leaks and data corruption.

6. What does the EOF macro represent?
(a) End of File
(b) End of Stream
(c) End of Text
(d) End of Function

Answer: (a) End of File

Explanation:
EOF is a macro defined in the stdio.h header, indicating the end of a file reached during reading.

7. How do you check if a file has been opened successfully?
(a) if (file != NULL)
(b) if (file != EOF)
(c) if (file > 0)
(d) if (file < 0)

Answer: (a) if (file != NULL). It is the correct way to check for successful file opening.

Explanation:
fopen() returns a pointer to the opened file stream. If the opening fails, it returns NULL.