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 Identify the issue in this PHP code:
$str = 'Hello'; echo $str[10];

A

Trying to access an undefined index

B

Syntax error

C

No error, it will print 'Hello'

D

It will print a space

Q62

Q62 Find the error in the following PHP code:
$arr = array("one", "two", "three");
foreach ($ar as $val) {
echo $val;
}

A

Typo in the variable name inside foreach

B

Syntax error in array declaration

C

No error in the code

D

Error in echo statement

Q63

Q63 What keyword is used to declare a class in PHP?

A

class

B

object

C

new

D

struct

Q64

Q64 What is the purpose of the __construct() method in a PHP class?

A

To configure a server

B

To create an object from the class

C

To initialize an object's properties

D

To delete an object

Q65

Q65 In PHP, which keyword is used to inherit a class from another class?

A

extends

B

inherits

C

uses

D

implements

Q66

Q66 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

Q67

Q67 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

Q68

Q68 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

Q69

Q69 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

Q70

Q70 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

Q71

Q71 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

Q72

Q72 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

Q73

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

A

@

B

#

C

$

D

!

Q74

Q74 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

Q75

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

A

set_error_handler()

B

error_report()

C

handle_error()

D

trigger_error()

Q76

Q76 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

Q77

Q77 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

Q78

Q78 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

Q79

Q79 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

Q80

Q80 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

Q81

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

A

fopen()

B

open_file()

C

file_open()

D

open()

Q82

Q82 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+

Q83

Q83 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

Q84

Q84 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

Q85

Q85 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

Q86

Q86 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

Q87

Q87 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

Q88

Q88 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

Q89

Q89 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

Q90

Q90 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

ad verticalad vertical
ad