NEW
Font size
WorksheetsObject Oriented Programming Quiz
Total questions: 40
Worksheet time: 20mins
What is encapsulation in object-oriented programming?
Writing code in a procedural style
Allowing free access to all class properties
Restricting direct access to some of an object's components
Making code longer and more complex
Which access modifier is commonly used to implement encapsulation?
public
protected
global
private
In PHP, which keyword restricts property access to within the class only?
public
private
external
abstract
What is the purpose of getter methods in encapsulation?
To delete object properties
To provide controlled read access to private properties
To directly change class variables
To generate random values
What is the role of a setter method in a class?
Sets cookies
Assigns permissions to users
Assigns a value to a private property using a public method
Reads data from the database
Which of the following is a benefit of encapsulation?
Slower performance
No need for class constructors
Better control over data and security
Increased global variables
Given the following class, which method is used to access the $name property?
$person->name;
$person->setName();
$person->getName();
$person->__get();
What happens if you try to directly modify a private property from outside the class?
PHP will automatically allow it
The property value is reset to default
A syntax warning is shown
A fatal error occurs
You want to ensure that a user's age cannot be set to a negative number. What should the setter method include?
return -$age;
if ($age < 0) { $this->age = 0; }
if ($age > 0) { $this->age = $age; }
$this->age = $age + 1;
In the following class, which method would properly update the $age property?
$user->age = 25;
$user->setAge(25);
$user->getAge();
$user->updateAge();
What design principle does encapsulation directly support?
Open/Closed Principle
DRY (Don't Repeat Yourself)
Data Hiding
Code Duplication
Consider the code below. Which modification best applies encapsulation principles?
Make the $balance private and provide getter/setter with validation
Leave it as public
Make $balance global
Remove the $balance property
A developer argues that using getter and setter methods is unnecessary if all properties are public. What's the best response?
"You're right; it's more efficient."
"Public access ensures better performance."
"Encapsulation promotes data integrity and control, which public properties lack."
"Public variables are part of encapsulation."
Which of the following best defines an interface in PHP?
A class with no methods
A class that must be extended
A contract that classes must implement
A special type of trait
What keyword is used to define an abstract class in PHP?
interface
extends
implements
Abstract
Which of the following statements is TRUE about abstract classes?
They can be instantiated directly.
They must contain only private methods.
They can contain both abstract and non-abstract methods.
They cannot have any properties.
Which PHP keyword is used to declare a class that follows an interface?
extends
follows
uses
implements
What happens if a class implements an interface but does not define all its methods?
It will work normally.
It will throw a warning.
It will result in a fatal error.
The interface methods are ignored.
Given the interface: interface Logger { public function log(string $message): void; } Which class correctly implements the interfaces?
class FileLogger { public function log(string $msg) {} }
class FileLogger implements Logger { public function log(string $message): void {} }
class FileLogger implements Logger { public function write(string $message): void {} }
class Logger implements FileLogger { public function log(string $message): void {} }
What is the correct way to prevent a class from being instantiated but allow other classes to extend it?
Declare it as static
Declare it as interface
Declare it as abstract
Declare it as private
Which of the following demonstrates polymorphism using interfaces?
$dog = new Dog(); $cat = new Cat(); $dog->makeSound(); $cat->makeSound();
function logMessage(Logger $logger) { $logger->log("Test message"); }
echo $this->message;
class A {} class B extends A {}
Given an abstract class Animal with an abstract method makeSound(), what must a child class like Dog do?
Call parent::makeSound()
Override makeSound() with a concrete implementation
Leave makeSound() undefined
Make Dog an interface
Which of the following combinations is correct?
class A extends Logger implements Animal
abstract class A implements Logger
interface A extends class Logger
interface A extends LoggerClass
You want to ensure all logging classes use a standard log() method, but allow each to handle storage differently. Which structure is best?
Use traits with static methods
Use an abstract class with default logging logic
Use an interface Logger and implement it in all classes
Use inheritance from a base class
You are designing a class hierarchy where Bird is abstract. Duck can fly, but Ostrich cannot. Where should the fly() method be placed?
Inside Bird as an abstract method
Inside Bird as a concrete method
Only in Duck, since not all birds can fly
In an interface and implemented by Bird
When should you choose an interface over an abstract class?
When you want to share method implementations
When you need to define optional methods
When you want multiple inheritance of behavior
When you only need to enforce a contract across unrelated classes
What is the primary purpose of namespaces in PHP?
Improve database connections
Avoid class name collisions
Increase memory usage
Automatically compile code
Which keyword is used to declare a namespace in PHP?
use
namespace
import
include
How do you import a class from a namespace?
include ClassName;
require ClassName;
use Namespace\ClassName;
fetch Namespace\ClassName;
What does PSR-4 relate to in PHP?
Error handling
Namespace declaration
Autoloading standard
Database abstraction
What is the default global namespace in PHP?
GlobalNamespace
Root
No namespace
System
Given the following: namespace App\Controllers; class HomeController {} What is the full qualified class name?
HomeController
App\HomeController
Controllers\HomeController
App\Controllers\HomeController
Which function is used in PHP to automatically load classes?
require_once()
__autoload()
spl_autoload_register()
autoload_class()
In the statement use App\Models\User;, what is User?
A constant
A namespace
A class
A file
Assuming a PSR-4 structure, which file path should be used for the class App\Services\MailService?
Services/MailService.php
App/MailService.php
App/Services/MailService.php
src/Services/MailService.class
What will the following code output? spl_autoload_register(function ($class) { echo $class; }); $obj = new App\Models\User();
It will instantiate User
It will echo App\Models\User
It will return an error
It will echo User
Why is autoloading preferred over manual require statements?
It loads all files before execution
It reduces code readability
It automates class file inclusion based on usage
It works only for namespaces
Which approach ensures that namespaced classes from multiple vendors don't conflict with each other?
Using PSR-1
Using traits
Using Composer with PSR-4 and distinct namespaces
Only using abstract classes
You created a custom autoloader but your app still throws a "class not found" error. What is the likely issue?
The class is in the global scope
The class doesn't have a constructor
The file path doesn't match the namespace structure
You used include_once instead of require
Which of the following correctly demonstrates namespaced class usage with an alias?
use App\Models\User; $user = new User();
use App\Models\User as U; $user = new App\Models\User();
use App\Models\User as U; $user = new U();
use App\Models\User; $user = new U();
