C Multiple Choice Questions & Answers on “File Operations – 2”.
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
Answer: d
Clarification: 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?
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
Answer: a
Clarification: gets() reads the next input line into the array s, terminating newline is replaced with ‘ ’.It returns s, or NULL if end of file or error occurs.
3. Which function will return the current file position for stream?
a) fgetpos()
b) fseek()
c) ftell()
d) fsetpos()
Answer: c
Clarification: 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.
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
Answer: a
Clarification:fgetpos() records the current position in stream in *s, for subsequent use by fsetpos() . The type fpost_t is suitable for recording such values.
5. Which functions is declared in
a) fseek()
b) ftell()
c) ferror()
d) fsetpos()
Answer: c
Clarification: ferror() is declared under
6. setvbuf() and setbuf() function controls buffering for the stream.
a) true
b) false
Answer: a
Clarification: 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
Answer: b
Clarification: 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()
Answer: b
Clarification: 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
Answer: a
Clarification: 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()
Answer: c
Clarification: getc() is equivalent to fgetc() except that if it is a macro, it may evaluate more than once.