250+ TOP MCQs on File Operations and Answers

C MCQs on “File Operations – 1”.

1. Which one of the following is correct syntax for opening a file.
a) FILE *fopen(const *filename, const char *mode)
b) FILE *fopen(const *filename)
c) FILE *open(const *filename, const char *mode)
d) FILE open(const*filename)
Answer: a
Clarification: fopen() opens the named file, and returns a stream, or NULL of the attempt fails.

2. What is the function of the mode ‘ w+’?
a) create text file for writing, discard previous contents if any
b) create text file for update, discard previous contents if any
c) create text file for writing, do not discard previous contents if any
d) create text file for update, do not discard previous contents if any
Answer: b
Clarification: w+ is a mode used to open a text file for update (i. e., writing and reading), discard previous contents if any.

3. If the mode includes b after the initial letter, what does it indicates?
a) text file
b) big text file
c) binary file
d) blueprint text
Answer: c
Clarification: If the mode consists of letter b after the first letter as in, “rb” or “w+b”, it indicates binary file.

4. fflush(NULL) flushes all ____________
a) input streams
b) output streams
c) previous contents
d) appended text
Answer: b
Clarification: fflush(FILE *stream) – fflush() causes any buffered but unwritten to be written on an Output stream. On an input stream, the effect is undefined. fflush(NULL) flushes all output streams.

5. _____removes the named file, so that a subsequent attempt to open it will fail.
a) remove(const *filename)
b) remove(filename)
c) remove()
d) fclose(filename)
Answer: a
Clarification: remove(const *filename) removes the named file, so that a subsequent attempt to open it will fail. It returns non-zero of the attempt fails.

6. What is the function of FILE *tmpfile(void)?
a) creates a temporary file of mode “wb+”
b) creates a temporary file of mode “wb”
c) creates a temporary file of mode ” w”
d) creates a temporary file of mode “w+”
Answer: a
Clarification: A temporary file is created by tmpfile() function of mode “wb+” that will be automatically removed when closed or when the program terminates normally.

7. What does tmpfile() returns when it could not create the file?
a) stream and NULL
b) only stream
c) only NULL
d) does not return anything
Answer: a
Clarification: tmpfile() returns a stream or NULL if it could not create the file.

8. Choose the right statement for fscanf() and scanf()
a) fscanf() can read from standard input whereas scanf() specifies a stream from which to read
b) fscanf() can specifies a stream from which to read whereas scanf() can read only from standard input
c) fscanf() and scanf() has no difference in their functions
d) fscanf() and scanf() can read from specified stream
Answer: b
Clarification: The fscanf() is similar to the scanf() function, except that the first argument of fscanf() specifies a stream from which to read whereas scanf() can read from standard input.

9. EOF is an integer type defined in stdio. hand has a value ____________
a) 1
b) 0
c) NULL
d) – 1
Answer: d
Clarification: EOF is an integer type defined in stdio. hand has a value – 1.

10. fwrite() can be used only with files that are opened in binary mode.
a) true
b) false
Answer: a
Clarification: fwrite() can be used to write characters, integers, or structures to a file. However, fwrite() can be used only with files opened in binary mode.

Leave a Reply

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