MCQs For Class 13: File Handling - Part 2

Rashmi Mishra
0

 Multiple-Choice Questions (MCQs)

Class 13: File Handling - Part 2


  1. Which function is used to copy a file in PHP?
    • A) move()
    • B) copy()
    • C) duplicate()
    • D) clone()
    • Answer: B) copy()
  2. What does the file_exists() function check?
    • A) If a directory is writable
    • B) If a file is empty
    • C) If a file or directory exists
    • D) If a file is readable
    • Answer: C) If a file or directory exists
  3. Which function would you use to delete a file?
    • A) unlink()
    • B) remove()
    • C) delete()
    • D) destroy()
    • Answer: A) unlink()
  4. What does the is_writable() function check?
    • A) If a file exists
    • B) If a directory is readable
    • C) If a file or directory is writable
    • D) If a file is executable
    • Answer: C) If a file or directory is writable
  5. Which function is used to read the entire content of a file into a string?
    • A) file_get_contents()
    • B) read_file()
    • C) file_read()
    • D) get_contents()
    • Answer: A) file_get_contents()
  6. To append data to a file, which flag should you use with file_put_contents()?
    • A) FILE_APPEND
    • B) FILE_WRITE
    • C) FILE_CREATE
    • D) FILE_ADD
    • Answer: A) FILE_APPEND
  7. Which function changes the permissions of a file in PHP?
    • A) fileperms()
    • B) chmod()
    • C) set_permissions()
    • D) change_mode()
    • Answer: B) chmod()
  8. What will rename("file.txt", "newfile.txt") do?
    • A) Copy file.txt to newfile.txt
    • B) Move file.txt to a new location
    • C) Delete file.txt
    • D) Rename file.txt to newfile.txt
    • Answer: D) Rename file.txt to newfile.txt
  9. Which of the following is true about file_put_contents()?
    • A) It can only write to binary files.
    • B) It can create a new file if it doesn't exist.
    • C) It can only append data to existing files.
    • D) It returns the number of bytes written to the file only.
    • Answer: B) It can create a new file if it doesn't exist.
  10. How can you read a file line by line in PHP?
    • A) file_get_contents()
    • B) fgets()
    • C) readline()
    • D) file_read_lines()
    • Answer: B) fgets()
  11. Which function checks the permissions of a file and returns its mode?
    • A) filemode()
    • B) permissions()
    • C) fileperms()
    • D) check_perms()
    • Answer: C) fileperms()
  12. What does the fopen() function do?
    • A) Opens a file for writing
    • B) Opens a file for reading
    • C) Opens a file for both reading and writing
    • D) All of the above
    • Answer: D) All of the above
  13. How do you create a directory in PHP?
    • A) create_dir()
    • B) mkdir()
    • C) make_directory()
    • D) new_directory()
    • Answer: B) mkdir()
  14. Which function can be used to delete a directory?
    • A) rmdir()
    • B) remove_dir()
    • C) delete_directory()
    • D) unlink_dir()
    • Answer: A) rmdir()
  15. If you want to write data to a file without overwriting it, you should use which mode with fopen()?
    • A) w
    • B) a
    • C) r
    • D) x
    • Answer: B) a
  16. What is the purpose of the basename() function?
    • A) Returns the filename without its extension
    • B) Returns the path of a file
    • C) Returns the last component of a path
    • D) None of the above
    • Answer: C) Returns the last component of a path
  17. Which function reads a file and returns its contents as an array, where each line is an element?
    • A) file()
    • B) fgetcsv()
    • C) read_file()
    • D) file_lines()
    • Answer: A) file()
  18. To get the current working directory, which function would you use?
    • A) getcwd()
    • B) current_dir()
    • C) get_current_directory()
    • D) cwd()
    • Answer: A) getcwd()
  19. Which function checks if a path is a directory?
    • A) is_dir()
    • B) directory_exists()
    • C) is_directory()
    • D) check_dir()
    • Answer: A) is_dir()
  20. What is the return type of file_get_contents()?
    • A) Boolean
    • B) Integer
    • C) Array
    • D) String
    • Answer: D) String
  21. What does the fclose() function do?
    • A) Closes a file pointer
    • B) Deletes a file
    • C) Flushes a file
    • D) Opens a file
    • Answer: A) Closes a file pointer
  22. Which of the following will NOT create a file if it doesn't exist?
    • A) fopen("file.txt", "w")
    • B) fopen("file.txt", "x")
    • C) file_put_contents("file.txt", "Hello")
    • D) file_get_contents("file.txt")
    • Answer: D) file_get_contents("file.txt")
  23. Which function can you use to get the last access time of a file?
    • A) filemtime()
    • B) fileatime()
    • C) filectime()
    • D) access_time()
    • Answer: B) fileatime()
  24. What will file("file.txt") return?
    • A) The content of the file as a string
    • B) An array of the file's lines
    • C) The number of lines in the file
    • D) A boolean value indicating success
    • Answer: B) An array of the file's lines
  25. Which PHP function allows you to iterate over each line of a file?
    • A) readline()
    • B) fgets()
    • C) fgetcsv()
    • D) fgetlines()
    • Answer: B) fgets()
  26. How can you open a file for both reading and writing?
    • A) fopen("file.txt", "r+")
    • B) fopen("file.txt", "rw")
    • C) fopen("file.txt", "a+")
    • D) fopen("file.txt", "x+")
    • Answer: A) fopen("file.txt", "r+")
  27. To read a file until the end, which function would you use?
    • A) readfile()
    • B) fread()
    • C) feof()
    • D) fget()
    • Answer: C) feof()
  28. Which function returns the number of bytes in a file?
    • A) filesize()
    • B) count_bytes()
    • C) byte_length()
    • D) file_count()
    • Answer: A) filesize()
  29. What does the scandir() function return?
    • A) An array of files and directories in a specified directory
    • B) A list of file permissions
    • C) The number of files in a directory
    • D) The path of a directory
    • Answer: A) An array of files and directories in a specified directory
  30. Which function would you use to change a directory's name?
    • A) rename_dir()
    • B) change_dir()
    • C) rename()
    • D) dir_rename()
    • Answer: C) rename()
  31. To write data to a file in binary mode, which flag would you use with fopen()?
    • A) b
    • B) wb
    • C) ab
    • D) rb
    • Answer: B) wb
  32. What does the rewind() function do?
    • A) Resets a file pointer to the beginning of a file
    • B) Closes a file
    • C) Deletes a file
    • D) Copies a file
    • Answer: A) Resets a file pointer to the beginning of a file
  33. How do you read a specific number of bytes from a file?
    • A) fread($fp, $length)
    • B) file_get_contents($length)
    • C) fgets($fp, $length)
    • D) read_bytes($fp, $length)
    • Answer: A) fread($fp, $length)
  34. Which function will you use to get a file's modification time?
    • A) filemtime()
    • B) file_date()
    • C) mod_time()
    • D) last_modified()
    • Answer: A) filemtime()
  35. What does the file_put_contents() function return on success?
    • A) Boolean value
    • B) Number of bytes written
    • C) The string written
    • D) File size
    • Answer: B) Number of bytes written
  36. Which of the following statements about fwrite() is true?
    • A) It can only write strings to a file.
    • B) It can write binary data to a file.
    • C) It closes the file after writing.
    • D) It can only append data.
    • Answer: B) It can write binary data to a file.
  37. How would you safely delete a file in PHP?
    • A) delete("file.txt")
    • B) unlink("file.txt")
    • C) remove("file.txt")
    • D) rmdir("file.txt")
    • Answer: B) unlink("file.txt")
  38. To read a CSV file, which function is commonly used?
    • A) fgetcsv()
    • B) file_csv()
    • C) csv_read()
    • D) read_csv()
    • Answer: A) fgetcsv()
  39. Which of the following correctly checks if a file is readable?
    • A) is_readable("file.txt")
    • B) check_readable("file.txt")
    • C) file_readable("file.txt")
    • D) Both A and C
    • Answer: D) Both A and C
  40. What is the effect of using the clearstatcache() function?
    • A) Clears the contents of a file
    • B) Clears the file pointer position
    • C) Clears the cache for file status functions
    • D) Clears the memory cache
    • Answer: C) Clears the cache for file status functions
  41. Which function would you use to get an array of files in a directory excluding the special entries?
    • A) scandir()
    • B) glob()
    • C) dir_list()
    • D) list_files()
    • Answer: B) glob()
  42. How can you create a temporary file in PHP?
    • A) tempfile()
    • B) create_temp_file()
    • C) make_temp_file()
    • D) tmpfile()
    • Answer: D) tmpfile()
  43. Which function checks if a file is a regular file?
    • A) is_file()
    • B) is_regular()
    • C) check_file()
    • D) file_check()
    • Answer: A) is_file()
  44. What is the correct way to open a file for writing, creating it if it doesn't exist?
    • A) fopen("file.txt", "a")
    • B) fopen("file.txt", "w+")
    • C) fopen("file.txt", "x")
    • D) fopen("file.txt", "c")
    • Answer: B) fopen("file.txt", "w+")
  45. What does the flock() function do?
    • A) Locks a file for reading or writing
    • B) Unlocks a file
    • C) Checks if a file is locked
    • D) Deletes a file lock
    • Answer: A) Locks a file for reading or writing
  46. Which function would you use to truncate a file to zero length?
    • A) truncate()
    • B) ftruncate()
    • C) clear()
    • D) empty_file()
    • Answer: B) ftruncate()
  47. How can you write an array to a file as JSON?
    • A) file_put_contents("file.json", json_encode($array))
    • B) write_json("file.json", $array)
    • C) json_write($array, "file.json")
    • D) fwrite("file.json", $array)
    • Answer: A) file_put_contents("file.json", json_encode($array))
  48. What does the touch() function do?
    • A) Creates a file if it doesn't exist
    • B) Updates the access time of a file
    • C) Both A and B
    • D) None of the above
    • Answer: C) Both A and B
  49. Which of the following would create a directory recursively?
    • A) mkdir("dir/subdir", 0777, true)
    • B) create_dir("dir/subdir")
    • C) mkdir("dir/subdir")
    • D) make_directory("dir/subdir")
    • Answer: A) mkdir("dir/subdir", 0777, true)
  50. To read from a file until EOF (end of file), which function can you use?
    • A) fgets()
    • B) fread()
    • C) feof()
    • D) readline()
    • Answer: C) feof()
  51. Which function is used to check if a directory is empty?
    • A) is_empty()
    • B) empty_dir()
    • C) check_empty()
    • D) count_files()
    • Answer: D) count_files()
  52. What is the default mode when opening a file with fopen()?
    • A) Read-only
    • B) Write-only
    • C) Read and write
    • D) Append
    • Answer: A) Read-only
  53. How do you ensure that a directory is created only if it does not already exist?
    • A) Check with is_dir()
    • B) Use mkdir() with error handling
    • C) Both A and B
    • D) Just use mkdir()
    • Answer: C) Both A and B
  54. Which function can be used to find the path of a file?
    • A) realpath()
    • B) pathinfo()
    • C) get_path()
    • D) file_path()
    • Answer: A) realpath()
  55. What is the purpose of fgetc()?
    • A) Reads a character from a file
    • B) Reads a line from a file
    • C) Reads the entire file
    • D) Writes a character to a file
    • Answer: A) Reads a character from a file
  56. What is the effect of using file_put_contents() with LOCK_EX?
    • A) Prevents concurrent writes
    • B) Forces a write
    • C) Deletes the file after writing
    • D) None of the above
    • Answer: A) Prevents concurrent writes
  57. Which function opens a file and locks it for writing?
    • A) fopen()
    • B) flock()
    • C) fwrite()
    • D) lock_file()
    • Answer: B) flock()
  58. What happens if you try to open a file with the x mode using fopen()?
    • A) It creates the file and opens it
    • B) It opens the file if it exists
    • C) It returns false if the file already exists
    • D) It truncates the file
    • Answer: C) It returns false if the file already exists
  59. How do you read a single line from a file in a loop until EOF?
    • A) while (!feof($fp)) { fgets($fp); }
    • B) while (fgets($fp) !== false) {}
    • C) for each (fgets($fp)) {}
    • D) do { fgets($fp); } while (!feof($fp));
    • Answer: B) while (fgets($fp) !== false) {}
  60. What is the correct way to delete a directory in PHP?
    • A) delete_directory("dir")
    • B) rmdir("dir")
    • C) unlink("dir")
    • D) remove_directory("dir")
    • Answer: B) rmdir("dir")
  61. Which function will give you the last access time of a file?
    • A) fileatime()
    • B) last_access()
    • C) file_access_time()
    • D) access_time()
    • Answer: A) fileatime()
  62. What function is used to write data to a file?
    • A) write_file()
    • B) fwrite()
    • C) file_write()
    • D) put_file()
    • Answer: B) fwrite()
  63. How would you open a file for both reading and writing?
    • A) fopen("file.txt", "rw")
    • B) fopen("file.txt", "r+")
    • C) fopen("file.txt", "w+")
    • D) fopen("file.txt", "a+")
    • Answer: B) fopen("file.txt", "r+")
  64. What is the output of var_dump(file_get_contents("file.txt"))?
    • A) String representation of the file contents
    • B) Boolean value
    • C) An array of file lines
    • D) Number of bytes in the file
    • Answer: A) String representation of the file contents
  65. Which of the following is a valid way to check if a file exists?
    • A) file_exists("file.txt")
    • B) check_file("file.txt")
    • C) is_file("file.txt")
    • D) Both A and C
    • Answer: D) Both A and C
  66. How do you create a file in write mode and truncate it if it already exists?
    • A) fopen("file.txt", "a")
    • B) fopen("file.txt", "w")
    • C) fopen("file.txt", "x")
    • D) fopen("file.txt", "c")
    • Answer: B) fopen("file.txt", "w")
  67. Which function reads a specific line from a file?
    • A) fgets()
    • B) fget_line()
    • C) get_line()
    • D) read_line()
    • Answer: A) fgets()
  68. What will happen if you try to read from a file that is not open?
    • A) An error will be thrown
    • B) It will return false
    • C) It will create a new file
    • D) It will read the last data
    • Answer: A) An error will be thrown
  69. Which function is used to read a directory's contents?
    • A) read_dir()
    • B) opendir()
    • C) scandir()
    • D) dir_contents()
    • Answer: C) scandir()
  70. How do you move the file pointer to the end of a file?
    • A) fseek($fp, 0, SEEK_END)
    • B) fseek($fp, -1, SEEK_END)
    • C) fseek($fp, 0, SEEK_SET)
    • D) fseek($fp, 1, SEEK_END)
    • Answer: A) fseek($fp, 0, SEEK_END)
  71. What is the purpose of the stat() function?
    • A) It returns information about a file
    • B) It opens a file
    • C) It creates a file
    • D) It deletes a file
    • Answer: A) It returns information about a file
  72. Which of the following functions will return the number of bytes in a file?
    • A) filesize()
    • B) file_length()
    • C) get_filesize()
    • D) count_bytes()
    • Answer: A) filesize()
  73. How would you append data to an existing file?
    • A) fopen("file.txt", "a")
    • B) fopen("file.txt", "w")
    • C) fwrite("file.txt", $data)
    • D) append_to_file("file.txt", $data)
    • Answer: A) fopen("file.txt", "a")
  74. What does the getcwd() function return?
    • A) Current working directory
    • B) Current file path
    • C) Current file name
    • D) Current script name
    • Answer: A) Current working directory
  75. What is the function to copy a file?
    • A) copy_file()
    • B) duplicate()
    • C) copy()
    • D) file_copy()
    • Answer: C) copy()
  76. How do you check if a file is writable?
    • A) is_writable("file.txt")
    • B) check_write("file.txt")
    • C) file_writable("file.txt")
    • D) can_write("file.txt")
    • Answer: A) is_writable("file.txt")
  77. Which of the following functions closes an open file?
    • A) close_file()
    • B) fclose()
    • C) end_file()
    • D) file_close()
    • Answer: B) fclose()
  78. How do you get a list of all directories in a specific path?
    • A) list_dirs()
    • B) get_dirs()
    • C) scandir()
    • D) dir_list()
    • Answer: C) scandir()
  79. What function would you use to create a symbolic link?
    • A) symlink()
    • B) link()
    • C) create_link()
    • D) add_symlink()
    • Answer: A) symlink()
  80. How can you read an entire file into a string?
    • A) read_file("file.txt")
    • B) file_get_contents("file.txt")
    • C) fread("file.txt")
    • D) fgets("file.txt")
    • Answer: B) file_get_contents("file.txt")
  81. Which function returns the current line number in a file?
    • A) line_number()
    • B) get_line_number()
    • C) ftell()
    • D) fgets()
    • Answer: C) ftell()
  82. To modify the contents of a file, which mode would you use with fopen()?
    • A) w
    • B) r
    • C) a
    • D) r+
    • Answer: D) r+
  83. Which function will check if a path is a directory?
    • A) is_directory()
    • B) is_dir()
    • C) check_dir()
    • D) dir_check()
    • Answer: B) is_dir()
  84. How do you read a file as an array of lines?
    • A) file("file.txt")
    • B) read_lines("file.txt")
    • C) fget_lines("file.txt")
    • D) lines("file.txt")
    • Answer: A) file("file.txt")
  85. Which of the following functions is used to rename a file?
    • A) rename_file()
    • B) rename()
    • C) change_name()
    • D) move_file()
    • Answer: B) rename()
  86. What is the effect of calling unlink() on a file?
    • A) It renames the file
    • B) It deletes the file
    • C) It moves the file
    • D) It locks the file
    • Answer: B) It deletes the file
  87. How can you check if a file is executable?
    • A) is_executable()
    • B) check_exec()
    • C) file_executable()
    • D) can_execute()
    • Answer: A) is_executable()
  88. What is the purpose of the fgets() function?
    • A) Reads a specified number of bytes from a file
    • B) Reads a line from a file
    • C) Writes a line to a file
    • D) Opens a file
    • Answer: B) Reads a line from a file
  89. Which function would you use to get the last modified time of a file?
    • A) filemtime()
    • B) last_modified()
    • C) get_mod_time()
    • D) modified_time()
    • Answer: A) filemtime()
  90. How do you change the permissions of a file?
    • A) chmod()
    • B) file_permissions()
    • C) change_mode()
    • D) set_permissions()
    • Answer: A) chmod()
  91. Which function would you use to create a new directory?
    • A) create_directory()
    • B) mkdir()
    • C) new_dir()
    • D) dir_create()
    • Answer: B) mkdir()
  92. What does the pathinfo() function return?
    • A) Information about a file's path
    • B) The current path
    • C) The directory path
    • D) The file's size
    • Answer: A) Information about a file's path
  93. How do you get the file name from a file path?
    • A) basename()
    • B) get_name()
    • C) file_name()
    • D) name_from_path()
    • Answer: A) basename()
  94. Which function checks if a file is empty?
    • A) is_empty()
    • B) empty_file()
    • C) filesize()
    • D) is_file_empty()
    • Answer: C) filesize()
  95. What happens if you try to write to a file that is opened in read mode?
    • A) It writes at the end of the file
    • B) An error occurs
    • C) It overwrites the file
    • D) Nothing happens
    • Answer: B) An error occurs
  96. Which function can you use to read a file line by line?
    • A) read_lines()
    • B) fgets()
    • C) file_line()
    • D) read_file()
    • Answer: B) fgets()
  97. How do you get the contents of a file and display them directly?
    • A) readfile("file.txt")
    • B) file("file.txt")
    • C) get_file_contents("file.txt")
    • D) fget("file.txt")
    • Answer: A) readfile("file.txt")
  98. What does the chgrp() function do?
    • A) Changes the file's group ownership
    • B) Changes the file's permissions
    • C) Changes the file's owner
    • D) Renames the file
    • Answer: A) Changes the file's group ownership
  99. Which of the following functions creates a temporary file?
    • A) tempfile()
    • B) tmpfile()
    • C) create_temp_file()
    • D) make_temp_file()
    • Answer: B) tmpfile()
  100. What is the return value of fopen() if it fails to open a file? - A) NULL - B) False - C) 0 - D) An error message - Answer: A) NULL

Quiz Preparation Instructions

  • Each question has four answer choices.
  • The correct answer is highlighted.
  • Consider presenting the questions to the class interactively to encourage participation and discussion.
  • Adjust the complexity based on your students’ knowledge and skill levels.
  • Provide explanations for each answer after the quiz to reinforce learning.

Feel free to use or modify this quiz as needed for your PHP class! If you have specific topics you'd like to focus on or need further adjustments, just let me know!

4o mini

Is this conversation helpful so far?

 

ChatGPT can make mistakes. Check important info.

 

Post a Comment

0Comments

Post a Comment (0)