5.4 - File Handling.

5.4.1 - Overview of File Handling.


1. Which function is used to open a file in C?
Correct Answer: (A) fopen()
2. What is the return type of fopen() function?
Correct Answer: (B) FILE*
3. Which mode in fopen() is used to open a file for writing at the end of the file?
Correct Answer: (C) "a"
4. Which function is used to read a single character from a file?
Correct Answer: (A) getc()
5. What does the fclose() function do in C?
Correct Answer: (B) Close a file
6. What is the correct syntax for closing a file in C?
Correct Answer: (A) fclose(file);
7. What is the function used to get the current position of the file pointer?
Correct Answer: (B) ftell()
8. Which function is used to write formatted data into a file?
Correct Answer: (A) fprintf()
9. What happens if a file opened in "r" mode does not exist?
Correct Answer: (B) fopen() returns NULL
10. Which function is used to set the position of the file pointer in C?
Correct Answer: (B) fseek()
11. How do you write an integer into a file?
Correct Answer: (B) putw()
12. Which function is used to read a string from a file in C?
Correct Answer: (C) fgets()
13. What is the file pointer variable type in C?
Correct Answer: (A) FILE*
14. What does the rewind() function do in file handling?
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()?
Correct Answer: (A) "r"
16. What is the purpose of the ftell() function in C?
Correct Answer: (B) To get the current file position
17. Which function is used to write a character to a file?
Correct Answer: (C) fputc()
18. How do you read an integer from a file?
Correct Answer: (C) getw()
19. What does the function fseek() do in file handling?
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?
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?
Correct Answer: (A) fopen()
22. What does the 'r' mode in fopen() signify?
Correct Answer: (A) Read-only mode
23. Which function is used to write data to a file in C?
Correct Answer: (B) fwrite()
24. What does the fclose() function do in C?
Correct Answer: (A) Closes a file
25. Which of the following modes does not create a new file if it doesn’t exist?
Correct Answer: (C) "r"
26. Which function reads a single character from a file?
Correct Answer: (B) fgetc()
27. Which function writes a string to a file in C?
Correct Answer: (C) fputs()
28. What is the return type of the fopen() function?
Correct Answer: (B) FILE*
29. Which function is used to set the position to a specified location in a file?
Correct Answer: (B) fseek()
30. Which function is used to get the current position in a file?
Correct Answer: (C) ftell()
31. What does the 'a' mode in fopen() signify?
Correct Answer: (C) Append mode
32. Which function can be used to read formatted data from a file?
Correct Answer: (B) fscanf()
33. What is the correct way to close a file in C?
Correct Answer: (A) fclose(fp);
34. Which function can be used to write formatted data into a file?
Correct Answer: (B) fprintf()
35. In C, what is a file pointer?
Correct Answer: (A) A pointer to a structure FILE
36. Which of the following is not a valid file operation mode?
Correct Answer: (C) "r++"
37. Which function is used to write integer data into a file?
Correct Answer: (B) putw()
38. How do you check if fopen() failed to open a file?
Correct Answer: (A) By checking if the pointer returned is NULL
39. Which function reads a string from a file?
Correct Answer: (B) fgets()
40. What will fopen() return if it fails to open a file?
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?
Correct Answer: (A) fopen()
42. Which mode is used to open a binary file for reading?
Correct Answer: (A) "rb"
43. What is the extension used for text files?
Correct Answer: (D) .txt
44. Which of the following is used to open a file in binary mode?
Correct Answer: (C) "wb"
45. What does the fwrite() function do in C?
Correct Answer: (B) Write data into a binary file
46. Which function is used to read from a binary file in C?
Correct Answer: (A) fread()
47. How is data stored in a binary file?
Correct Answer: (C) In binary format (0s and 1s)
48. What is the primary difference between text files and binary files?
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?
Correct Answer: (C) "ab"
50. What is the return value of fread() on successful reading?
Correct Answer: (B) The number of elements read
51. Which function can write data into a binary file?
Correct Answer: (B) fwrite()
52. What happens if you try to open a binary file in "rb" mode and the file does not exist?
Correct Answer: (B) fopen() returns NULL
53. Which function is used to move the file pointer to a specific position in a file?
Correct Answer: (C) fseek()
54. What is the advantage of using binary files over 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?
Correct Answer: (A) "r+b"
56. How is end-of-file detected when working with text files?
Correct Answer: (B) By checking if fgetc() returns EOF
57. In which of the following is data stored in human-readable format?
Correct Answer: (A) Text files
58. What does fseek() return if it successfully moves the file pointer?
Correct Answer: (C) 0
59. What happens when fwrite() fails to write to a file?
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?
Correct Answer: (B) "w"