MCQs Of Class 31 - Advanced File Handling in PHP
Here are 100 multiple-choice questions (MCQs) related to PHP, specifically focused on file handling, advanced file operations, and related concepts. Each question includes four options, with the correct answer indicated.
MCQs on PHP File Handling
1. Which function is used to open a file in PHP?
o A) open()
o B) fopen()
o C) file_open()
o D) file()
Answer: B) fopen()
2. What mode should you use to open a file for writing and truncating its contents?
o A) 'r'
o B) 'w'
o C) 'a'
o D) 'r+'
Answer: B) 'w'
3. Which function is used to read a single line from a file?
o A) read_line()
o B) fgets()
o C) read()
o D) file_get_contents()
Answer: B) fgets()
4. What does the fwrite() function do?
o A) Reads from a file
o B) Writes to a file
o C) Closes a file
o D) Deletes a file
Answer: B) Writes to a file
5. How can you check if a file exists in PHP?
o A) file_exists()
o B) is_file()
o C) check_file()
o D) exists()
Answer: A) file_exists()
6. Which function is used to close an open file?
o A) close()
o B) fclose()
o C) file_close()
o D) endfile()
Answer: B) fclose()
7. What does the fread() function return?
o A) The entire file content
o B) The number of bytes read
o C) A specific number of bytes
o D) True if successful
Answer: C) A specific number of bytes
8. Which function can be used to read the entire file into an array?
o A) file()
o B) readfile()
o C) file_get_contents()
o D) file_array()
Answer: A) file()
9. In which mode does fopen() open a file for appending?
o A) 'r'
o B) 'w'
o C) 'a'
o D) 'x'
Answer: C) 'a'
10. Which of the following will output the contents of a file?
o A) file_get_contents('filename.txt');
o B) readfile('filename.txt');
o C) Both A and B
o D) None of the above
Answer: C) Both A and B
11. Which function is used to get the size of a file?
o A) filesize()
o B) file_size()
o C) getfilesize()
o D) filelength()
Answer: A) filesize()
12. What happens if you try to open a file that does not exist using fopen() with mode 'r'?
o A) A new file is created
o B) An error is returned
o C) The file is opened in read mode
o D) The function returns null
Answer: B) An error is returned
13. Which mode is used for reading and writing to a file without truncating it?
o A) 'r+'
o B) 'w+'
o C) 'a+'
o D) 'x+'
Answer: A) 'r+'
14. Which of the following functions can be used to read a CSV file?
o A) fgetcsv()
o B) read_csv()
o C) file_get_csv()
o D) csv_read()
Answer: A) fgetcsv()
15. What is the default file mode for fopen() if no mode is specified?
o A) 'r'
o B) 'w'
o C) 'a'
o D) 'r+'
Answer: A) 'r'
16. How do you write a line to a file and include a newline character in PHP?
o A) fwrite($file, "Line 1");
o B) fwrite($file, "Line 1\n");
o C) fwrite($file, "Line 1\r\n");
o D) Both B and C
Answer: D) Both B and C
17. Which function checks whether a variable is a file resource?
o A) is_file()
o B) is_resource()
o C) filetype()
o D) is_open()
Answer: B) is_resource()
18. What will file_get_contents('file.txt') return if the file does not exist?
o A) False
o B) An empty string
o C) Null
o D) An error
Answer: A) False
19. Which function is NOT used for file handling in PHP?
o A) fwrite()
o B) fclose()
o C) readfile()
o D) checkfile()
Answer: D) checkfile()
20. What is the return type of fopen() on success?
o A) Boolean
o B) Integer
o C) Resource
o D) String
Answer: C) Resource
21. What is the output of file_put_contents('test.txt', 'Hello World!')?
o A) Number of bytes written
o B) True
o C) Hello World!
o D) False
Answer: A) Number of bytes written
22. Which function can be used to copy a file?
o A) copy()
o B) move()
o C) file_copy()
o D) duplicate()
Answer: A) copy()
23. Which function is used to delete a file?
o A) unlink()
o B) delete_file()
o C) remove()
o D) file_delete()
Answer: A) unlink()
24. Which of the following is true about the file() function?
o A) It reads the file as a single string.
o B) It returns an array of file lines.
o C) It only works with text files.
o D) It can write to a file.
Answer: B) It returns an array of file lines.
25. What will happen if you try to open a file with fopen() in 'x' mode when the file already exists?
o A) The file will be truncated
o B) An error will occur
o C) The file will be opened for reading
o D) The existing file will be deleted
Answer: B) An error will occur
26. What is the purpose of the flock() function?
o A) To read from a file
o B) To lock a file for reading or writing
o C) To delete a file
o D) To close a file
Answer: B) To lock a file for reading or writing
27. Which function reads a file and outputs it directly to the browser?
o A) readfile()
o B) file_get_contents()
o C) outputfile()
o D) file_echo()
Answer: A) readfile()
28. How do you get the current position of a file pointer?
o A) ftell()
o B) fposition()
o C) file_pos()
o D) tell()
Answer: A) ftell()
29. What will rewind($handle) do?
o A) Close the file
o B) Move the file pointer to the end
o C) Move the file pointer to the beginning
o D) Read the entire file
Answer: C) Move the file pointer to the beginning
30. Which of the following file modes allows for both reading and writing without truncating the file?
o A) 'a+'
o B) 'r+'
o C) 'w+'
o D) 'x+'
Answer: B) 'r+'
31. In PHP, what does the fileperms() function return?
o A) The owner of the file
o B) The permissions of the file
o C) The size of the file
o D) The last modified time of the file
Answer: B) The permissions of the file
32. Which function will you use to set the file pointer to a specific position in a file?
o A) fseek()
o B) fset()
o C) setpos()
o D) move()
Answer: A) fseek()
33. Which of the following functions will return the last modified time of a file?
o A) filemtime()
o B) getmtime()
o C) last_modified()
o D) file_time()
Answer: A) filemtime()
34. How do you check if a directory exists in PHP?
o A) directory_exists()
o B) is_dir()
o C) check_dir()
o D) dir_exists()
Answer: B) is_dir()
35. What is the function used to create a directory in PHP?
o A) mk_dir()
o B) mkdir()
o C) create_dir()
o D) new_dir()
Answer: B) mkdir()
36. Which function reads the entire contents of a file into a string?
o A) file_get_contents()
o B) readfile()
o C) file_read()
o D) read_string()
Answer: A) file_get_contents()
37. What is the correct way to use the fgets() function?
o A) fgets($file_handle);
o B) fgets('file.txt');
o C) fgets($file_handle, $length);
o D) Both A and C
Answer: D) Both A and C
38. What does the r+ mode allow you to do?
o A) Read and write but truncate
o B) Read and write without truncating
o C) Append only
o D) Read only
Answer: B) Read and write without truncating
39. Which of the following statements is true regarding the unlink() function?
o A) It can delete both files and directories.
o B) It can only delete files.
o C) It is used to remove file permissions.
o D) It cannot delete files that are open.
Answer: B) It can only delete files.
40. Which PHP function can be used to output a file's contents directly to the output buffer?
o A) file_output()
o B) outputfile()
o C) readfile()
o D) file_get_contents()
Answer: C) readfile()
41. What is the return type of fgetcsv()?
o A) String
o B) Array
o C) Boolean
o D) Integer
Answer: B) Array
42. Which of the following will NOT create a new file if it doesn't exist?
o A) 'w'
o B) 'a'
o C) 'x'
o D) 'r'
Answer: D) 'r'
43. Which function will you use to read the next line from a file pointer?
o A) read_line()
o B) fgets()
o C) get_line()
o D) next_line()
Answer: B) fgets()
44. How can you read a file line by line in a loop?
o A) While ($line = fgets($file)) {}
o B) For ($line in $file) {}
o C) Loop ($file) {}
o D) read($file);
Answer: A) While ($line = fgets($file)) {}
45. What will the chmod() function do?
o A) Change file ownership
o B) Change file permissions
o C) Change file size
o D) Change file name
Answer: B) Change file permissions
46. Which mode should you use to create a new file or return false if the file already exists?
o A) 'w'
o B) 'a'
o C) 'x'
o D) 'r+'
Answer: C) 'x'
47. What will fclose($handle) do?
o A) It will lock the file.
o B) It will unlock the file.
o C) It will close the file resource.
o D) It will delete the file.
Answer: C) It will close the file resource.
48. How do you change the current working directory in PHP?
o A) change_dir()
o B) setcwd()
o C) chdir()
o D) cwd()
Answer: C) chdir()
49. What will the readfile() function return?
o A) Number of bytes read
o B) Contents of the file
o C) True if successful
o D) It outputs the contents directly to the output buffer.
Answer: D) It outputs the contents directly to the output buffer.
50. Which function can be used to append data to a file?
o A) fwrite()
o B) appendfile()
o C) file_put_contents()
o D) Both A and C
Answer: D) Both A and C
51. What will happen if you attempt to read from a file that you opened with 'w' mode?
o A) It will throw an error.
o B) It will read the data before truncation.
o C) It will read nothing because the file is empty.
o D) It will read the entire file.
Answer: C) It will read nothing because the file is empty.
52. What is the purpose of the file_get_contents() function?
o A) To write data to a file
o B) To read the entire contents of a file into a string
o C) To get the size of a file
o D) To copy a file
Answer: B) To read the entire contents of a file into a string
53. How can you create a temporary file in PHP?
o A) temp_file()
o B) tmpfile()
o C) create_temp_file()
o D) make_temp_file()
Answer: B) tmpfile()
54. Which function can be used to rename a file?
o A) change()
o B) rename()
o C) move_file()
o D) file_rename()
Answer: B) rename()
55. What does fputcsv() do?
o A) Outputs a CSV string
o B) Reads a CSV file
o C) Writes an array to a CSV file
o D) None of the above
Answer: C) Writes an array to a CSV file
56. What is the purpose of the fstat() function?
o A) To get the status of a file
o B) To get file statistics
o C) To read file content
o D) To check file permissions
Answer: B) To get file statistics
57. What is the purpose of the file_put_contents() function?
o A) To read a file
o B) To write data to a file
o C) To get file content
o D) To check if a file exists
Answer: B) To write data to a file
58. Which mode will truncate the file if it exists?
o A) 'w'
o B) 'a'
o C) 'r'
o D) 'x'
Answer: A) 'w'
59. How do you safely delete a file in PHP?
o A) unlink()
o B) delete_file()
o C) remove()
o D) clear_file()
Answer: A) unlink()
60. Which function can be used to read a specified number of bytes from a file?
o A) fread()
o B) read_bytes()
o C) get_bytes()
o D) file_read()
Answer: A) fread()
61. What is the use of the file_exists() function?
o A) To check if a file is empty
o B) To verify if a file is accessible
o C) To determine if a file is present
o D) To check if a file is writable
Answer: C) To determine if a file is present
62. Which function will move the file pointer back to the beginning of the file?
o A) rewind()
o B) fseek()
o C) reset()
o D) start()
Answer: A) rewind()
63. What will the file() function return if the file is empty?
o A) An empty string
o B) Null
o C) An empty array
o D) False
Answer: C) An empty array
64. Which PHP function can be used to get the last access time of a file?
o A) fileatime()
o B) get_last_access()
o C) filemtime()
o D) access_time()
Answer: A) fileatime()
65. What happens if you call fclose() on a file that is already closed?
o A) Nothing
o B) It will throw an error
o C) It will reopen the file
o D) It will delete the file
Answer: A) Nothing
66. How can you ensure a file is created if it does not exist when opening?
o A) Use 'w' mode
o B) Use 'a' mode
o C) Use 'x' mode
o D) Both A and B
Answer: C) Use 'x' mode
67. Which function would you use to check the type of a file?
o A) file_type()
o B) get_file_type()
o C) filetype()
o D) check_file_type()
Answer: C) filetype()
68. What is the purpose of the stream_get_contents() function?
o A) To read a stream into a string
o B) To get the contents of a file
o C) To write data to a stream
o D) To close a stream
Answer: A) To read a stream into a string
69. How do you prevent a file from being overwritten when writing?
o A) Check file size before writing
o B) Use 'w' mode
o C) Use 'x' mode
o D) None of the above
Answer: C) Use 'x' mode
70. What does the clearstatcache() function do?
o A) Clears the cache for file statistics
o B) Clears all file caches
o C) Resets file permissions
o D) Deletes all cached files
Answer: A) Clears the cache for file statistics
71. What type of value does filemtime() return?
o A) String
o B) Boolean
o C) Timestamp
o D) Integer
Answer: C) Timestamp
72. Which function is used to get the current working directory?
o A) current_dir()
o B) getcwd()
o C) cwd()
o D) get_current_directory()
Answer: B) getcwd()
73. Which of the following is true about fseek()?
o A) Moves the file pointer to a specific location
o B) Cannot move the pointer backward
o C) Can only move to the end of the file
o D) It closes the file
Answer: A) Moves the file pointer to a specific location
74. What will the is_readable() function return?
o A) True if the file can be read, otherwise false
o B) The number of bytes readable
o C) The contents of the file
o D) True if the file exists
Answer: A) True if the file can be read, otherwise false
75. Which function will remove all data from a file?
o A) unlink()
o B) delete()
o C) truncate()
o D) None of the above
Answer: C) truncate()
76. How can you check if a file is writable?
o A) is_writable()
o B) file_writable()
o C) check_writable()
o D) writable()
Answer: A) is_writable()
77. Which of the following functions is used to write data to a file?
o A) fwrite()
o B) write_file()
o C) put_contents()
o D) Both A and C
Answer: D) Both A and C
78. What does the file_put_contents() function return on success?
o A) Number of bytes written
o B) True
o C) String
o D) Array
Answer: A) Number of bytes written
79. What will the fileperms() function return?
o A) File path
o B) File permissions
o C) File owner
o D) File size
Answer: B) File permissions
80. Which mode should you use to read and write to a file without truncating it?
o A) 'w'
o B) 'a'
o C) 'r+'
o D) 'x'
Answer: C) 'r+'
81. What happens if you use fopen() with a mode that doesn't exist?
o A) It throws an error
o B) It creates a new file
o C) It returns false
o D) It returns null
Answer: C) It returns false
82. What is the purpose of the pathinfo() function?
o A) To get file statistics
o B) To retrieve information about a file path
o C) To check file existence
o D) To rename a file
Answer: B) To retrieve information about a file path
83. Which function will give you the size of a file?
o A) filesize()
o B) file_size()
o C) get_size()
o D) length()
Answer: A) filesize()
84. What will feof() check?
o A) If the file is open
o B) If the end of the file has been reached
o C) If the file is readable
o D) If the file exists
Answer: B) If the end of the file has been reached
85. What is returned by fread()?
o A) The entire contents of the file
o B) The number of bytes read
o C) A specified number of bytes
o D) True if successful
Answer: C) A specified number of bytes
86. Which PHP function is used to open a file?
o A) open_file()
o B) fopen()
o C) file_open()
o D) get_file()
Answer: B) fopen()
87. How can you read a CSV file in PHP?
o A) Using fgetcsv()
o B) Using fread()
o C) Using read_csv()
o D) Using file_get_contents()
Answer: A) Using fgetcsv()
88. Which of the following functions will read from a file until the end?
o A) fread()
o B) read_all()
o C) feof()
o D) fgets()
Answer: A) fread()
89. What will the fgetss() function do?
o A) Reads a string from a file and strips HTML tags
o B) Reads a string from a file
o C) Writes a string to a file
o D) Gets the size of a string
Answer: A) Reads a string from a file and strips HTML tags
90. What does the file_put_contents() function do if the file exists?
o A) It appends to the file
o B) It overwrites the file
o C) It throws an error
o D) It does nothing
Answer: B) It overwrites the file
91. Which of the following modes is used for writing only?
o A) 'a'
o B) 'r'
o C) 'w'
o D) 'x'
Answer: C) 'w'
92. What does the is_dir() function check?
o A) If a path is a file
o B) If a path is a directory
o C) If a directory is empty
o D) If a directory exists
Answer: B) If a path is a directory
93. Which function can be used to clear the output buffer?
o A) ob_clean()
o B) clear_buffer()
o C) flush()
o D) ob_flush()
Answer: A) ob_clean()
94. What will the set_time_limit() function do?
o A) It will limit the file size
o B) It will limit the execution time of the script
o C) It will change the timeout for file reading
o D) It will stop file execution
Answer: B) It will limit the execution time of the script
95. What does stat() return?
o A) An array of file information
o B) A string with the file name
o C) A boolean value
o D) An integer
Answer: A) An array of file information
96. Which function is used to change the owner of a file?
o A) chown()
o B) change_owner()
o C) set_owner()
o D) file_owner()
Answer: A) chown()
97. What happens if you open a file in 'a' mode?
o A) It truncates the file
o B) It appends data to the file
o C) It creates a new file
o D) It opens the file for reading
Answer: B) It appends data to the file
98. Which of the following can be used to check for the existence of a file?
o A) exists()
o B) file_exists()
o C) is_present()
o D) file_check()
Answer: B) file_exists()
99. Which function will give the path of the parent directory?
o A) dirname()
o B) parent_dir()
o C) get_parent_directory()
o D) path_parent()
Answer: A) dirname()
100. What will happen if you try to create a directory that already exists using mkdir()? - A) It will throw an error - B) It will overwrite the existing directory - C) It will do nothing - D) It will append a number to the directory name
Answer: A) It will throw an error