wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

PHP 8 OOP Quiz

Total questions: 38

Worksheet time: 19mins

Name
Class
Date
1.

What is one of the main advantages of PHP 8 over its previous versions?

a)

Reduced file size

b)

JIT Compiler for improved performance

c)

Better browser support

d)

New file system

2.

Which feature allows passing arguments based on parameter names in PHP 8?

a)

Match Expressions

b)

Named Arguments

c)

Union Types

d)

Default Parameters

3.

What is a class in PHP OOP?

a)

A variable

b)

A data type

c)

A blueprint for creating objects

d)

A loop structure

4.

What keyword is used to create a class instance?

a)

static

b)

this

c)

new

d)

get

5.

What is the output of this code? $car = new Car("red", "Toyota"); echo $car->getDescription();

a)

Toyota red car

b)

This car is a red Toyota.

c)

Car object created

d)

red Toyota

6.

What is the purpose of the __construct method in a class?

a)

To delete properties

b)

To initialize object properties

c)

To print the class

d)

To inherit another class

7.

Given this code snippet: function foo(int|float $value): int|float { return $value; } echo foo(3.5); //What will be the output?

a)

int

b)

3.5

c)

Error

d)

Float

8.

Why would a developer use Union Types in PHP 8?

a)

To simplify loops

b)

To handle multiple databases

c)

To allow variables to accept multiple types

d)

To replace arrays

9.

Consider this class: class Car { public string $color; public function getColor(): string { return $this->color; } } //Which statement correctly assigns and accesses the color?

a)

$car = Car("red"); echo getColor();

b)

$car->color = "red"; echo $car->getColor();

c)

Car.color = "red"; getColor();

d)

color = red; echo $car.getColor();

10.

You need to create multiple objects with different properties from a single template. Which concept applies?

a)

Function declaration

b)

Array creation

c)

Class instantiation

d)

Global variable assignment

11.

What will this return? $car = new Car("blue", "Ford"); echo $car->getDescription();

a)

blue Ford

b)

Description of car

c)

This car is a blue Ford.

d)

Ford blue

12.

Why is the $this keyword used in a class method?

a)

To access constants

b)

To refer to the current object instance

c)

To declare variables

d)

To include other files

13.

Given the class definition: class Dog { public function bark() { return "Woof!"; } } //How do you call the bark() method?

a)

Dog->bark();

b)

new Dog.bark();

c)

$dog = new Dog(); echo $dog->bark();

d)

bark();

14.

Why would you use a method inside a class instead of a regular function?

a)

To slow down the execution

b)

For random output

c)

To bind behavior to an object

d)

For PHP backward compatibility

15.

In OOP, which of the following best describes an object?

a)

A variable with static scope

b)

A function with global access

c)

An instance of a class with properties and methods

d)

A loop with reusable steps

16.

You want to represent a book with title and author, and print a message like "Title by Author". What OOP elements should you use?

a)

Functions and loops

b)

Arrays and echo

c)

Class with properties and a method

d)

Superglobals

17.

What does OOP stand for in programming?

a)

Original Output Process

b)

Object-Oriented Programming

c)

Output Order Parameter

d)

Organized Object Program

18.

What is the purpose of a method inside a class?

a)

To store data

b)

To create loops

c)

To define actions/behaviors

d)

To write HTML

19.

Which of the following best describes a property in a class?

a)

A function

b)

A condition

c)

A variable that belongs to an object

d)

A constructor

20.

Find the missing code shown below color = $color; $this->model = $model; } public function getDescription(): string { return "This car is a $this->color $this->model."; } } $car = new Car("red", "Toyota"); echo $car->(); // Output: This car is a red Toyota. ?>

a)

$car

b)

Echo

c)

setDescription

d)

getDescription

21.

What is inheritance in PHP OOP?

a)

A way to loop through arrays

b)

A class inheriting properties and methods from another class

c)

A function calling another function

d)

An object creating another object

22.

What is the keyword used in PHP to inherit from a parent class?

a)

extend

b)

implements

c)

extends

d)

use

23.

What is the purpose of the parent::__construct() statement?

a)

To create a new object

b)

To call the parent class constructor

c)

To delete the child class

d)

To declare a function

24.

In polymorphism, objects are treated as what type?

a)

String

b)

Child class only

