NEW
Font size
WorksheetsObject-Oriented Programming Quiz
Total questions: 40
Worksheet time: 30mins
What is the primary purpose of exceptions in PHP?
To improve performance
To handle errors and exceptional conditions
To create faster loops
To replace variables
Which PHP keyword is used to define a block of code where exceptions may occur?
throw
catch
try
finally
Which keyword is used to raise an exception in PHP?
raise
throw
error
catch
In the given example, what is the name of the custom exception class?
MyException
CustomException
UserException
DivisionException
What block of code is executed regardless of whether an exception is thrown or not?
try
throw
catch
finally
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?
Returns 0
Throws an Exception
Prints "Division by zero."
Ignores the error
If an exception is thrown but no catch block is defined, what will happen?
The program ignores the exception
The program continues as normal
The program terminates with a fatal error
The program retries the block
Consider the code: try { echo divide(10, 2); } catch (CustomException $e) { echo "Error: " . $e->getMessage(); } What will be the output?
Error: Division by zero
5
Nothing
Execution complete
Why might a developer create a CustomException class instead of using the built-in Exception class?
To add unique behavior for specific error types
To make code run faster
To avoid using try-catch blocks
To simplify syntax
What is the output of this code? try { echo divide(10, 0); } catch (CustomException $e) { echo "Error: " . $e->getMessage(); } finally { echo " Execution complete."; }
Error: Division by zero. Execution complete.
Execution complete.
0 Execution complete.
Program crashes
Which block handles the error when an exception is thrown?
finally
try
catch
throw
Suppose you are designing a banking system. Which scenario would best justify creating a custom exception class?
Looping through transactions
Logging successful deposits
Handling "InsufficientBalanceException" when withdrawal exceeds balance
Printing account statements
You want to ensure your program always logs an action even if an exception occurs. Which structure should you use?
try-throw
try-catch
try-catch-finally
throw-catch
Imagine you want to handle multiple error types differently. Which approach should you use?
Use multiple catch blocks for different exception classes
Use only one catch block for all exceptions
Avoid try-catch and use if-else statements
Use finally for all errors
What is object composition in PHP?
Using objects within other objects to build functionality
Reusing variables across classes
Replacing inheritance with loops
Combining arrays into one
In the provided example, which class is responsible for starting the car?
Car
Engine
Vehicle
Driver
Which keyword is used to create an object inside another class in PHP?
function
new
include
use
What does the Engine class's start() method return in the example?
"Car started"
"Engine running"
"Engine started"
True
Which OOP concept is most closely related to object composition?
Polymorphism
Encapsulation
Code reuse and modularity
Exception handling
In the example, the Car class has which type of relationship with the Engine class?
Inheritance (is-a)
Composition (has-a)
Polymorphism
Abstraction
What will the following code output? $car = new Car(); echo $car->startCar();
Engine started
Car started
Start successful
Nothing
Why is object composition sometimes preferred over inheritance?
It prevents code reuse
It reduces flexibility
It allows combining behaviors without creating large hierarchies
It requires fewer classes
If we want the Car class to also include a Radio object, how should we add it?
Inherit the Radio class
Create a Radio object inside Car
Use exception handling
Replace Engine with Radio
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) Car moving
b) Wheel rotating
c) Engine started
d) Rotate complete
What best describes the code private Engine $engine; in the Car class?
Dependency injection
Composition of objects
Static property declaration
Polymorphic interface
Suppose you are designing a Computer class that should use a Processor and Memory object. Which OOP technique is best?
Inheritance from Processor and Memory
Composition: include Processor and Memory as objects inside Computer
Exception handling for Processor errors
Using global variables for Processor and Memory
You want to design a Smartphone class that uses Camera, Battery, and Screen. Which is the most scalable approach?
Extend from Camera, Battery, and Screen classes
Use multiple traits
Apply composition by creating these objects inside Smartphone
Use only one base class
What is a trait in PHP?
A class that cannot be instantiated
A mechanism for code reuse in single inheritance languages
An interface for defining contracts
A function that replaces inheritance
Which keyword is used to include a trait in a class?
require
include
use
trait
What is the keyword used to define a trait?
class
interface
rait
abstract
In the example, what does the log() method inside the Logger trait do?
Writes logs into a file
Prints a log message prefixed with "Log:"
Sends a log to a database
Stops code execution
Which PHP limitation are traits designed to overcome?
Lack of abstract classes
Lack of multiple inheritance
Lack of encapsulation
Lack of namespaces
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?
Log saved
Log: User Alice created.
User Alice created.
Nothing
Which statement best describes the relationship between traits and classes?
Traits are inherited like parent classes
Traits are copied into the class at compile time
Traits override constructors
Traits replace class declarations
Why might a developer choose traits over inheritance?
Traits allow methods to be shared across unrelated classes
Traits increase runtime performance
Traits prevent encapsulation issues
Traits make classes final
If both User and Order classes use the Logger trait, what happens?
Only User can log messages
Both classes get the log() method
The trait causes an error
Only Order inherits the trait
Which of the following is TRUE when multiple classes use the same trait?
Each class has its own independent copy of the trait methods
The trait can only be used once in the whole program
Traits automatically create abstract methods
Traits prevent polymorphism
Suppose you want to reuse the same sendEmail() method across Admin, User, and Manager classes. What's the best approach?
Put sendEmail() in an abstract class and inherit
Copy-paste the method into each class
Create a trait with sendEmail() and include it in each class
Use global functions
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?
Create a base class and force both to inherit it
Duplicate the method in both classes
Create a ReportGenerator trait and use it in both classes
Use exception handling
You want to combine logging and reporting in one class (Admin). You have two traits: Logger and ReportGenerator. What should you do?
Use multiple inheritance
Include both traits in the class using use Logger, ReportGenerator;
Merge both traits into a single trait
Replace traits with interfaces
