NEW
Font size
WorksheetsPHP 8 OOP Quiz
Total questions: 38
Worksheet time: 19mins
What is one of the main advantages of PHP 8 over its previous versions?
Reduced file size
JIT Compiler for improved performance
Better browser support
New file system
Which feature allows passing arguments based on parameter names in PHP 8?
Match Expressions
Named Arguments
Union Types
Default Parameters
What is a class in PHP OOP?
A variable
A data type
A blueprint for creating objects
A loop structure
What keyword is used to create a class instance?
static
this
new
get
What is the output of this code? $car = new Car("red", "Toyota"); echo $car->getDescription();
Toyota red car
This car is a red Toyota.
Car object created
red Toyota
What is the purpose of the __construct method in a class?
To delete properties
To initialize object properties
To print the class
To inherit another class
Given this code snippet: function foo(int|float $value): int|float { return $value; } echo foo(3.5); //What will be the output?
int
3.5
Error
Float
Why would a developer use Union Types in PHP 8?
To simplify loops
To handle multiple databases
To allow variables to accept multiple types
To replace arrays
Consider this class: class Car { public string $color; public function getColor(): string { return $this->color; } } //Which statement correctly assigns and accesses the color?
$car = Car("red"); echo getColor();
$car->color = "red"; echo $car->getColor();
Car.color = "red"; getColor();
color = red; echo $car.getColor();
You need to create multiple objects with different properties from a single template. Which concept applies?
Function declaration
Array creation
Class instantiation
Global variable assignment
What will this return? $car = new Car("blue", "Ford"); echo $car->getDescription();
blue Ford
Description of car
This car is a blue Ford.
Ford blue
Why is the $this keyword used in a class method?
To access constants
To refer to the current object instance
To declare variables
To include other files
Given the class definition: class Dog { public function bark() { return "Woof!"; } } //How do you call the bark() method?
Dog->bark();
new Dog.bark();
$dog = new Dog(); echo $dog->bark();
bark();
Why would you use a method inside a class instead of a regular function?
To slow down the execution
For random output
To bind behavior to an object
For PHP backward compatibility
In OOP, which of the following best describes an object?
A variable with static scope
A function with global access
An instance of a class with properties and methods
A loop with reusable steps
You want to represent a book with title and author, and print a message like "Title by Author". What OOP elements should you use?
Functions and loops
Arrays and echo
Class with properties and a method
Superglobals
What does OOP stand for in programming?
Original Output Process
Object-Oriented Programming
Output Order Parameter
Organized Object Program
What is the purpose of a method inside a class?
To store data
To create loops
To define actions/behaviors
To write HTML
Which of the following best describes a property in a class?
A function
A condition
A variable that belongs to an object
A constructor
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. ?>
$car
Echo
setDescription
getDescription
What is inheritance in PHP OOP?
A way to loop through arrays
A class inheriting properties and methods from another class
A function calling another function
An object creating another object
What is the keyword used in PHP to inherit from a parent class?
extend
implements
extends
use
What is the purpose of the parent::__construct() statement?
To create a new object
To call the parent class constructor
To delete the child class
To declare a function
In polymorphism, objects are treated as what type?
String
Child class only
Parent class or interface
Float
Which of the following is an example of polymorphism in PHP?
Using echo in a loop
Using multiple constructors in a class
Multiple classes implementing the same interface
Declaring a global variable
In the code below, what does the getDetails() method in Car do? public function getDetails(): string { return parent::getDetails() . " with $this->doors doors"; }
Returns only car doors
Calls the Vehicle class's getDetails method and adds door info
Creates a new car
Deletes the model property
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"?
Corolla Toyota
Toyota Corolla
$make $model
Nothing
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?
It must define a calculate() method
It must define a constructor
It must define the area() method
It must implement a parent class
What does the implements keyword signify in PHP?
The class copies another class
The class extends another class
The class adheres to the interface definition
The class overwrites properties
What happens if a class claims to implement an interface but does not define all its methods?
The program runs with a warning
PHP will silently skip the methods
A fatal error will be thrown
The missing methods will be automatically added
Given: interface Shape { public function area(): float; }//Which of the following class definitions correctly implements the interface?
class Square { public function draw() {} }
class Circle implements Shape { public function area(): float { return 3.14; } }
class Triangle implements Shape { public function draw() {} }
class Rectangle { public function area() {} }
What is the result of running this function? function printArea(Shape $shape) { echo "Area: " . $shape->area(); }//Assume a proper Shape object is passed.
Prints a message with the area
Returns the perimeter
Declares a new class
Causes a syntax error
Why would a developer use inheritance?
To reduce execution speed
To avoid using interfaces
To reuse and extend existing code
To convert properties into strings
In this code: class Car extends Vehicle { public int $doors; }//What does extends Vehicle imply?
Car creates a vehicle
Car is the same as vehicle
Car inherits Vehicle's properties and methods
Car deletes vehicle
Which of the following shows runtime polymorphism?
Defining functions inside a loop
Two classes implementing the same interface and using it in a single function
Calling private methods
Using include for files
What will be the output of this? $car = new Car("Toyota", "Corolla", 4); echo $car->getDetails(); //Assuming proper class definitions.
Corolla Toyota
Toyota Corolla with 4 doors
4 doors only
Car created
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);
Inheritance
Polymorphism
Both Inheritance and Polymorphism
Neither
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();
Inheritance
Polymorphism
Both Inheritance and Polymorphism
Neither
