wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Object Oriented Programming Quiz

Total questions: 40

Worksheet time: 20mins

Name
Class
Date
1.

What is encapsulation in object-oriented programming?

a)

Writing code in a procedural style

b)

Allowing free access to all class properties

c)

Restricting direct access to some of an object's components

d)

Making code longer and more complex

2.

Which access modifier is commonly used to implement encapsulation?

a)

public

b)

protected

c)

global

d)

private

3.

In PHP, which keyword restricts property access to within the class only?

a)

public

b)

private

c)

external

d)

abstract

4.

What is the purpose of getter methods in encapsulation?

a)

To delete object properties

b)

To provide controlled read access to private properties

c)

To directly change class variables

d)

To generate random values

5.

What is the role of a setter method in a class?

a)

Sets cookies

b)

Assigns permissions to users

c)

Assigns a value to a private property using a public method

d)

Reads data from the database

6.

Which of the following is a benefit of encapsulation?

a)

Slower performance

b)

No need for class constructors

c)

Better control over data and security

d)

Increased global variables

7.

Given the following class, which method is used to access the $name property?

a)

$person->name;

b)

$person->setName();

c)

$person->getName();

d)

$person->__get();

8.

What happens if you try to directly modify a private property from outside the class?

a)

PHP will automatically allow it

b)

The property value is reset to default

c)

A syntax warning is shown

d)

A fatal error occurs

9.

You want to ensure that a user's age cannot be set to a negative number. What should the setter method include?

a)

return -$age;

b)

if ($age < 0) { $this->age = 0; }

c)

if ($age > 0) { $this->age = $age; }

d)

$this->age = $age + 1;

10.

In the following class, which method would properly update the $age property?

a)

$user->age = 25;

b)

$user->setAge(25);

c)

$user->getAge();

d)

$user->updateAge();

11.

What design principle does encapsulation directly support?

a)

Open/Closed Principle

b)

DRY (Don't Repeat Yourself)

c)

Data Hiding

d)

Code Duplication

12.

Consider the code below. Which modification best applies encapsulation principles?

a)

Make the $balance private and provide getter/setter with validation

b)

Leave it as public

c)

Make $balance global

d)

Remove the $balance property

13.

A developer argues that using getter and setter methods is unnecessary if all properties are public. What's the best response?

a)

"You're right; it's more efficient."

b)

"Public access ensures better performance."

c)

"Encapsulation promotes data integrity and control, which public properties lack."

d)

"Public variables are part of encapsulation."

14.

Which of the following best defines an interface in PHP?

a)

A class with no methods

b)

A class that must be extended

c)

A contract that classes must implement

d)

A special type of trait

15.

What keyword is used to define an abstract class in PHP?

a)

interface

b)

extends

c)

implements

d)

Abstract

16.

Which of the following statements is TRUE about abstract classes?

a)

They can be instantiated directly.

b)

They must contain only private methods.

c)

They can contain both abstract and non-abstract methods.

d)

They cannot have any properties.

17.

Which PHP keyword is used to declare a class that follows an interface?

a)

extends

b)

follows

c)

uses

d)

implements

18.

What happens if a class implements an interface but does not define all its methods?

a)

It will work normally.

b)

It will throw a warning.

c)

It will result in a fatal error.

d)

The interface methods are ignored.

19.

Given the interface: interface Logger { public function log(string $message): void; } Which class correctly implements the interfaces?

a)

class FileLogger { public function log(string $msg) {} }

b)

class FileLogger implements Logger { public function log(string $message): void {} }

c)

class FileLogger implements Logger { public function write(string $message): void {} }

d)

class Logger implements FileLogger { public function log(string $message): void {} }

20.

What is the correct way to prevent a class from being instantiated but allow other classes to extend it?

a)

Declare it as static

b)

Declare it as interface

c)

Declare it as abstract

d)

Declare it as private

21.

Which of the following demonstrates polymorphism using interfaces?

a)