c)

Parent class or interface

d)

Float

25.

Which of the following is an example of polymorphism in PHP?

a)

Using echo in a loop

b)

Using multiple constructors in a class

c)

Multiple classes implementing the same interface

d)

Declaring a global variable

26.

In the code below, what does the getDetails() method in Car do? public function getDetails(): string { return parent::getDetails() . " with $this->doors doors"; }

a)

Returns only car doors

b)

Calls the Vehicle class's getDetails method and adds door info

c)

Creates a new car

d)

Deletes the model property

27.

Given this code: class Vehicle { public string $make; public string $model; public function __construct(string $make, string $model) { $this->make = $make; $this->model = $model; } public function getDetails(): string { return "$this->make $this->model"; } } //What will getDetails() return if $make = "Toyota" and $model = "Corolla"?

a)

Corolla Toyota

b)

Toyota Corolla

c)

$make $model

d)

Nothing

28.

In the following code: class Circle implements Shape { public function area(): float { return pi() * $this->radius * $this->radius; } }//What interface requirement must the class fulfill?

a)

It must define a calculate() method

b)

It must define a constructor

c)

It must define the area() method

d)

It must implement a parent class

29.

What does the implements keyword signify in PHP?

a)

The class copies another class

b)

The class extends another class

c)

The class adheres to the interface definition

d)

The class overwrites properties

30.

What happens if a class claims to implement an interface but does not define all its methods?

a)

The program runs with a warning

b)

PHP will silently skip the methods

c)

A fatal error will be thrown

d)

The missing methods will be automatically added

31.

Given: interface Shape { public function area(): float; }//Which of the following class definitions correctly implements the interface?

a)

class Square { public function draw() {} }

b)

class Circle implements Shape { public function area(): float { return 3.14; } }

c)

class Triangle implements Shape { public function draw() {} }

d)

class Rectangle { public function area() {} }

32.

What is the result of running this function? function printArea(Shape $shape) { echo "Area: " . $shape->area(); }//Assume a proper Shape object is passed.

a)

Prints a message with the area

b)

Returns the perimeter

c)

Declares a new class

d)

Causes a syntax error

33.

Why would a developer use inheritance?

a)

To reduce execution speed

b)

To avoid using interfaces

c)

To reuse and extend existing code

d)

To convert properties into strings

34.

In this code: class Car extends Vehicle { public int $doors; }//What does extends Vehicle imply?

a)

Car creates a vehicle

b)

Car is the same as vehicle

c)

Car inherits Vehicle's properties and methods

d)

Car deletes vehicle

35.

Which of the following shows runtime polymorphism?

a)

Defining functions inside a loop

b)

Two classes implementing the same interface and using it in a single function

c)

Calling private methods

d)

Using include for files

36.

What will be the output of this? $car = new Car("Toyota", "Corolla", 4); echo $car->getDetails(); //Assuming proper class definitions.

a)

Corolla Toyota

b)

Toyota Corolla with 4 doors

c)

4 doors only

d)

Car created

37.

What OOP concept(s) does this code demonstrate? interface PaymentMethod { public function pay(float $amount): string; } class CreditCard implements PaymentMethod { public function pay(float $amount): string { return "Paid $amount using Credit Card"; } } class PayPal implements PaymentMethod { public function pay(float $amount): string { return "Paid $amount using PayPal"; } } function processPayment(PaymentMethod $method, float $amount) { echo $method->pay($amount); } $card = new CreditCard(); $paypal = new PayPal(); processPayment($card, 100.00); processPayment($paypal, 50.00);

a)

Inheritance

b)

Polymorphism

c)

Both Inheritance and Polymorphism

d)

Neither

38.

What OOP concept(s) does the code below demonstrate? class Vehicle { public string $brand; public function __construct(string $brand) { $this->brand = $brand; } public function getBrand(): string { return $this->brand; } } class Car extends Vehicle { public int $doors; public function __construct(string $brand, int $doors) { parent::__construct($brand); $this->doors = $doors; } public function getCarInfo(): string { return $this->getBrand() . " with " . $this->doors . " doors"; } } $myCar = new Car("Toyota", 4); echo $myCar->getCarInfo();

a)

Inheritance

b)

Polymorphism

c)

Both Inheritance and Polymorphism

d)

Neither