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
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.
Differences between Flowcharts, Algorithms, and Pseudocode
Flowchart
- Visual representation: Uses symbols and connecting lines to depict the steps and flow of a process or algorithm.
- Easy to understand: Visual nature makes it accessible even for those without programming knowledge.
- Good for communication: Effective for explaining processes to non-technical audiences and collaborating within teams.
- Can be cumbersome for complex algorithms: May become unwieldy for large or intricate logic.
Algorithm
- Step-by-step procedure: A precise, ordered set of instructions to solve a problem or achieve a task.
- Foundation for problem-solving: Forms the basis for both flowcharts and pseudocode.
- Can be expressed in various ways: Can be written in natural language, mathematical notation, or a combination of both.
- Not necessarily executable: May require further translation into a programming language for implementation.
Pseudocode
- Structured English-like language: Uses a combination of natural language and programming constructs to describe algorithms.
- Close to actual code: Resembles real programming languages, making it easier to translate into code.
- Good for detailed planning: Helps refine algorithms before coding, catching potential errors early.
- Not standardized: Lacks strict syntax rules, allowing flexibility but potentially leading to inconsistencies.
Key Differences Summary
Best Practices
- Use flowcharts to visualize the overall flow of a process, especially for communication and collaboration.
- Use algorithms to clearly define the problem-solving steps in a logical and concise manner.
- Use pseudocode to refine algorithms and make them more concrete before actual coding, ensuring clarity and accuracy.
Consider using a combination of these tools for optimal results, depending on the specific task and audience.