$dog = new Dog(); $cat = new Cat(); $dog->makeSound(); $cat->makeSound();

b)

function logMessage(Logger $logger) { $logger->log("Test message"); }

c)

echo $this->message;

d)

class A {} class B extends A {}

22.

Given an abstract class Animal with an abstract method makeSound(), what must a child class like Dog do?

a)

Call parent::makeSound()

b)

Override makeSound() with a concrete implementation

c)

Leave makeSound() undefined

d)

Make Dog an interface

23.

Which of the following combinations is correct?

a)

class A extends Logger implements Animal

b)

abstract class A implements Logger

c)

interface A extends class Logger

d)

interface A extends LoggerClass

24.

You want to ensure all logging classes use a standard log() method, but allow each to handle storage differently. Which structure is best?

a)

Use traits with static methods

b)

Use an abstract class with default logging logic

c)

Use an interface Logger and implement it in all classes

d)

Use inheritance from a base class

25.

You are designing a class hierarchy where Bird is abstract. Duck can fly, but Ostrich cannot. Where should the fly() method be placed?

a)

Inside Bird as an abstract method

b)

Inside Bird as a concrete method

c)

Only in Duck, since not all birds can fly

d)

In an interface and implemented by Bird

26.

When should you choose an interface over an abstract class?

a)

When you want to share method implementations

b)

When you need to define optional methods

c)

When you want multiple inheritance of behavior

d)

When you only need to enforce a contract across unrelated classes

27.

What is the primary purpose of namespaces in PHP?

a)

Improve database connections

b)

Avoid class name collisions

c)

Increase memory usage

d)

Automatically compile code

28.

Which keyword is used to declare a namespace in PHP?

a)

use

b)

namespace

c)

import

d)

include

29.

How do you import a class from a namespace?

a)

include ClassName;

b)

require ClassName;

c)

use Namespace\ClassName;

d)

fetch Namespace\ClassName;

30.

What does PSR-4 relate to in PHP?

a)

Error handling

b)

Namespace declaration

c)

Autoloading standard

d)

Database abstraction

31.

What is the default global namespace in PHP?

a)

GlobalNamespace

b)

Root

c)

No namespace

d)

System

32.

Given the following: namespace App\Controllers; class HomeController {} What is the full qualified class name?

a)

HomeController

b)

App\HomeController

c)

Controllers\HomeController

d)

App\Controllers\HomeController

33.

Which function is used in PHP to automatically load classes?

a)

require_once()

b)

__autoload()

c)

spl_autoload_register()

d)

autoload_class()

34.

In the statement use App\Models\User;, what is User?

a)

A constant

b)

A namespace

c)

A class

d)

A file

35.

Assuming a PSR-4 structure, which file path should be used for the class App\Services\MailService?

a)

Services/MailService.php

b)

App/MailService.php

c)

App/Services/MailService.php

d)

src/Services/MailService.class

36.

What will the following code output? spl_autoload_register(function ($class) { echo $class; }); $obj = new App\Models\User();

a)

It will instantiate User

b)

It will echo App\Models\User

c)

It will return an error

d)

It will echo User

37.

Why is autoloading preferred over manual require statements?

a)

It loads all files before execution

b)

It reduces code readability

c)

It automates class file inclusion based on usage

d)

It works only for namespaces

38.

Which approach ensures that namespaced classes from multiple vendors don't conflict with each other?

a)

Using PSR-1

b)

Using traits

c)

Using Composer with PSR-4 and distinct namespaces

d)

Only using abstract classes

39.

You created a custom autoloader but your app still throws a "class not found" error. What is the likely issue?

a)

The class is in the global scope

b)

The class doesn't have a constructor

c)

The file path doesn't match the namespace structure

d)

You used include_once instead of require

40.

Which of the following correctly demonstrates namespaced class usage with an alias?

a)

use App\Models\User; $user = new User();

b)

use App\Models\User as U; $user = new App\Models\User();

c)

use App\Models\User as U; $user = new U();

d)

use App\Models\User; $user = new U();