wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Object-Oriented Programming Quiz

Total questions: 40

Worksheet time: 30mins

Name
Class
Date
1.

What is the primary purpose of exceptions in PHP?

a)

To improve performance

b)

To handle errors and exceptional conditions

c)

To create faster loops

d)

To replace variables

2.

Which PHP keyword is used to define a block of code where exceptions may occur?

a)

throw

b)

catch

c)

try

d)

finally

3.

Which keyword is used to raise an exception in PHP?

a)

raise

b)

throw

c)

error

d)

catch

4.

In the given example, what is the name of the custom exception class?

a)

MyException

b)

CustomException

c)

UserException

d)

DivisionException

5.

What block of code is executed regardless of whether an exception is thrown or not?

a)

try

b)

throw

c)

catch

d)

finally

6.

Given the function: function divide($a, $b) { if ($b === 0) { throw new Exception("Division by zero."); } return $a / $b; } What happens when divide(4, 0) is called?

a)

Returns 0

b)

Throws an Exception

c)

Prints "Division by zero."

d)

Ignores the error

7.

If an exception is thrown but no catch block is defined, what will happen?

a)

The program ignores the exception

b)

The program continues as normal

c)

The program terminates with a fatal error

d)

The program retries the block

8.

Consider the code: try { echo divide(10, 2); } catch (CustomException $e) { echo "Error: " . $e->getMessage(); } What will be the output?

a)

Error: Division by zero

b)

5

c)

Nothing

d)

Execution complete

9.

Why might a developer create a CustomException class instead of using the built-in Exception class?

a)

To add unique behavior for specific error types

b)

To make code run faster

c)

To avoid using try-catch blocks

d)

To simplify syntax

10.

What is the output of this code? try { echo divide(10, 0); } catch (CustomException $e) { echo "Error: " . $e->getMessage(); } finally { echo " Execution complete."; }

a)

Error: Division by zero. Execution complete.

b)

Execution complete.

c)

0 Execution complete.

d)

Program crashes

11.

Which block handles the error when an exception is thrown?

a)

finally

b)

try

c)

catch

d)

throw

12.

Suppose you are designing a banking system. Which scenario would best justify creating a custom exception class?

a)

Looping through transactions

b)

Logging successful deposits

c)

Handling "InsufficientBalanceException" when withdrawal exceeds balance

d)

Printing account statements

13.

You want to ensure your program always logs an action even if an exception occurs. Which structure should you use?

a)

try-throw

b)

try-catch

c)

try-catch-finally

d)

throw-catch

14.

Imagine you want to handle multiple error types differently. Which approach should you use?

a)

Use multiple catch blocks for different exception classes

b)

Use only one catch block for all exceptions

c)

Avoid try-catch and use if-else statements

d)

Use finally for all errors

15.

What is object composition in PHP?

a)

Using objects within other objects to build functionality

b)

Reusing variables across classes

c)

Replacing inheritance with loops

d)

Combining arrays into one

16.

In the provided example, which class is responsible for starting the car?

a)

Car

b)

Engine

c)

Vehicle

d)

Driver

17.

Which keyword is used to create an object inside another class in PHP?

a)

function

b)

new

c)

include

d)

use

18.

What does the Engine class's start() method return in the example?

a)

"Car started"

b)

"Engine running"

c)

"Engine started"

d)

True

19.

Which OOP concept is most closely related to object composition?

a)

Polymorphism

b)

Encapsulation

c)

Code reuse and modularity

d)

Exception handling

20.

In the example, the Car class has which type of relationship with the Engine class?

a)

Inheritance (is-a)

b)

Composition (has-a)

c)

Polymorphism

d)

Abstraction

21.

What will the following code output? $car = new Car(); echo $car->startCar();

a)

Engine started

b)

Car started

c)

Start successful

d)

Nothing

22.

Why is object composition sometimes preferred over inheritance?

a)

It prevents code reuse

b)

It reduces flexibility

c)

It allows combining behaviors without creating large hierarchies

d)

It requires fewer classes

23.

If we want the Car class to also include a Radio object, how should we add it?

