php programming banner

PHP Multiple Choice Questions (MCQs) and Answers

Master PHP with Practice MCQs. Explore our curated collection of Multiple Choice Questions. Ideal for placement and interview preparation, our questions range from basic to advanced, ensuring comprehensive coverage of PHP. Begin your placement preparation journey now!

Q91

Q91 What is Cross-Site Request Forgery (CSRF) and how can it be prevented in PHP?

A

A type of attack where a malicious website performs actions on behalf of a user on another website

B

Using SSL certificates

C

Validating user input

D

Using tokens in forms

Q92

Q92 How can htmlspecialchars() function in PHP help in preventing security risks?

A

By encrypting data

B

By suppressing error messages

C

By converting special characters to HTML entities, thus preventing XSS attacks

D

By validating user input

Q93

Q93 In PHP, how can you securely handle file uploads to prevent malicious files from being uploaded?

A

By checking the file extension only

B

By limiting the file size

C

By validating the MIME type and checking file extensions, and storing files outside the web directory

D

By renaming files upon upload

Q94

Q94 Identify the security flaw in this PHP code snippet:
if (isset($_GET['user_id'])) {
$user_id = $_GET['user_id']; // Perform database query }

A

The user input is not sanitized before being used

B

No error in the code

C

The user ID should be stored in a session

D

The user ID should be encrypted

Q95

Q95 Spot the vulnerability in this PHP session handling:
session_start();
if (!isset($_SESSION['user'])) {
header('Location: login.php');
}

A

The session is not regenerated upon login

B

The session ID is not stored securely

C

There is no vulnerability in this code

D

The header location is not absolute

Q96

Q96 What is the primary purpose of cookies in web development?

A

To store server data

B

To store client-side, persistent user data

C

To improve network speed

D

To encrypt data

Q97

Q97 How are PHP sessions different from cookies?

A

Sessions are stored on the client-side, while cookies are stored on the server-side

B

Sessions and cookies are the same

C

Sessions are stored on the server-side, while cookies are stored on the client-side

D

Sessions encrypt data automatically

Q98

Q98 In PHP, what function is used to start a session?

A

session()

B

session_start()

C

start_session()

D

begin_session()

Q99

Q99 What is a session hijacking attack?

A

An attack where the attacker steals the session cookie

B

An attack where the server hijacks a client's session

C

A brute force attack

D

A SQL injection attack

Q100

Q100 How can you increase the security of PHP sessions?

A

By using SSL and storing sessions in a database

B

By increasing the session timeout

C

By disabling cookies

D

By using longer session IDs

Q101

Q101 What will be the output of the following PHP code?
setcookie("user", "John Doe", time() + 3600); echo $_COOKIE["user"];

A

John Doe

B

An error

C

Nothing

D

The current time + 3600

Q102

Q102 In PHP, how can you delete a cookie?

A

By setting its value to null

B

By using the delete_cookie() function

C

By setting its expiration date in the past

D

By unsetting it in $_COOKIE

Q103

Q103 Consider this PHP code:
session_start();
$_SESSION['user'] = 'Alice'; session_destroy();
echo $_SESSION['user'];
What is output?

A

Alice

B

An error

C

Nothing

D

The session ID

Q104

Q104 Identify the error in this PHP cookie code:
setcookie("user", "Alice", time() - 3600);

A

The cookie is being set with a past expiration time

B

The cookie value is incorrect

C

There is no error in the code

D

The cookie name is incorrect

Q105

Q105 Spot the mistake in this PHP session handling code:
session_start(); $_SESSION = array(); session_destroy();

A

Not using session_unset() before session_destroy()

B

Incorrect use of session_start()

C

No mistake

D

The session array should not be emptied

Q106

Q106 What is the main advantage of using a PHP framework like Laravel or Symfony?

A

Standardized coding practices

B

Faster execution of scripts

C

Automatic HTML parsing

D

Increased PHP script size

Q107

Q107 How does the MVC (Model-View-Controller) architecture in frameworks like Laravel and Symfony benefit web application development?

A

It enhances the UI/UX design

B

It speeds up the database read/write operations

C

It separates business logic, presentation, and data access

D

It reduces server load

Q108

Q108 In Laravel, what is a "Route" used for?

A

To optimize database queries

B

To create links to different pages

C

To define URLs and their corresponding actions in the application

D

To manage user sessions

Q109

Q109 Given a Laravel blade template, how can you display a variable passed from a controller?

A

{{ $variable }}

B

<php echo $variable; ?>

C

@variable

D

{!! $variable !!}

Q110

Q110 In Symfony, what is the purpose of a "Service Container"?

A

To manage user authentication

B

To store and retrieve session data

C

To automatically inject dependencies into classes

D

To handle database migrations

Q111

Q111 Identify the issue in this Symfony controller method:
public function index() {
return $this->render('index.html.twig');
}

A

The method doesn't specify a route

B

The template file extension should be .blade.php

C

The method must return a Response object

D

No issue in the code

Q112

Q112 Spot the mistake in this Laravel Eloquent query:
$users = User::where('age', '>', 30)->get();

A

The get() method is used incorrectly

B

The where clause is incorrect

C

There is no mistake

D

The model User is likely not defined

Q113

Q113 What is a RESTful API in the context of web services?

A

An API that uses XML for data exchange

B

An API that allows only GET requests

C

An API designed around the principles of REST (Representational State Transfer)

D

An API that requires SOAP protocol

Q114

Q114 What is the primary role of JSON in web services developed with PHP?

A

To structure the layout of a web page

B

To act as a query language for databases

C

To format and transfer data between client and server

D

To encrypt data during transmission

Q115

Q115 How can you send a JSON response from a PHP script?

A

json_encode()

B

json_response()

C

header("Content-Type: application/json"); echo json_encode($data);

D

send_json($data);

Q116

Q116 In PHP, how can you consume a RESTful API using cURL?

A

Using file_get_contents() with the API URL

B

Using the cURL functions to make a request and receive a response

C

Using JSON encoding and decoding

D

Using simple XML loading functions

Q117

Q117 Identify the common error in this API request code in PHP:
$response = file_get_contents('https://api.example.com/data');

A

Not checking for a false response

B

Incorrect URL format

C

Not setting a user agent

D

The function file_get_contents cannot be used for API requests

Q118

Q118 Spot the mistake in this PHP code for sending a POST request using cURL:
curl_setopt($ch, CURLOPT_POST, true); curl_exec($ch);

A

Not setting the CURLOPT_POSTFIELDS option

B

The CURLOPT_POST option is incorrectly used

C

curl_exec() is used incorrectly

D

No mistake in the code

Q119

Q119 What is the significance of using the PHP Data Objects (PDO) extension for database access?

A

It provides faster query execution

B

It is specific to MySQL databases

C

It offers a secure, consistent way to interact with different databases using the same interface

D

It automatically creates database schemas

Q120

Q120 Why is it recommended to avoid using the PHP closing tag ?> at the end of a file?

A

To prevent syntax errors

B

To improve performance

C

To prevent accidental whitespace or new lines after the tag, which can cause output buffering issues

D

To conform to PSR standards

ad verticalad vertical
ad