Unit 5 PHP MCQ
Unit 5 PHP MCQ
a) include 'header.php';
b) require 'header.php'; (✔)
c) require_once();
d) import 'header.php';
Which keyword does not stop script execution when the file is missing?
a) require
b) include (✔)
c) require_once
d) All of the above
Which construct returns true on success and false on failure while continuing execution?
a) include (✔)
b) require
c) require_once
d) None
Which wrapper allows file inclusion over HTTP when allow_url_include is enabled?
a) file://
b) ftp://
c) http:// (✔)
d) php://input
Which magic constant reveals the absolute filename of the current script?
a) __DIR__
b) __FILE__ (✔)
c) __LINE__
d) __PATH__
Sensitive config files should live outside web-root because they might:
a) Cache forever
b) Be downloaded as plain text if mis-served (✔)
c) Upsize DNS
d) Disable buffering
a) chmod()
b) chown()
c) fopen()
d) umask()
Answer: a) chmod()
a) 0777
b) 0755
c) 0644
d) 0700
Answer: b) 0755
(If ownership is www-data:www-data, 0755 grants write only to the owner, satisfying security
best-practices.)
4. What does the leading zero in the octal literal 0750 signify in a chmod() call?
5. Which umask setting should you apply before creating a log file with fopen() so
that the resulting file has permissions rw-r----- (0640)?
a) umask(000)
b) umask(022)
c) umask(027)
d) umask(002)
Answer: c) umask(027)
6. Using fileperms() returns 16877 for a directory. What symbolic permission string
does this represent?
a) drwxr-xr-x
b) drwxrwxrwx
c) -rw-r--r--
d) drwx------
Answer: a) drwxr-xr-x
(16877 decimal = 040755 octal; leading 04 indicates directory.)
7. Which of these is NOT a valid reason for getting a “Permission denied” error
in PHP when writing to a file?
Answer: b) It grants write access to every user on the server, posing security risks.
a) 0644
b) 0755
c) 0700
d) 0775
Answer: a) 0644
(No execute bit for anyone, read for web server to serve the image.)
10. When you run PHP via CLI as your normal user, touch("notes.txt"); creates the
file with rw-rw-r--. Which umask is most likely in effect?
a) 0002
b) 0022
c) 0277
d) 0777
Answer: a) 0002
1. Which PHP function is used to open a file?
a) open_file()
b) file_open()
c) fopen()
d) open()
Answer: c) fopen()
3. Which file access mode in fopen() opens a file for writing only, placing the file pointer at the
end of the file?
a) w
b) r+
c) a
d) x
Answer: c) a
4. Which file access mode in fopen() opens a file for reading and writing; creates a new file if it
doesn't exist, otherwise overwrites the existing file?
a) r+
b) w+
c) a+
d) x+
Answer: b) w+
a) close_file()
b) file_close()
c) fclose()
d) close()
Answer: c) fclose()
6. Which PHP function reads the entire content of a file into a string?
a) readfile()
b) fread()
c) file_get_contents()
d) get_file()
Answer: c) file_get_contents()
a) fgets()
b) fgetline()
c) freadln()
d) readline()
Answer: a) fgets()
8. Which PHP function reads a specified number of bytes from a file pointer?
a) fread()
b) freadbytes()
c) fgetbytes()
d) readbytes()
Answer: a) fread()
a) fwrite()
b) file_write()
c) write_file()
d) fput()
Answer: a) fwrite()
10. Which PHP function writes the entire content of a string to a file?
a) fwrite_all()
b) file_put_contents()
c) put_file()
d) string_to_file()
Answer: b) file_put_contents()
a) file_exists()
b) is_exist()
c) exist_file()
d) check_file()
Answer: a) file_exists()
a) delete_file()
b) remove_file()
c) unlink()
d) erase()
Answer: c) unlink()
a) change_name()
b) rename_file()
c) move_file()
d) rename()
Answer: d) rename()
a) file_copy()
b) copy_file()
c) duplicate_file()
d) copy()
Answer: d) copy()
a) filesize()
b) size_file()
c) get_file_size()
d) file_length()
Answer: a) filesize()
b) is_directory()
c) check_dir()
d) dir_exists()
Answer: a) is_dir()
a) make_dir()
b) create_dir()
c) mkdir()
d) new_dir()
Answer: c) mkdir()
a) remove_dir()
b) delete_dir()
c) rmdir()
d) erase_dir()
Answer: c) rmdir()
19. What function returns an array of files and directories within a specified path?
a) get_files()
b) list_files()
c) scandir()
d) read_dir()
Answer: c) scandir()
20. Which function changes the permissions of a file or directory?
a) change_perms()
b) set_permissions()
c) chmod()
d) file_perms()
Answer: c) chmod()
21. What does the acronym CSV stand for in the context of file input/output?
a) readcsv()
b) parse_csv()
c) fgetcsv()
d) getcsv()
Answer: c) fgetcsv()
23. When reading a CSV file with fgetcsv(), what is the default delimiter?
a) Semicolon (;)
b) Tab (\t)
c) Comma (,)
d) Space ()
Answer: c) Comma (,)
24. What function is often used to output data in a structured format that can be easily read by
other applications or stored in files?
a) print_r()
b) var_dump()
c) json_encode()
d) serialize()
Answer: c) json_encode()
25. Which function is used to decode a JSON string back into a PHP variable?
a) json_decode()
b) decode_json()
c) unjson()
d) parse_json()
Answer: a) json_decode()
a) To format a file.
c) To find a file.
27. Which lock type in flock() acquires an exclusive lock for writing?
a) LOCK_SH
b) LOCK_EX
c) LOCK_UN
d) LOCK_NB
Answer: b) LOCK_EX
c) Releases a lock.
29. What function is used to get information about a file, such as its access time, modification
time, etc.?
a) fileinfo()
b) file_info()
c) stat()
d) get_file_info()
Answer: c) stat()
30. Which of the following is NOT a valid file access mode for fopen()?
a) r
b) w
c) z
d) a
Answer: c) z
• Which command is commonly used in the command line to create a new directory?
a) cd
b) rm
c) mkdir
d) ls
Answer: c) mkdir
a) Move Directory
b) Make Directory
c) Modify Directory
d) Manage Directory
• To create a directory named "reports" in the current working directory, which command would
you use?
a) cd reports
b) rm reports
c) mkdir reports
d) ls reports
• Which option for the mkdir command allows you to create parent directories if they don't
exist?
a) -r
b) -f
c) -p
d) –v
Answer: c) -p
• If you want to create a directory named "data" inside a directory named "projects" (and
"projects" might not exist), which command would you use?
a) mkdir data/projects
b) mkdir -p data/projects
c) mkdir projects/data
d) mkdir -p projects/data
a) rd
b) del
c) rmdir
d) erase
Answer: c) rmdir
• What is the primary difference between rmdir and rm -r when deleting directories?
• To delete an empty directory named "temp", which command would you use?
a) rm temp
b) rm -r temp
c) rmdir temp
d) del temp
• To recursively delete a directory named "backup" and all its contents (files and subdirectories),
which command would you use?
a) rmdir backup
b) rm backup
c) rm -r backup
d) del /f /q backup
Answer: c) rm -r backup
a) Remove forcefully
b) Remove recursively
c) Remove silently
a) md
b) rd
c) cd
d) pwd
Answer: c) cd
a) Change Directory
b) Current Directory
c) Create Directory
d) Command Directory
• To move from your current directory to a subdirectory named "images", which command
would you use?
a) cd ..
b) cd /images
c) cd images
d) move images
Answer: c) cd images
• To move one level up in the directory hierarchy, which command would you use?
a) cd .
b) cd /
c) cd ..
d) cd ../..
Answer: c) cd ..
• To go directly to your home directory from any location in the file system, you can often use
the command:
a) cd /home
b) cd ~
c) cd .
d) cd root
Answer: b) cd ~
• What does the tilde symbol (~) typically represent in command-line environments?
a) ls
b) pwd
c) cd
d) dir
Answer: b) pwd
a) cd documents/downloads
b) cd ../downloads
c) cd /home/user/downloads
d) Both b and c
• Which of the following commands would create a directory named "new_folder" and then
move you into it?
a) cd new_folder
c) mkdir -p new_folder
• Which command would you use to delete a directory named "old_files" even if it contains files
and subdirectories, without prompting for confirmation?
a) rmdir old_files
b) rm -i -r old_files
c) rm -f -r old_files
d) del old_files /q
Answer: c) rm -f -r old_files
• What is the purpose of the -v (verbose) option in the mkdir command?
• Suppose you are in the /usr/local directory. Which command would take you to the root
directory (/)?
a) cd .
b) cd ..
c) cd ../..
d) cd /
Answer: d) cd /
• You are in the directory /home/user/data/images. Which command would take you directly
to /home/user?
a) cd ..
b) cd ../..
c) cd /home/user
d) cd ../../..
Answer: b) cd ../..
• Which of the following is NOT a standard command for working with directories in most
command-line interfaces?
a) mkdir
b) rmdir
c) cd
d) movedir
Answer: d) movedir
• What happens if you try to use rmdir on a directory that is not empty?
d) It will move the contents to the parent directory and then delete the original directory.
• You want to create a directory named "logs" and then immediately navigate into it. Which of
the following sequences of commands would achieve this?
• Which HTTP method is essential for submitting file uploads via HTML forms?
a) GET
b) POST
c) PUT
d) DELETE
Answer: b) POST
• What enctype attribute is mandatory in the <form> tag for file uploads?
a) application/x-www-form-urlencoded
b) text/plain
c) multipart/form-data
d) application/octet-stream
Answer: c) multipart/form-data
a) $_GET
b) $_POST
c) $_SESSION
d) $_FILES
Answer: d) $_FILES
• To access the original name of an uploaded file (from an input named userfile), you would
use:
a) $_FILES['userfile']['name']
b) $_POST['userfile_name']
c) $_GET['userfile']
d) $_FILE['name']['userfile']
Answer: a) $_FILES['userfile']['name']
• Which key in the $_FILES array holds the temporary location of the uploaded file on the
server?
a) name
b) size
c) type
d) tmp_name
Answer: d) tmp_name
• Which PHP function is used to move an uploaded file from its temporary location to a
permanent directory?
a) copy()
b) rename()
c) move_uploaded_file()
d) file_put_contents()
Answer: c) move_uploaded_file()
• What is the return value of move_uploaded_file() upon successfully moving the file?
c) true
d) false
Answer: c) true
• A crucial security practice when handling file uploads is to verify the file's:
a) Temporary name
b) Original name
• Which PHP configuration directives often limit the maximum size of uploaded files?
a) The webroot
d) A database
• What is a common method to prevent overwriting files with the same name during uploads?
• When handling multiple file uploads from an input with multiple, how do you typically
access the individual file information in $_FILES (for an input named files[])?
• A significant security risk associated with using the original uploaded filename for storage is:
Answer: b) To organize code into reusable and manageable units called objects.
a) Procedural programming
b) Functional decomposition
c) Encapsulation
d) Imperative programming
Answer: c) Encapsulation
a) An instance of an object.
a) class
b) object
c) new
d) function
Answer: a) class
a) class
b) object
c) new
d) instanceof
Answer: c) new
Answer: c) Functions that belong to a class and can operate on its objects.
• Which of the following is NOT a common visibility modifier for class properties and methods
in PHP?
a) public
b) private
c) protected
d) internal
Answer: d) internal
c) Access from anywhere (within the class, its parent classes, and outside the class).
d) No access at all.
Answer: c) Access from within the class and its parent classes.
b) The mechanism by which a class can acquire the properties and methods of another class.
d) The ability of objects of different classes to respond to the same method call in their own way.
Answer: b) The mechanism by which a class can acquire the properties and methods of another
class.
• Which keyword is used in PHP to implement inheritance?
a) implements
b) extends
c) uses
d) inherits
Answer: b) extends
c) The ability of objects of different classes to respond to the same method call in their own way.
Answer: c) The ability of objects of different classes to respond to the same method call in their
own way.
b) The mechanism of bundling data (properties) and the methods that operate on that data within
a single unit (class).
Answer: b) The mechanism of bundling data (properties) and the methods that operate on that
data within a single unit (class).
c) A class that can have abstract methods (declared but not implemented) and cannot be directly
instantiated.
Answer: c) A class that can have abstract methods (declared but not implemented) and cannot be
directly instantiated.
a) static
b) final
c) abstract
d) interface
Answer: c) abstract
b) A blueprint for classes, defining a set of methods that a class implementing the interface must
define.
Answer: b) A blueprint for classes, defining a set of methods that a class implementing the
interface must define.
b) implements
c) uses
d) inherits
Answer: b) implements