C Questions and Answers – File Operations – 1

«
»

This set of C MCQs focuses 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)
View Answer

Answer: a
Explanation: fopen() opens the named file, and returns a stream, or NULL of the attempt fails.
advertisement

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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: c
Explanation: If the mode consists of letter b after the first letter as in, “rb” or “w+b”, it indicates binary file.
advertisement
advertisement

4. fflush(NULL) flushes all ____________
a) input streams
b) output streams
c) previous contents
d) appended text
View Answer

Answer: b
Explanation: 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)
View Answer

Answer: a
Explanation: 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.
advertisement

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+”
View Answer

Answer: a
Explanation: 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
View Answer

Answer: a
Explanation: tmpfile() returns a stream or NULL if it could not create the file.
advertisement

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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: d
Explanation: EOF is an integer type defined in stdio. hand has a value – 1.
advertisement

10. fwrite() can be used only with files that are opened in binary mode.
a) true
b) false
View Answer

Answer: a
Explanation: 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.

Sanfoundry Global Education & Learning Series – C Programming Language.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

advertisement
advertisement

Leave a Comment

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter