5.4 - File Handling.


Back Button Home

5.4.1 - Overview of File Handling.


1. Which function is used to open a file in C?
  • (A) fopen()
  • (B) fread()
  • (C) fclose()
  • (D) fseek()
Correct Answer: (A) fopen()
2. What is the return type of fopen() function?
  • (A) int
  • (B) FILE*
  • (C) char*
  • (D) void*
Correct Answer: (B) FILE*
3. Which mode in fopen() is used to open a file for writing at the end of the file?
  • (A) "r"
  • (B) "w"
  • (C) "a"
  • (D) "rw"
Correct Answer: (C) "a"
4. Which function is used to read a single character from a file?
  • (A) getc()
  • (B) putc()
  • (C) fread()
  • (D) fputc()
Correct Answer: (A) getc()
5. What does the fclose() function do in C?
  • (A) Open a file
  • (B) Close a file
  • (C) Read from a file
  • (D) Write to a file
Correct Answer: (B) Close a file
6. What is the correct syntax for closing a file in C?
  • (A) fclose(file);
  • (B) closefile(file);
  • (C) endfile(file);
  • (D) stopfile(file);
Correct Answer: (A) fclose(file);
7. What is the function used to get the current position of the file pointer?
  • (A) fseek()
  • (B) ftell()
  • (C) rewind()
  • (D) fgetpos()
Correct Answer: (B) ftell()
8. Which function is used to write formatted data into a file?
  • (A) fprintf()
  • (B) fscanf()
  • (C) fwrite()
  • (D) fread()
Correct Answer: (A) fprintf()
9. What happens if a file opened in "r" mode does not exist?
  • (A) A new file is created
  • (B) fopen() returns NULL
  • (C) fopen() returns 0
  • (D) The program crashes
Correct Answer: (B) fopen() returns NULL
10. Which function is used to set the position of the file pointer in C?
  • (A) fset()
  • (B) fseek()
  • (C) fmove()
  • (D) fchange()
Correct Answer: (B) fseek()
11. How do you write an integer into a file?
  • (A) fwrite()
  • (B) putw()
  • (C) putc()
  • (D) fprintf()
Correct Answer: (B) putw()
12. Which function is used to read a string from a file in C?
  • (A) gets()
  • (B) fputs()
  • (C) fgets()
  • (D) fscanf()
Correct Answer: (C) fgets()
13. What is the file pointer variable type in C?
  • (A) FILE*
  • (B) int*
  • (C) char*
  • (D) float*
Correct Answer: (A) FILE*
14. What does the rewind() function do in file handling?
  • (A) Moves the file pointer to the end of the file
  • (B) Reverses the contents of the file
  • (C) Moves the file pointer to the beginning of the file
  • (D) Closes the file
Correct Answer: (C) Moves the file pointer to the beginning of the file
15. What is the default mode when a file is opened using fopen()?
  • (A) "r"
  • (B) "w"
  • (C) "a"
  • (D) "rw"
Correct Answer: (A) "r"
16. What is the purpose of the ftell() function in C?
  • (A) To move the file pointer
  • (B) To get the current file position
  • (C) To rewind the file pointer
  • (D) To close the file
Correct Answer: (B) To get the current file position
17. Which function is used to write a character to a file?
  • (A) putc()
  • (B) fwrite()
  • (C) fputc()
  • (D) fprintf()
Correct Answer: (C) fputc()
18. How do you read an integer from a file?
  • (A) getc()
  • (B) fread()
  • (C) getw()
  • (D) fscanf()
Correct Answer: (C) getw()
19. What does the function fseek() do in file handling?
  • (A) Opens a file
  • (B) Reads a file
  • (C) Sets the file pointer to a specific location
  • (D) Closes a file
Correct Answer: (C) Sets the file pointer to a specific location
20. What is the result of opening a file in "w" mode if the file already exists?
  • (A) The file is appended
  • (B) The file is truncated
  • (C) The file is read
  • (D) The file is deleted
Correct Answer: (B) The file is truncated

5.4.2 - File Management I/O Functions.


21. Which function is used to open a file in C?
  • (A) fopen()
  • (B) fget()
  • (C) fset()
  • (D) getopen()
Correct Answer: (A) fopen()
22. What does the 'r' mode in fopen() signify?
  • (A) Read-only mode
  • (B) Write-only mode
  • (C) Append mode
  • (D) Create a new file
Correct Answer: (A) Read-only mode
23. Which function is used to write data to a file in C?
  • (A) fscanf()
  • (B) fwrite()
  • (C) fclose()
  • (D) fputc()
Correct Answer: (B) fwrite()
24. What does the fclose() function do in C?
  • (A) Closes a file
  • (B) Opens a file
  • (C) Reads a file
  • (D) Deletes a file
Correct Answer: (A) Closes a file
25. Which of the following modes does not create a new file if it doesn’t exist?
  • (A) "w"
  • (B) "a"
  • (C) "r"
  • (D) "w+"
Correct Answer: (C) "r"
26. Which function reads a single character from a file?
  • (A) fputc()
  • (B) fgetc()
  • (C) fputs()
  • (D) fprintf()
Correct Answer: (B) fgetc()
27. Which function writes a string to a file in C?
  • (A) fprintf()
  • (B) fscanf()
  • (C) fputs()
  • (D) fwrite()
Correct Answer: (C) fputs()
28. What is the return type of the fopen() function?
  • (A) int
  • (B) FILE*
  • (C) char*
  • (D) void
Correct Answer: (B) FILE*
29. Which function is used to set the position to a specified location in a file?
  • (A) ftell()
  • (B) fseek()
  • (C) fwrite()
  • (D) fread()
Correct Answer: (B) fseek()
30. Which function is used to get the current position in a file?
  • (A) fwrite()
  • (B) fseek()
  • (C) ftell()
  • (D) fputc()
Correct Answer: (C) ftell()
31. What does the 'a' mode in fopen() signify?
  • (A) Read-only mode
  • (B) Write-only mode
  • (C) Append mode
  • (D) Create a new file
Correct Answer: (C) Append mode
32. Which function can be used to read formatted data from a file?
  • (A) fprintf()
  • (B) fscanf()
  • (C) fputs()
  • (D) fread()
Correct Answer: (B) fscanf()
33. What is the correct way to close a file in C?
  • (A) fclose(fp);
  • (B) close(fp);
  • (C) delete(fp);
  • (D) end(fp);
Correct Answer: (A) fclose(fp);
34. Which function can be used to write formatted data into a file?
  • (A) fscanf()
  • (B) fprintf()
  • (C) fread()
  • (D) fclose()
Correct Answer: (B) fprintf()
35. In C, what is a file pointer?
  • (A) A pointer to a structure FILE
  • (B) A pointer to the first line in the file
  • (C) A pointer to the memory of a file
  • (D) A pointer to the last line in the file
Correct Answer: (A) A pointer to a structure FILE
36. Which of the following is not a valid file operation mode?
  • (A) "w+"
  • (B) "r+"
  • (C) "r++"
  • (D) "a+"
Correct Answer: (C) "r++"
37. Which function is used to write integer data into a file?
  • (A) putc()
  • (B) putw()
  • (C) fputs()
  • (D) fwrite()
Correct Answer: (B) putw()
38. How do you check if fopen() failed to open a file?
  • (A) By checking if the pointer returned is NULL
  • (B) By checking if the file size is zero
  • (C) By checking if the file name is empty
  • (D) By checking the file mode
Correct Answer: (A) By checking if the pointer returned is NULL
39. Which function reads a string from a file?
  • (A) fscanf()
  • (B) fgets()
  • (C) fread()
  • (D) fgetc()
Correct Answer: (B) fgets()
40. What will fopen() return if it fails to open a file?
  • (A) -1
  • (B) NULL
  • (C) 0
  • (D) EOF
Correct Answer: (B) NULL

5.4.3 - Working with Text Files and Binary Files.


41. Which function is used to open a text or binary file in C?
  • (A) fopen()
  • (B) fread()
  • (C) fseek()
  • (D) fputs()
Correct Answer: (A) fopen()
42. Which mode is used to open a binary file for reading?
  • (A) "rb"
  • (B) "wb"
  • (C) "ab"
  • (D) "r"
Correct Answer: (A) "rb"
43. What is the extension used for text files?
  • (A) .bin
  • (B) .text
  • (C) .dat
  • (D) .txt
Correct Answer: (D) .txt
44. Which of the following is used to open a file in binary mode?
  • (A) "w"
  • (B) "b"
  • (C) "wb"
  • (D) "a"
Correct Answer: (C) "wb"
45. What does the fwrite() function do in C?
  • (A) Write data into a text file
  • (B) Write data into a binary file
  • (C) Write data into a console
  • (D) Write data into a string
Correct Answer: (B) Write data into a binary file
46. Which function is used to read from a binary file in C?
  • (A) fread()
  • (B) fprintf()
  • (C) fseek()
  • (D) fputs()
Correct Answer: (A) fread()
47. How is data stored in a binary file?
  • (A) In ASCII format
  • (B) In human-readable format
  • (C) In binary format (0s and 1s)
  • (D) In hexadecimal format
Correct Answer: (C) In binary format (0s and 1s)
48. What is the primary difference between text files and binary files?
  • (A) Text files store data as binary while binary files store data as text
  • (B) Text files store data in ASCII format while binary files store data in binary format
  • (C) Text files are only for integers while binary files are for floating points
  • (D) Text files can’t be read by humans but binary files can
Correct Answer: (B) Text files store data in ASCII format while binary files store data in binary format
49. Which of the following modes can be used to append data to a binary file?
  • (A) "rb"
  • (B) "wb"
  • (C) "ab"
  • (D) "r+"
Correct Answer: (C) "ab"
50. What is the return value of fread() on successful reading?
  • (A) The number of bytes read
  • (B) The number of elements read
  • (C) 0
  • (D) NULL
Correct Answer: (B) The number of elements read
51. Which function can write data into a binary file?
  • (A) fputc()
  • (B) fwrite()
  • (C) fprintf()
  • (D) fputs()
Correct Answer: (B) fwrite()
52. What happens if you try to open a binary file in "rb" mode and the file does not exist?
  • (A) A new file is created
  • (B) fopen() returns NULL
  • (C) The program exits
  • (D) An error message is printed
Correct Answer: (B) fopen() returns NULL
53. Which function is used to move the file pointer to a specific position in a file?
  • (A) ftell()
  • (B) fwrite()
  • (C) fseek()
  • (D) fgetpos()
Correct Answer: (C) fseek()
54. What is the advantage of using binary files over text files?
  • (A) Binary files are easier to edit manually
  • (B) Binary files are platform-independent
  • (C) Binary files require less storage and are faster for numerical data
  • (D) Binary files support more data types than text files
Correct Answer: (C) Binary files require less storage and are faster for numerical data
55. Which mode is used to open a binary file for both reading and writing?
  • (A) "r+b"
  • (B) "wb"
  • (C) "ab"
  • (D) "w+b"
Correct Answer: (A) "r+b"
56. How is end-of-file detected when working with text files?
  • (A) By checking if fread() returns -1
  • (B) By checking if fgetc() returns EOF
  • (C) By checking if fwrite() returns 0
  • (D) By checking if the file pointer is NULL
Correct Answer: (B) By checking if fgetc() returns EOF
57. In which of the following is data stored in human-readable format?
  • (A) Text files
  • (B) Binary files
  • (C) Both A and B
  • (D) Neither A nor B
Correct Answer: (A) Text files
58. What does fseek() return if it successfully moves the file pointer?
  • (A) -1
  • (B) NULL
  • (C) 0
  • (D) 1
Correct Answer: (C) 0
59. What happens when fwrite() fails to write to a file?
  • (A) The program crashes
  • (B) It returns a value less than the number of items to be written
  • (C) It writes random data to the file
  • (D) It automatically retries writing
Correct Answer: (B) It returns a value less than the number of items to be written
60. Which of the following file modes is used to overwrite an existing text file?
  • (A) "r"
  • (B) "w"
  • (C) "a"
  • (D) "r+"
Correct Answer: (B) "w"