MCQs of Class 12: File Handling - Part 1

Rashmi Mishra
0

  

Multiple-Choice Questions (MCQs)

    Class 12: File Handling - Part 1

    1. What is the primary purpose of file handling in PHP?
      • A) To connect to a database
      • B) To manage data in files
      • C) To create web pages
      • D) To process user input
        Answer: B
    2. Which PHP function is used to open a file?
      • A) fopen()
      • B) readfile()
      • C) file_open()
      • D) open_file()
        Answer: A
    3. Which mode would you use with fopen() to open a file for writing only, truncating the file to zero length?
      • A) "r"
      • B) "w"
      • C) "a"
      • D) "x"
        Answer: B
    4. What does the "r" mode in fopen() signify?
      • A) Read and write
      • B) Read only
      • C) Write only
      • D) Append
        Answer: B
    5. How can you read a single line from a file in PHP?
      • A) file_get_contents()
      • B) fgets()
      • C) fread()
      • D) file_read()
        Answer: B
    6. Which function can be used to write data to a file?
      • A) fwrite()
      • B) file_put_contents()
      • C) write_file()
      • D) append_file()
        Answer: A
    7. What does the function fclose() do?
      • A) Closes the PHP script
      • B) Closes a file handle
      • C) Flushes output
      • D) Closes the web page
        Answer: B
    8. How would you append data to an existing file in PHP?
      • A) Use mode "r"
      • B) Use mode "w"
      • C) Use mode "a"
      • D) Use mode "x"
        Answer: C
    9. Which function checks whether a file exists?
      • A) file_exists()
      • B) is_file()
      • C) file_check()
      • D) check_file()
        Answer: A
    10. What does the function fgetcsv() do?
      • A) Reads a line from a file
      • B) Reads a CSV file into an array
      • C) Writes to a CSV file
      • D) Creates a CSV file
        Answer: B
    11. How do you write an array to a file in PHP?
      • A) Using fwrite() directly
      • B) Using implode() and fwrite()
      • C) Using fputcsv()
      • D) Both B and C
        Answer: D
    12. What is the return value of fread() on failure?
      • A) FALSE
      • B) NULL
      • C) 0
      • D) -1
        Answer: A
    13. Which of the following modes will allow you to read from and write to a file without truncating it?
      • A) "r+"
      • B) "w+"
      • C) "a+"
      • D) "x+"
        Answer: A
    14. What does the "b" flag indicate when opening a file in PHP?
      • A) Binary mode
      • B) Buffered mode
      • C) Both A and B
      • D) No special mode
        Answer: A
    15. How can you read all the contents of a file into a string?
      • A) file_read_contents()
      • B) readfile()
      • C) file_get_contents()
      • D) fgets()
        Answer: C
    16. Which PHP function will return the size of a file?
      • A) filesize()
      • B) file_size()
      • C) getsize()
      • D) size_of_file()
        Answer: A
    17. What does the function file_put_contents() do?
      • A) Writes data to a file
      • B) Reads data from a file
      • C) Appends data to a file
      • D) Deletes a file
        Answer: A
    18. Which of the following is NOT a file handling function in PHP?
      • A) fopen()
      • B) fclose()
      • C) file_read()
      • D) fwrite()
        Answer: C
    19. What is the purpose of the mode "x" in fopen()?
      • A) To create a new file for writing
      • B) To open an existing file
      • C) To open a file for appending
      • D) To open a file for reading
        Answer: A
    20. How can you ensure that your script does not overwrite an existing file when writing?
      • A) Use the "r" mode
      • B) Use the "a" mode
      • C) Use the "x" mode
      • D) Use the "w" mode
        Answer: C
    21. Which function can be used to delete a file?
      • A) remove()
      • B) unlink()
      • C) delete_file()
      • D) file_delete()
        Answer: B
    22. What happens if you try to open a file that does not exist in "r" mode?
      • A) The file is created
      • B) An error occurs
      • C) The function returns false
      • D) Both B and C
        Answer: D
    23. What does the UPLOAD_ERR_OK constant represent in file uploads?
      • A) File upload was successful
      • B) File upload failed
      • C) No file was uploaded
      • D) File exceeds size limit
        Answer: A
    24. Which of the following is NOT a valid error code for file uploads?
      • A) UPLOAD_ERR_INI_SIZE
      • B) UPLOAD_ERR_FORM_SIZE
      • C) UPLOAD_ERR_NO_FILE
      • D) UPLOAD_ERR_UNDEFINED
        Answer: D
    25. How do you move an uploaded file to a specific directory?
      • A) move_uploaded_file()
      • B) upload_file()
      • C) copy_uploaded_file()
      • D) save_uploaded_file()
        Answer: A
    26. What is the default permissions mode for a newly created file in PHP?
      • A) 0644
      • B) 0755
      • C) 0777
      • D) 0700
        Answer: A
    27. Which function would you use to read a file line by line until the end of the file?
      • A) fread()
      • B) fgets()
      • C) readfile()
      • D) fgetcsv()
        Answer: B
    28. How can you check if a file is writable?
      • A) is_writable()
      • B) can_write()
      • C) writable()
      • D) write_check()
        Answer: A
    29. Which PHP function is used to upload files?
      • A) upload_file()
      • B) file_upload()
      • C) move_uploaded_file()
      • D) post_file()
        Answer: C
    30. What should be done before processing file uploads?
      • A) Verify file size and type
      • B) Check server logs
      • C) Ignore all errors
      • D) Delete all existing files
        Answer: A
    31. What is the purpose of the temporary directory in file uploads?
      • A) To store uploaded files permanently
      • B) To hold files before they are moved
      • C) To delete files automatically
      • D) To check file permissions
        Answer: B
    32. Which function would you use to read the entire file into an array?
      • A) file()
      • B) readfile()
      • C) file_get_contents()
      • D) fread()
        Answer: A
    33. How can you check the MIME type of an uploaded file?
      • A) Using $_FILES['file']['type']
      • B) Using mime_type()
      • C) Using getimagesize()
      • D) Using file_type()
        Answer: A
    34. What happens if the move_uploaded_file() function fails?
      • A) An error message is displayed
      • B) The script terminates
      • C) No file is moved
      • D) All of the above
        Answer: D
    35. Which file upload error indicates that the uploaded file exceeds the maximum file size?
      • A) UPLOAD_ERR_NO_FILE
      • B) UPLOAD_ERR_INI_SIZE
      • C) UPLOAD_ERR_FORM_SIZE
      • D) UPLOAD_ERR_PARTIAL
        Answer: B
    36. What is the result of using the fputcsv() function?
      • A) Reads a CSV file
      • B) Writes data to a CSV file
      • C) Creates a CSV file
      • D) Deletes a CSV file
        Answer: B
    37. How can you check if a file is executable?
      • A) is_executable()
      • B) can_execute()
      • C) executable()
      • D) exec_check()
        Answer: A
    38. Which function can read a file character by character?
      • A) fgetc()
      • B) fread()
      • C) fgets()
      • D) file_get_contents()
        Answer: A
    39. What is the default file pointer position when a file is opened in "r" mode?
      • A) End of the file
      • B) Beginning of the file
      • C) Middle of the file
      • D) Random position
        Answer: B
    40. Which function can be used to get the last access time of a file?
      • A) fileatime()
      • B) filemtime()
      • C) filectime()
      • D) get_file_time()
        Answer: A
    41. What is the return type of file() function in PHP?
      • A) String
      • B) Array
      • C) Integer
      • D) Boolean
        Answer: B
    42. How can you create a directory in PHP?
      • A) mkdir()
      • B) create_dir()
      • C) new_directory()
      • D) make_dir()
        Answer: A
    43. What does the function rmdir() do?
      • A) Deletes a file
      • B) Renames a file
      • C) Removes an empty directory
      • D) Creates a new directory
        Answer: C
    44. Which function can retrieve the contents of a file in a single operation?
      • A) fread()
      • B) readfile()
      • C) file()
      • D) file_get_contents()
        Answer: D
    45. What is the purpose of the fopen() function?
      • A) To create a new file
      • B) To read from a file
      • C) To open a file for reading or writing
      • D) To write to a file
        Answer: C
    46. How can you safely read from a file while checking for errors?
      • A) Use try-catch blocks
      • B) Use error_reporting()
      • C) Use file_exists()
      • D) Use if statements
        Answer: A
    47. What is the maximum file size allowed for uploads by default in PHP?
      • A) 2MB
      • B) 8MB
      • C) 10MB
      • D) 20MB
        Answer: A
    48. Which function would you use to obtain information about an uploaded file?
      • A) file_info()
      • B) $_FILES
      • C) file_attributes()
      • D) get_file_info()
        Answer: B
    49. What is the correct way to check for file upload errors?
      • A) $_FILES['file']['error'] == 0
      • B) $_FILES['file']['error'] != 0
      • C) $_FILES['file']['upload'] == 0
      • D) $_FILES['file']['status'] == 'OK'
        Answer: A
    50. How can you read a specific number of bytes from a file?
      • A) fread($handle, $length)
      • B) fgets($handle, $length)
      • C) file_get_contents($length)
      • D) read_bytes($handle, $length)
        Answer: A
    51. Which function will read a file until the end of the file (EOF)?
      • A) fseek()
      • B) fread()
      • C) fgetc()
      • D) feof()
        Answer: D
    52. What is the purpose of the file_get_contents() function?
      • A) To open a file
      • B) To read a file into a string
      • C) To write to a file
      • D) To get the file metadata
        Answer: B
    53. Which PHP function can be used to create a copy of a file?
      • A) copy()
      • B) clone()
      • C) duplicate()
      • D) cp_file()
        Answer: A
    54. How do you delete a directory in PHP?
      • A) unlink()
      • B) delete_dir()
      • C) rmdir()
      • D) remove_directory()
        Answer: C
    55. What happens if you try to delete a directory that is not empty using rmdir()?
      • A) It is deleted
      • B) An error occurs
      • C) Only empty files are deleted
      • D) Nothing happens
        Answer: B
    56. What does the function fseek() do?
      • A) Reads from a file
      • B) Moves the file pointer to a specific location
      • C) Writes to a file
      • D) Closes the file
        Answer: B
    57. Which of the following modes is used for reading and writing in binary mode?
      • A) "r+"
      • B) "w+b"
      • C) "a+b"
      • D) "b+"
        Answer: D
    58. How do you read an entire file as an array of lines?
      • A) file()
      • B) readfile()
      • C) file_lines()
      • D) read_lines()
        Answer: A
    59. What is the default behavior of file_get_contents() when a file cannot be read?
      • A) Returns an empty string
      • B) Returns false
      • C) Throws an exception
      • D) Returns NULL
        Answer: B
    60. How can you check if a directory exists in PHP?
      • A) is_dir()
      • B) dir_exists()
      • C) check_dir()
      • D) exists_dir()
        Answer: A
    61. Which of the following is true about the file handling functions in PHP?
      • A) They all return the same data type.
      • B) They are not affected by permissions.
      • C) They can throw exceptions if not handled properly.
      • D) They require an active database connection.
        Answer: C
    62. What does the function getcwd() return?
      • A) Current working directory
      • B) Current file size
      • C) Current user permissions
      • D) Current file name
        Answer: A
    63. How do you get the last modification time of a file?
      • A) filemtime()
      • B) getmtime()
      • C) last_modified()
      • D) modified_time()
        Answer: A
    64. Which of the following can be used to upload multiple files?
      • A) $_FILES with array notation
      • B) $_POST
      • C) $_GET
      • D) All of the above
        Answer: A
    65. What does the function stream_get_contents() do?
      • A) Reads from a file stream
      • B) Writes to a file stream
      • C) Copies a file stream
      • D) Deletes a file stream
        Answer: A
    66. How can you create a file with specific permissions?
      • A) fopen() with mode
      • B) chmod() after creation
      • C) Both A and B
      • D) None of the above
        Answer: C
    67. What is the purpose of file_info() function?
      • A) To get metadata of a file
      • B) To write to a file
      • C) To delete a file
      • D) To read a file
        Answer: A
    68. Which PHP directive controls the maximum upload file size?
      • A) upload_max_filesize
      • B) post_max_size
      • C) max_execution_time
      • D) Both A and B
        Answer: D
    69. How can you securely upload files in PHP?
      • A) By validating file types
      • B) By limiting file size
      • C) By using HTTPS
      • D) All of the above
        Answer: D
    70. What will happen if you try to write to a file opened in "r" mode?
      • A) The file will be truncated
      • B) An error will occur
      • C) The data will be appended
      • D) The file will be created
        Answer: B
    71. Which function can be used to get the file type in PHP?
      • A) filetype()
      • B) get_file_type()
      • C) mime_content_type()
      • D) Both A and C
        Answer: D
    72. How can you handle file upload errors?
      • A) Check $_FILES['file']['error']
      • B) Use try-catch
      • C) Use error_reporting
      • D) Ignore errors
        Answer: A
    73. What does the function file_exists() return?
      • A) True if the file exists
      • B) False if the file exists
      • C) NULL if the file exists
      • D) An error if the file exists
        Answer: A
    74. How do you check if a file is readable?
      • A) is_readable()
      • B) can_read()
      • C) read_check()
      • D) check_read()
        Answer: A
    75. What will be the output of the following code if the file does not exist?

    php

    Copy code

    echo file_get_contents('nonexistent.txt');

      • A) ""
      • B) NULL
      • C) false
      • D) An error message
        Answer: C
    1. Which PHP function can read the entire file into a string?
      • A) file()
      • B) file_get_contents()
      • C) fread()
      • D) readfile()
        Answer: B
    2. How can you move a file from one location to another?
      • A) move_file()
      • B) copy() then unlink()
      • C) rename()
      • D) Both B and C
        Answer: D
    3. What is the purpose of the function feof()?
      • A) Checks if the end of the file has been reached
      • B) Closes a file
      • C) Reads a line from a file
      • D) Gets the file pointer position
        Answer: A
    4. Which function can be used to get an array of files in a directory?
      • A) scandir()
      • B) get_files()
      • C) list_files()
      • D) file_array()
        Answer: A
    5. How can you write data to a file without truncating it?
      • A) Use "a" mode in fopen()
      • B) Use "r" mode in fopen()
      • C) Use "w" mode in fopen()
      • D) None of the above
        Answer: A
    6. What does the function basename() return?
      • A) The name of the file without path
      • B) The full path of the file
      • C) The directory of the file
      • D) The file extension
        Answer: A
    7. Which of the following is NOT a file permission in PHP?
      • A) Read
      • B) Write
      • C) Execute
      • D) Modify
        Answer: D
    8. How can you set file permissions in PHP?
      • A) chmod()
      • B) set_permissions()
      • C) file_permissions()
      • D) Both A and C
        Answer: A
    9. What is the return type of get_file_contents() when a file does not exist?
      • A) An empty string
      • B) false
      • C) NULL
      • D) An error message
        Answer: B
    10. What is the result of trying to open a non-existent file in "r" mode?
      • A) The file is created
      • B) An error is returned
      • C) The operation succeeds but returns NULL
      • D) The pointer is moved to EOF
        Answer: B
    11. Which function can delete a file in PHP?
      • A) unlink()
      • B) remove()
      • C) delete_file()
      • D) drop()
        Answer: A
    12. What is the correct way to open a file for writing and truncating it?
      • A) fopen('file.txt', 'r')
      • B) fopen('file.txt', 'w')
      • C) fopen('file.txt', 'a')
      • D) fopen('file.txt', 'r+')
        Answer: B
    13. How can you append data to a file?
      • A) fopen() with "w" mode
      • B) fopen() with "a" mode
      • C) fopen() with "r" mode
      • D) fopen() with "r+" mode
        Answer: B
    14. Which function can check if a file is writable?
      • A) is_writable()
      • B) can_write()
      • C) writable_check()
      • D) check_writable()
        Answer: A
    15. What is the effect of using fclose() on an open file pointer?
      • A) It closes the file pointer
      • B) It deletes the file
      • C) It saves changes to the file
      • D) It resets the file pointer position
        Answer: A
    16. Which function can check if a file is executable?
      • A) is_executable()
      • B) can_execute()
      • C) exec_check()
      • D) check_executable()
        Answer: A
    17. How can you prevent file upload vulnerabilities?
      • A) Validate file types
      • B) Limit file size
      • C) Store files outside of the web root
      • D) All of the above
        Answer: D
    18. What does the function pathinfo() return?
      • A) Information about the path of a file
      • B) The contents of a file
      • C) The file permissions
      • D) The file size
        Answer: A
    19. How do you read a file line by line?
      • A) Using fgets()
      • B) Using fgetc()
      • C) Using fread()
      • D) Using file_get_contents()
        Answer: A
    20. What will happen if you call unlink() on a file that does not exist?
      • A) Nothing happens
      • B) An error is thrown
      • C) The system crashes
      • D) A warning is displayed
        Answer: B
    21. How do you copy a file?
      • A) copy('source.txt', 'dest.txt')
      • B) duplicate('source.txt', 'dest.txt')
      • C) clone('source.txt', 'dest.txt')
      • D) move('source.txt', 'dest.txt')
        Answer: A
    22. What is the use of the rewind() function?
      • A) Moves the pointer to the start of the file
      • B) Closes the file
      • C) Writes to the file
      • D) Reads from the file
        Answer: A
    23. How can you check the size of a file?
      • A) filesize()
      • B) get_file_size()
      • C) file_size()
      • D) size_of_file()
        Answer: A
    24. Which function can be used to change the name of a file?
      • A) rename()
      • B) move()
      • C) change_name()
      • D) update_file()
        Answer: A
    25. What does the file_get_contents() function return if successful? - A) An array of lines - B) A string of file contents - C) A boolean value - D) An error message
      Answer: B

     

    Post a Comment

    0Comments

    Post a Comment (0)