C MCQ Questions - File Handling
This section focuses on the "File Handling" in C programming. These C MCQ Questions should be practiced to improve the C programming skills required for various interviews (campus interview, walk-in interview, company interview), placement, entrance exam and other competitive examinations.
1. Which of the following true about FILE *fp
A. FILE is a keyword in C for representing files and fp is a variable of FILE type.
B. FILE is a stream
C. FILE is a buffered stream
D. FILE is a structure and fp is a pointer to the structure of FILE type
View Answer
Ans : D
Explanation: fp is a pointer of FILE type and FILE is a structure that store following information about opened file.
2. Which of the following mode argument is used to truncate?
A. a
B. w
C. f
D. t
View Answer
Ans : B
Explanation: None
3. The first and second arguments of fopen() are
A. A character string containing the name of the file & the second argument is the mode
B. A character string containing the name of the user & the second argument is the mode
C. A character string containing file pointer & the second argument is the mode
D. None of the mentioned
View Answer
Ans : A
Explanation: None.
4. FILE is of type ______
A. int type
B. char * type
C. struct type
D. None of the mentioned
View Answer
Ans : C
Explanation: It is what is typically termed an opaque data type, meaning it's typically declared as a simple structure, and then internally in the OS libraries the FILE pointer is cast to the actual date-type of the data-structure that the OS will use access data from a file. A lot of these details are system-specific though, so depending on the OS, the definition may differ.
5. fseek() should be preferred over rewind() mainly because
A. rewind() doesn't work for empty files
B. rewind() may fail for large files
C. In rewind, there is no way to check if the operations completed successfully
D. All of the above
View Answer
Ans : C
Explanation: The rewind function sets the file position indicator for the stream pointed to by stream to the beginning of the file.
6. FILE reserved word is?
A. A structure tag declared in stdio.h
B. One of the basic datatypes in c
C. Pointer to the structure defined in stdio.h
D. It is a type name defined in stdio.h
View Answer
Ans : D
Explanation: It is a type name defined in stdio.h
7. For binary files, a ___ must be appended to the mode string.
A. "b"
B. "B"
C. "binary"
D. "01"
View Answer
Ans : A
Explanation: None
8. Which of the following statements about stdout and stderr are true?
A. Same
B. Both connected to screen always.
C. Both connected to screen by default.
D. stdout is line buffered but stderr is unbuffered.
View Answer
Ans : C
Explanation: None
9. Which type of files can't be opened using fopen()?
A. .txt
B. .bin
C. .c
D. None of the above
View Answer
Ans : D
Explanation: None
10. When a C program is started, O.S environment is responsible for opening file and providing pointer for that file?
A. Standard input
B. Standard output
C. Standard error
D. All of the above
View Answer
Ans : D
Explanation: None
11. If there is any error while opening a file, fopen will return?
A. Nothing
B. EOF
C. NULL
D. Depends on compiler
View Answer
Ans : C
Explanation: None
12. It is not possible to combine two or more file opening mode in open () method.
A. TRUE
B. FALSE
C. May Be
D. Can't Say
View Answer
Ans : B
Explanation: None
13. What is the return value of putchar()?
A. The character written
B. EOF if an error occurs
C. Nothing
D. Both character written & EOF if an error occurs
View Answer
Ans : D
Explanation: None
14. Which is true?
A. The symbolic constant EOF is defined in
B. The value is -1
C. The symbolic constant EOF is defined in & value is -1
D. Only value is -1
View Answer
Ans : C
Explanation: None
15. What is the purpose of "rb" in fopen() function used below in the code?
FILE *fp;
fp = fopen("demo.txt", "rb");
A. Open "demo.txt" in binary mode for reading
B. Create a new file "demo.txt" for reading and writing
C. Open "demo.txt" in binary mode for reading and writing
D. None of the above
View Answer
Ans : A
Explanation: The file demo.txt will be opened in the binary mode.
16. Which files will get closed through the fclose() in the following program?
void main()
{
FILE *fp, *ft;
fp = fopen("a.txt", "r");
ft = fopen("b.txt", "r");
fclose(fp,ft);
}
A. a, b
B. a
C. b
D. Error in fclose
View Answer
Ans : D
Explanation: Error occurs due to extra parameter in call to fclose().
17. When fopen() is not able to open a file, it returns
A. EOF
B. NULL
C. Run-time Error
D. None of the above
View Answer
Ans : B
Explanation: fopen() returns NULL if it is not able to open the file
18. getc() returns EOF when
A. When getc() fail to read the character
B. When end of file is reached
C. Both A and B
D. None of the above
View Answer
Ans : C
Explanation: None
19. What is the output of this program?
#include <stdio.h>
int main(){
FILE *fp;
char *str;
fp=fopen("demo.txt","r");// demo.txt :you are a good programmer
while(fgets(str,6,fp)!=NULL)
puts(str);
fclose(fp);
return 0;
}
A. you are a good programmer
B. e a good programmer
C. you ar
D. you are
View Answer
Ans : B
Explanation: It will print only six five character
20. What is the output of this program?
#include <stdio.h>
int main(){
char c;
FILE *fp;
fp=fopen("demo.txt","r");
while((c=fgetc(fp))!=EOF)
printf("%c",c);
fclose(fp);
return 0;
}
A. It will print the content of file demo.txt
B. It will print the content of file till it encouter new line character
C. Compilation Error
D. None of the above
View Answer
Ans : A
Explanation: None
21. What is the output of this program?
#include <stdio.h>
int main(){
char c;
FILE *fp;
fp=fopen("demo.txt","a+"); // demo.txt : hello you are reading a file
fprintf(fp," demo");
fclose(fp);
fp=fopen("myfile.txt","r");
while((c=fgetc(fp))!=EOF)
printf("%c",c);
fclose(fp);
return 0;
}
A. hello you are reading a file
B. hello you are reading a file demo
C. demo
D. None of the above
View Answer
Ans : B
Explanation: Mode a+ means we can read and write on file but when we will write on file it will append at the end content and it doesn't truncate the content of file.
22. A data of the file is stored in?
A. Ram
B. Hard disk
C. Rom
D. None
View Answer
Ans : B
Explanation: A data of the file is stored in Hard disk.
23. Select a function which is used to write a string to a file?
A. pits()
B. putc()
C. fputs()
D. fgets()
View Answer
Ans : C
Explanation: fputs() is a function which is used to write a string to a file.
24. Select text file in which data is stored in
A. ASC11 code
B. Binary code
C. Octal code
D. text code
View Answer
Ans : A
Explanation: ASC11 code is the text file in which data is stored .
25. We should not read after a write to a file without an intervening call to fflush(), fseek() or rewind()
A. TRUE
B. FALSE
C. May Be
D. Can't Say
View Answer
Ans : A
Explanation: True, we should not be able to read a file after writing in that file without calling the below functions.
26. Offset used in fseek() function call can be a negative number.
A. TRUE
B. FALSE
C. May Be
D. Can't Say
View Answer
Ans : A
Explanation: True, offset in fseek() function can be a negative number. It makes the file pointer to move backwards from the current position
27. Will the following program work?
#include <stdio.h>
int main()
{
int i=3;
printf("i=%*d", i, i);
return 0;
}
A. Yes
B. No
C. May Be
D. Can't Say
View Answer
Ans : A
Explanation: It prints i=3.
28. stderr, stdin, stdout are FILE pointers
A. Yes
B. No
C. May Be
D. Can't Say
View Answer
Ans : A
Explanation: Yes, these will be declared like
The corresponding stdio.h variable is FILE* stdin;
The corresponding stdio.h variable is FILE* stdout;
The corresponding stdio.h variable is FILE* stderr;
29. which Is data type of file pointer is?
A. int
B. double
C. void
D. File
View Answer
Ans : D
Explanation: The data type of file pointer is File.
30. In fopen(), the open mode "wx" is sometimes preferred "w" because.
1) Use of wx is more efficient.
2) If w is used, old contents of file are erased and a new empty file is created. When wx is used, fopen() returns NULL if file already exists.
A. Only 1
B. Only 2
C. Both 1 and 2
D. Neither 1 nor 2
View Answer
Ans : B
Explanation: No Explanation.
31. A file written in text mode can be read back in binary mode.
A. Yes
B. No
C. May Be
D. Can't Say
View Answer
Ans : B
Explanation: we cannot read the correct the data in binary mode.
32. A file written in text mode can be read back in binary mode.
A. Yes
B. No
C. May Be
D. Can't Say
View Answer
Ans : B
Explanation: we cannot read the correct the data in binary mode.
33. A text stream is an ordered sequence of characters composed into lines, each line consisting of zero or more characters plus a terminating new-line character.
A. TRUE
B. FALSE
C. May Be
D. Can't Say
View Answer
Ans : A
Explanation: True, each line may contain zero or more characters terminated by a newline character.
34. While calling the fprintf() function in the format string conversion specifier %s can be used to write a character string in capital letters.
A. TRUE
B. FALSE
C. May Be
D. Can't Say
View Answer
Ans : B
Explanation: The %s format specifier tells the compiler the given input was string of characters.
35. Select text file in which number will take.
A. 2 bytes
B. 4 bytes
C. 3 bytes
D. 8bytes
View Answer
Ans : B
Explanation: 4 bytes number will take
36. Which is true about getc.getc returns?
A. The next character from the stream referred to by file pointer
B. EOF for end of file or error
C. Both a & b
D. Nothing
View Answer
Ans : C
Explanation: True, offset in fseek() function can be a negative number. It makes the file pointer to move backwards from the current position
37. What is the meant by 'a' in the following operation?
fp = fopen("letsfindcourse.txt", "a");
A. Attach
B. Append
C. Apprehend
D. Add
View Answer
Ans : B
Explanation: Append is the meant by 'a' in the following operation.
38. What does the following segment of code do?
fprintf(fp, "Copying!");
A. It writes "Copying!" into the file pointed by fp
B. It reads "Copying!" from the file and prints on display
C. It writes as well as reads "Copying!" to and from the file and prints it
D. None of the mentioned
View Answer
Ans : A
Explanation: No explanation.
39. _____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
Ans : 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.
40. EOF is an integer type defined in stdio. hand has a value ____________
A. 1
B. 0
C. NULL
D. -1
View Answer
Ans : D
Explanation: EOF is an integer type defined in stdio. hand has a value 1.
Also check :
Discussion