an HCL GUVI product

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!

Q61

Q61 What does the final keyword signify when applied to a method in PHP?

A

The method cannot be overridden

B

The method is the last in the class

C

The method is static

D

The method is the most important in the class

Q62

Q62 How is a static property accessed in a PHP class?

A

With the new keyword

B

With the -> operator

C

Using the class name and ::

D

Using the $this keyword

Q63

Q63 What will be the output of the following PHP code?
class Test {
public $prop = 'Hello';
}
$obj = new Test();
echo $obj->prop;

A

Hello

B

Test

C

prop

D

Error

Q64

Q64 Consider the PHP class:
class Circle {
private $radius;
public function __construct($r) {
$this->radius = $r;
}
public function getArea() {
return pi() *
$this->radius * $this->radius; }
}
$circle = new Circle(3);
echo $circle->getArea();

A

The area of a circle with radius 3

B

Syntax error

C

No output

D

Error due to private property access

Q65

Q65 What is the result of calling the following PHP code?
class Animal {
protected $sound = "No sound";
public function makeSound() {
return $this->sound;
}
}
class Dog extends Animal {
protected $sound = "Bark";
}
$dog = new Dog();
echo $dog->makeSound();

A

Bark

B

No sound

C

Error

D

Dog

Q66

Q66 Identify the error in the following PHP class definition:
class Car {
function __construct() {
$this->model = '';
}
public function getModel() {
return model;
}
}

A

Undefined variable model

B

Syntax error in constructor

C

Missing property declaration

D

No error in the code

Q67

Q67 Find the mistake in this PHP code involving object-oriented principles:
class Book { public $title; public function __construct(title) { $this->title = title; } }

A

Missing $ before parameter in constructor

B

Syntax error in property declaration

C

No error in the code

D

Error in method definition

Q68

Q68 In PHP, which operator is used at the beginning of an expression to suppress error messages that it may generate?

A

@

B

#

C

$

D

!

Q69

Q69 What is the purpose of the try and catch block in PHP?

A

To detect and handle syntax errors

B

To manage program flow

C

To handle exceptions and errors

D

To debug code

Q70

Q70 Which PHP function is used to set a custom error handler?

A

set_error_handler()

B

error_report()

C

handle_error()

D

trigger_error()

Q71

Q71 What is the difference between exceptions and errors in PHP?

A

Exceptions can be caught and handled, errors cannot

B

Errors are for system-level issues, exceptions are for script issues

C

Exceptions are fatal, errors are not

D

There is no difference

Q72

Q72 What will happen if an exception is not caught in PHP?

A

The script will continue running

B

A fatal error will occur

C

The exception will be ignored

D

The script will pause

Q73

Q73 Given the code:
$num = -1;
try {
if($num < 0) {
throw new Exception("Negative number");
}
} catch (Exception $e) {
echo $e->getMessage();
}
What will be output?

A

Negative number

B

Nothing

C

Exception in script

D

Error

Q74

Q74 Identify the error in this PHP error handling code:
try {
//code } catch() {
echo "Error occurred";
}

A

Empty catch block

B

Missing exception type in catch block

C

Syntax error in try block

D

No error in the code

Q75

Q75 Spot the mistake in this PHP exception handling:
try {
$value = 10/0;
}
catch (Exception $e) {
echo "Caught exception: ", $e->getMessage();
}

A

Dividing by zero is not caught

B

Incorrect message concatenation

C

No mistake, it will catch and display the exception

D

The variable $e is not defined

Q76

Q76 In PHP, which function is used to open a file?

A

fopen()

B

open_file()

C

file_open()

D

open()

Q77

Q77 Which file mode in PHP can be used to open a file for both reading and writing, and places the file pointer at the beginning?

A

r+

B

w+

C

a+

D

x+

Q78

Q78 What does the PHP file_get_contents() function do?

A

Reads the entire file into a string

B

Deletes the specified file

C

Creates a new file

D

Writes content to a file

Q79

Q79 In PHP, how do you check if a file exists and is readable?

A

Using is_readable() and file_exists()

B

Using file_exists()

C

Using can_read()

D

Using fopen() and checking if it's false

Q80

Q80 What is the result of the following PHP code?
$file = fopen("test.txt", "w"); fwrite($file, "Hello, World!"); fclose($file);

A

A new file named "test.txt" is created with "Hello, World!" inside

B

An error occurs

C

Nothing happens

D

The file "test.txt" is deleted

Q81

Q81 In PHP, what does the file_put_contents() function do when the FILE_APPEND flag is used?

A

Replaces the content of the file

B

Deletes the file and creates a new one

C

Appends content to the end of the file

D

Creates a new file

Q82

Q82 Identify the error in this PHP file handling code:
$file = fopen("nonexistent.txt", "r"); fwrite($file, "Data"); fclose($file);

A

The file does not exist

B

Incorrect mode for fwrite

C

Syntax error

D

No error in the code

Q83

Q83 Spot the mistake in this PHP code for reading a file:
$file = fopen("file.txt", "r");
$data = fread($file, filesize("file.txt"));
fclose($file); echo $data;

A

There is no mistake

B

Missing error handling for file opening

C

Incorrect use of filesize()

D

The file is not closed properly

Q84

Q84 What is the primary purpose of using prepared statements in PHP?

A

To optimize query execution speed

B

To improve code readability

C

To protect against SQL injection attacks

D

To handle large datasets

Q85

Q85 In PHP, what security measure should be implemented to protect sensitive data, like passwords, stored in a database?

A

Storing them as plain text with unique identifiers

B

Encrypting them using reversible encryption

C

Hashing them using a cryptographic hash function

D

Compressing them before storage

Q86

Q86 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

Q87

Q87 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

Q88

Q88 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

Q89

Q89 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

Q90

Q90 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