a)

Inherit the Radio class

b)

Create a Radio object inside Car

c)

Use exception handling

d)

Replace Engine with Radio

24.

Consider: class Wheel { public function rotate() { return "Wheel rotating"; } } class Car { private Wheel $wheel; public function __construct() { $this->wheel = new Wheel(); } public function move() { return $this->wheel->rotate(); } } $car = new Car(); echo $car->move(); What is the output?

a)

a) Car moving

b)

b) Wheel rotating

c)

c) Engine started

d)

d) Rotate complete

25.

What best describes the code private Engine $engine; in the Car class?

a)

Dependency injection

b)

Composition of objects

c)

Static property declaration

d)

Polymorphic interface

26.

Suppose you are designing a Computer class that should use a Processor and Memory object. Which OOP technique is best?

a)

Inheritance from Processor and Memory

b)

Composition: include Processor and Memory as objects inside Computer

c)

Exception handling for Processor errors

d)

Using global variables for Processor and Memory

27.

You want to design a Smartphone class that uses Camera, Battery, and Screen. Which is the most scalable approach?

a)

Extend from Camera, Battery, and Screen classes

b)

Use multiple traits

c)

Apply composition by creating these objects inside Smartphone

d)

Use only one base class

28.

What is a trait in PHP?

a)

A class that cannot be instantiated

b)

A mechanism for code reuse in single inheritance languages

c)

An interface for defining contracts

d)

A function that replaces inheritance

29.

Which keyword is used to include a trait in a class?

a)

require

b)

include

c)

use

d)

trait

30.

What is the keyword used to define a trait?

a)

class

b)

interface

c)

rait

d)

abstract

31.

In the example, what does the log() method inside the Logger trait do?

a)

Writes logs into a file

b)

Prints a log message prefixed with "Log:"

c)

Sends a log to a database

d)

Stops code execution

32.

Which PHP limitation are traits designed to overcome?

a)

Lack of abstract classes

b)

Lack of multiple inheritance

c)

Lack of encapsulation

d)

Lack of namespaces

33.

In the example: trait Logger { public function log(string $message) { echo "Log: " . $message; } } class User { use Logger; public function createUser(string $name) { $this->log("User $name created."); } } $user = new User(); $user->createUser("Alice"); What will be the output?

a)

Log saved

b)

Log: User Alice created.

c)

User Alice created.

d)

Nothing

34.

Which statement best describes the relationship between traits and classes?

a)

Traits are inherited like parent classes

b)

Traits are copied into the class at compile time

c)

Traits override constructors

d)

Traits replace class declarations

35.

Why might a developer choose traits over inheritance?

a)

Traits allow methods to be shared across unrelated classes

b)

Traits increase runtime performance

c)

Traits prevent encapsulation issues

d)

Traits make classes final

36.

If both User and Order classes use the Logger trait, what happens?

a)

Only User can log messages

b)

Both classes get the log() method

c)

The trait causes an error

d)

Only Order inherits the trait

37.

Which of the following is TRUE when multiple classes use the same trait?

a)

Each class has its own independent copy of the trait methods

b)

The trait can only be used once in the whole program

c)

Traits automatically create abstract methods

d)

Traits prevent polymorphism

38.

Suppose you want to reuse the same sendEmail() method across Admin, User, and Manager classes. What's the best approach?

a)

Put sendEmail() in an abstract class and inherit

b)

Copy-paste the method into each class

c)

Create a trait with sendEmail() and include it in each class

d)

Use global functions

39.

Imagine you are building an e-commerce system where both Product and Order classes need a generateReport() method. What's the best way to implement this?

a)

Create a base class and force both to inherit it

b)

Duplicate the method in both classes

c)

Create a ReportGenerator trait and use it in both classes

d)

Use exception handling

40.

You want to combine logging and reporting in one class (Admin). You have two traits: Logger and ReportGenerator. What should you do?

a)

Use multiple inheritance

b)

Include both traits in the class using use Logger, ReportGenerator;

c)

Merge both traits into a single trait

d)

Replace traits with interfaces