File Handling in C Programming MCQ Questions and Answers
1. Which header file is required for file handling in C?
A) <stdio.h>
B) <conio.h>
C) <stdlib.h>
D) <string.h>
Answer: A
2. Which of the following modes opens a file for reading only?
A) “w”
B) “r”
C) “a”
D) “rw”
Answer: B
3. What is the return type of fopen() function?
A) int
B) FILE*
C) char*
D) void*
Answer: B
4. Which function is used to close a file in C?
A) close()
B) fclose()
C) fileclose()
D) endfile()
Answer: B
5. Which mode is used to open a file for both reading and writing?
A) “r+”
B) “w”
C) “a”
D) “r”
Answer: A
6. What happens if we try to open a non-existing file in “r” mode?
A) File is created
B) File is truncated
C) Error occurs
D) File is appended
Answer: C
7. Which function reads a single character from a file?
A) fgetc()
B) fputc()
C) getc()
D) putc()
Answer: A
8. Which function writes a single character to a file?
A) fgetc()
B) fputc()
C) getc()
D) getchar()
Answer: B
9. Which function is used to read a string from a file?
A) fscanf()
B) fgets()
C) scanf()
D) gets()
Answer: B
10. Which function writes a string to a file?
A) fprintf()
B) fputs()
C) puts()
D) fscanf()
Answer: B
11. What does feof() function check?
A) End of file
B) File opening error
C) File closing error
D) File pointer null
Answer: A
12. Which function returns the current position of the file pointer?
A) ftell()
B) fseek()
C) rewind()
D) fpos()
Answer: A
13. How can we move the file pointer to a specific location?
A) ftell()
B) fseek()
C) fclose()
D) rewind()
Answer: B
14. Which function sets the file pointer to the beginning of the file?
A) fseek(fp, 0, SEEK_END)
B) fseek(fp, 0, SEEK_CUR)
C) rewind(fp)
D) ftell(fp)
Answer: C
15. Which constant represents the beginning of the file in fseek()?
A) SEEK_CUR
B) SEEK_END
C) SEEK_SET
D) START_FILE
Answer: C
16. Which constant represents the current position of the file pointer in fseek()?
A) SEEK_CUR
B) SEEK_SET
C) SEEK_END
D) CURRENT_POS
Answer: A
17. Which constant represents the end of the file in fseek()?
A) SEEK_CUR
B) SEEK_END
C) SEEK_SET
D) EOF
Answer: B
18. What is the correct syntax to open a file in append mode?
A) fopen(“file.txt”,”a”)
B) fopen(“file.txt”,”w”)
C) fopen(“file.txt”,”r”)
D) fopen(“file.txt”,”r+”)
Answer: A
19. Which function reads formatted input from a file?
A) fscanf()
B) scanf()
C) fgets()
D) fgetc()
Answer: A
20. Which function writes formatted output to a file?
A) fprintf()
B) printf()
C) fputs()
D) fputc()
Answer: A
21. If we open a file in “w” mode and the file already exists, what happens?
A) File content remains unchanged
B) File content is deleted
C) File is appended
D) File pointer moves to end
Answer: B
22. If we open a file in “a” mode and the file already exists, what happens?
A) File is truncated
B) File content remains and pointer moves to end
C) Error occurs
D) File pointer moves to beginning
Answer: B
23. Which of the following is NOT a valid file mode in C?
A) “r”
B) “w”
C) “rw”
D) “a+”
Answer: C
24. What is the correct way to declare a file pointer?
A) FILE fp;
B) FILE *fp;
C) file *fp;
D) file fp;
Answer: B
25. Which function writes a block of data to a file?
A) fread()
B) fwrite()
C) fgets()
D) fputs()
Answer: B
26. Which function reads a block of data from a file?
A) fwrite()
B) fputs()
C) fread()
D) fscanf()
Answer: C
27. What is the prototype of fwrite() function?
A) size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
B) int fwrite(const void *ptr, FILE *stream);
C) void fwrite(FILE *fp);
D) size_t fwrite(FILE *fp, char *ptr);
Answer: A
28. What is the prototype of fread() function?
A) size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
B) int fread(FILE *fp);
C) void fread(FILE *fp, char *ptr);
D) size_t fread(FILE *fp, char *ptr);
Answer: A
29. Which function is used to remove a file in C?
A) delete()
B) remove()
C) unlink()
D) erase()
Answer: B
30. Which function renames a file in C?
A) rename()
B) reopen()
C) renamefile()
D) change()
Answer: A
31. Which of the following is true about binary files in C?
A) Only contain text
B) Contain data in machine-readable form
C) Cannot be opened in “rb” mode
D) Cannot be written to
Answer: B
32. Which mode is used to open a binary file for reading?
A) “r”
B) “rb”
C) “wb”
D) “a”
Answer: B
33. Which mode is used to open a binary file for writing?
A) “r”
B) “w”
C) “wb”
D) “a”
Answer: C
34. Which mode is used to append data to a binary file?
A) “ab”
B) “wb”
C) “rb”
D) “a”
Answer: A
35. What is the return value of fclose() if the file is closed successfully?
A) 0
B) 1
C) -1
D) EOF
Answer: A
36. Which of the following is correct about getc() function?
A) Reads one character from file
B) Writes one character to file
C) Reads formatted input
D) Writes formatted output
Answer: A
37. Which of the following is correct about putc() function?
A) Reads one character from file
B) Writes one character to file
C) Reads formatted input
D) Writes formatted output
Answer: B
38. What does rewind(fp) do?
A) Closes the file
B) Sets file pointer to the start of the file
C) Moves file pointer to end
D) Deletes file
Answer: B
39. What is the default file pointer position after opening a file in “w” mode?
A) Beginning of file
B) End of file
C) Middle of file
D) Undefined
Answer: A
40. Which of the following functions is used to get a line from a file?
A) fgets()
B) fscanf()
C) fgetc()
D) fread()
Answer: A
41. Which function can check if the end of a file has been reached?
A) ferror()
B) feof()
C) fclose()
D) fseek()
Answer: B
42. What does the ferror() function check?
A) End of file
B) File read/write error
C) File pointer position
D) File name validity
Answer: B
43. Which mode opens a file for both reading and writing but does not truncate the file?
A) “w+”
B) “r+”
C) “a”
D) “r”
Answer: B
44. Which mode opens a file for reading and writing and truncates it if it exists?
A) “w+”
B) “r+”
C) “a+”
D) “r”
Answer: A
45. Which function moves the file pointer relative to the current position?
A) ftell()
B) fseek(fp, offset, SEEK_CUR)
C) rewind()
D) fclose()
Answer: B
46. What is the purpose of “a+” mode?
A) Read only
B) Append and read
C) Write only
D) Read and overwrite
Answer: B
47. Which function can read multiple elements from a binary file?
A) fread()
B) fwrite()
C) fgetc()
D) fputc()
Answer: A
48. Which function can write multiple elements to a binary file?
A) fread()
B) fwrite()
C) fgets()
D) fputs()
Answer: B
49. What happens when fopen() fails to open a file?
A) Returns NULL
B) Returns 0
C) Creates a new file
D) Exits the program
Answer: A
50. Which function can be used to get a character from the standard input?
A) fgetc(stdin)
B) fputc(stdin)
C) getc(fp)
D) putc(fp)
Answer: A
51. What is the difference between getc() and fgetc()?
A) getc() is a macro, fgetc() is a function
B) fgetc() is a macro, getc() is a function
C) Both are functions
D) Both are macros
Answer: A
52. Which function can write formatted output to the standard output and file simultaneously?
A) fprintf()
B) printf()
C) sprintf()
D) snprintf()
Answer: A
53. Which function can read formatted input from the standard input and a file?
A) scanf()
B) fscanf()
C) sscanf()
D) gets()
Answer: B
54. Which of the following correctly opens a file in binary read/write mode without truncating?
A) “rb+”
B) “wb”
C) “ab”
D) “r+”
Answer: A
55. What happens if you write to a file opened in “r” mode?
A) Writes successfully
B) Causes runtime error
C) Truncates the file
D) Appends to file
Answer: B
56. Which function writes a character array to a file?
A) fputc()
B) fputs()
C) fprintf()
D) fread()
Answer: B
57. Which function reads a character array from a file?
A) fgets()
B) fputc()
C) fprintf()
D) fwrite()
Answer: A
58. Which of the following is used to remove a file in C?
A) unlink()
B) remove()
C) deletefile()
D) closefile()
Answer: B
59. Which function can rename a file?
A) rename()
B) reopen()
C) frename()
D) changefile()
Answer: A
60. Which of the following is true about text files?
A) Stored in human-readable format
B) Stored in machine-readable format
C) Cannot be opened in “r” mode
D) Cannot be appended
Answer: A
61. Which function returns the number of items successfully read by fread()?
A) fread() itself
B) ftell()
C) fseek()
D) feof()
Answer: A
62. Which function returns the number of items successfully written by fwrite()?
A) fwrite() itself
B) fseek()
C) ftell()
D) feof()
Answer: A
63. Which function sets error indicators for a file?
A) clearerr()
B) ferror()
C) feof()
D) fclose()
Answer: A
64. Which function clears both error and EOF indicators for a file?
A) clearerr()
B) fclose()
C) feof()
D) ferror()
Answer: A
65. Which of the following is used to read a formatted string from a file?
A) fscanf()
B) fputs()
C) fgetc()
D) fread()
Answer: A
66. Which of the following is used to write a formatted string to a file?
A) fprintf()
B) fgets()
C) fread()
D) fgetc()
Answer: A
67. What happens if fclose() is not called?
A) Data may not be flushed to file
B) File is automatically deleted
C) Program crashes
D) File pointer resets
Answer: A
68. Which function flushes the output buffer to a file?
A) fflush()
B) fclose()
C) fseek()
D) rewind()
Answer: A
69. Which of the following opens a file for appending in binary mode?
A) “ab”
B) “wb”
C) “rb”
D) “a+”
Answer: A
70. What is the maximum number of files that can be opened simultaneously in C?
A) 1
B) System dependent
C) 10
D) 100
Answer: B
71. Which function reads characters until a newline or EOF is encountered?
A) fgets()
B) fgetc()
C) fscanf()
D) fread()
Answer: A
72. Which function moves the file pointer to the end of the file?
A) fseek(fp, 0, SEEK_END)
B) rewind(fp)
C) ftell(fp)
D) fclose(fp)
Answer: A
73. Which function writes a character to a file and returns the character written?
A) fputc()
B) putc()
C) fprintf()
D) fwrite()
Answer: A
74. Which function reads a character from a file and returns EOF at the end?
A) fgetc()
B) getc()
C) fgets()
D) fscanf()
Answer: A
75. Which function can read data from a string instead of a file?
A) sscanf()
B) scanf()
C) fscanf()
D) fgets()
Answer: A
76. Which function can write data to a string instead of a file?
A) sprintf()
B) printf()
C) fprintf()
D) fputs()
Answer: A
77. Which of the following indicates end-of-file in C?
A) NULL
B) EOF
C) 0
D) -1
Answer: B
78. Which function returns non-zero if end-of-file is reached?
A) feof()
B) ferror()
C) fclose()
D) rewind()
Answer: A
79. Which function is used to open a file and set the file pointer to the end?
A) “a” or “ab”
B) “w”
C) “r”
D) “r+”
Answer: A
80. Which function writes formatted data to memory instead of a file?
A) sprintf()
B) fprintf()
C) printf()
D) fputs()
Answer: A
81. What is the return type of fseek()?
A) int
B) void
C) long
D) FILE*
Answer: A
82. What is the return value of fseek() if successful?
A) 0
B) 1
C) EOF
D) -1
Answer: A
83. Which function can be used to copy one file to another in C?
A) Using fread() and fwrite()
B) Using fgetc() and fputc()
C) Both A and B
D) None of the above
Answer: C
84. Which function returns the position of the file pointer?
A) ftell()
B) fseek()
C) rewind()
D) fclose()
Answer: A
85. Which function sets a file pointer to a specific position?
A) fseek()
B) ftell()
C) rewind()
D) fclose()
Answer: A
86. Which function returns non-zero if an error occurs while file operations?
A) ferror()
B) feof()
C) clearerr()
D) fclose()
Answer: A
87. What is the default file pointer position when opening in “r+” mode?
A) Beginning of file
B) End of file
C) Middle of file
D) Undefined
Answer: A
88. Which function reads data until whitespace is encountered?
A) fscanf()
B) fgets()
C) fread()
D) fgetc()
Answer: A
89. Which of the following is used to read a fixed number of bytes from a file?
A) fread()
B) fgets()
C) fscanf()
D) fgetc()
Answer: A
90. Which of the following is used to write a fixed number of bytes to a file?
A) fwrite()
B) fputs()
C) fprintf()
D) fputc()
Answer: A
91. Which function can determine the size of a file?
A) fseek() + ftell()
B) fread()
C) fwrite()
D) fputs()
Answer: A
92. Which function is used to write a single character to a text file?
A) fputc()
B) fgetc()
C) fread()
D) fwrite()
Answer: A
93. Which function is used to read a single character from a text file?
A) fgetc()
B) fputc()
C) fwrite()
D) fread()
Answer: A
94. Which function writes a string to a file without adding newline?
A) fputs()
B) puts()
C) fprintf()
D) fwrite()
Answer: A
95. Which function reads a string from a file and stops at newline?
A) fgets()
B) fscanf()
C) fgetc()
D) fread()
Answer: A
96. Which of the following can open a text file for reading and writing from the end?
A) “a+”
B) “r+”
C) “w+”
D) “r”
Answer: A
97. Which of the following is true about binary file reading?
A) Data is read exactly as stored
B) Data is converted to text
C) Data is truncated
D) Data is ignored
Answer: A
98. Which of the following is true about binary file writing?
A) Data is stored exactly as given
B) Data is converted to text
C) Data is compressed
D) Data is ignored
Answer: A
99. Which function sets file pointer to the beginning and clears error indicators?
A) rewind()
B) fseek(fp, 0, SEEK_SET)
C) clearerr()
D) Both A and C
Answer: D
100. Which of the following is correct to append to a binary file?
A) fopen(“file.bin”,”ab”)
B) fopen(“file.bin”,”rb”)
C) fopen(“file.bin”,”wb”)
D) fopen(“file.bin”,”r”)
Answer: A
