C Questions and Answers – File Operations – 2

This set of C Multiple Choice Questions & Answers (MCQs) focuses on “File Operations – 2”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.

1. what is the function of fputs()?
a) read a line from a file
b) read a character from a file
c) write a character to a file
d) write a line to a file
View Answer

Answer: d
Explanation: The fputs() is used to write a line to a file. fputs() syntax can be written as
int fputs(const char *str, FILE *stream);

2. What does the following C code snippet mean?

advertisement
advertisement
char *gets(char *s)

a) reads the next input line into the array s
b) writes the line into the array s
c) reads the next input character into the array s
d) write a character into the array
View Answer

Answer: a
Explanation: gets() reads the next input line into the array s, terminating newline is replaced with ‘\0’.It returns s, or NULL if end of file or error occurs.
Note: Join free Sanfoundry classes at Telegram or Youtube

3. Which function will return the current file position for stream?
a) fgetpos()
b) fseek()
c) ftell()
d) fsetpos()
View Answer

Answer: c
Explanation: The current file position is returned by ftell() function for stream, or -1L on error.

4. Select the right explanation for the following C code snippet.

advertisement
int fgetpos(FILE *stream, fpos_t *s)

a) records the current position in stream in *s
b) sets the file position for stream in *s
c) positions stream at the position recorded in *s
d) reads from stream into the array ptr
View Answer

Answer: a
Explanation:fgetpos() records the current position in stream in *s, for subsequent use by fsetpos() . The type fpost_t is suitable for recording such values.
advertisement

5. Which functions is declared in <errno. h>?
a) fseek()
b) ftell()
c) ferror()
d) fsetpos()
View Answer

Answer: c
Explanation: ferror() is declared under <errno. h>. ferror() returns non-zero if the error indicator for stream is set.

6. setvbuf() and setbuf() function controls buffering for the stream.
a) true
b) false
View Answer

Answer: a
Explanation: setvbuf() and setbuf() controls buffering for the stream. If buff is NULL, buffering is turned off for the stream.

7. The functions vprintf(), vfprintf(), and vsprintf() are not equivalent to the corresponding printf() functions except the variable argument list.
a) true
b) false
View Answer

Answer: b
Explanation: The functions vprintf() , vfprintf() , and vsprintf() are similar to the corresponding printf() functions except that the variable argument list is replaced by arg.

8. The______function reads atmost one less than the number of characters specified by size from the given stream and it is stored in the string str.
a) fget()
b) fgets()
c) fput()
d) fputs()
View Answer

Answer: b
Explanation: The fgets() function reads one less than the number of characters indicated by the size from the given stream and it is stored in the string str. The fgets() terminates as soon as it encounters either a newline character, EOF, or other error.

9. What does the following C code snippet mean?

int ungetc(int c, FILE *stream)

a) pushes c back onto a stream
b) deletes c from the stream
c) reads frequency of c in stream
d) no action is taken by the command
View Answer

Answer: a
Explanation: ungetc() pushes c back onto stream, where it will be returned on the next read. Only one character of pushback per stream is Guaranteed.

10. Choose the correct difference between getc() and fgetc().
a) If it is not a macro, it may evaluate stream more than once
b) if it is amacro, it may not evaluate stream more than once
c) if it is a macro, it may evaluate stream more than once
d) no difference between fgetc() and getc()
View Answer

Answer: c
Explanation: getc() is equivalent to fgetc() except that if it is a macro, it may evaluate more than once.

Sanfoundry Global Education & Learning Series – C Programming Language.

To practice all areas of C language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). 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!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.