PHP OOPs MCQ Questions and Answers

1. What keyword is used to declare a class in PHP?

a) class
b) Class
c) declare
d) new

Answer:

a) class

Explanation:

The 'class' keyword is used to declare a class in PHP.

2. How do you create an instance of a class in PHP?

a) new ClassName()
b) create ClassName()
c) class ClassName()
d) instance ClassName()

Answer:

a) new ClassName()

Explanation:

An instance of a class is created using the 'new' keyword followed by the class name.

3. What is the method inside a class that is called automatically when an object is created?

a) start()
b) __begin()
c) __construct()
d) __init()

Answer:

c) __construct()

Explanation:

The __construct() method is a constructor method in PHP that is called automatically when an object is created.

4. What is the visibility keyword that allows properties or methods to be accessible from anywhere?

a) public
b) private
c) protected
d) global

Answer:

a) public

Explanation:

The 'public' visibility keyword allows properties or methods to be accessible from anywhere.

5. What keyword is used to define a constant inside a class in PHP?

a) define
b) const
c) constant
d) def

Answer:

b) const

Explanation:

The 'const' keyword is used to define a constant inside a class in PHP.

6. What is it called when a class inherits the properties and methods of another class in PHP?

a) Class cloning
b) Extending
c) Implementing
d) Instantiating

Answer:

b) Extending

Explanation:

Inheritance in PHP is achieved when a class 'extends' another class, inheriting its properties and methods.

7. What is encapsulation in PHP OOP?

a) Packaging data and methods together
b) Splitting a class into multiple classes
c) Copying properties from one class to another
d) Hiding the internal state of an object

Answer:

a) Packaging data and methods together

Explanation:

Encapsulation involves bundling the data (properties) and methods that operate on the data into a single unit or class.

8. How do you refer to the current object's properties or methods from within a class's methods in PHP?

a) $this
b) self
c) static
d) this

Answer:

a) $this

Explanation:

The $this keyword is used to refer to the current object's properties or methods within a class.

9. What is the meaning of polymorphism in PHP OOP?

a) A class can have only one form
b) A class can take on many forms
c) Multiple classes can have the same name
d) Changing properties at runtime

Answer:

b) A class can take on many forms

Explanation:

Polymorphism allows objects of different classes to be treated as objects of a common super class.

10. Which keyword is used to define a method that cannot be overridden in a child class in PHP?

a) final
b) static
c) sealed
d) fixed

Answer:

a) final

Explanation:

The 'final' keyword is used to define a method that cannot be overridden in a child class.

11. How do you create an abstract class in PHP?

a) abstract class ClassName { ... }
b) class abstract ClassName { ... }
c) class ClassName abstract { ... }
d) Abstract class ClassName { ... }

Answer:

a) abstract class ClassName { ... }

Explanation:

An abstract class is created using the 'abstract' keyword before the 'class' keyword.

12. What is an interface in PHP?

a) A class that implements all methods
b) A template that defines what methods a class should implement
c) A built-in PHP class
d) A way to interact with objects

Answer:

b) A template that defines what methods a class should implement

Explanation:

An interface is a template that defines a set of methods that a class must implement.

13. What is the purpose of the 'static' keyword for properties and methods in PHP?

a) To make them constant
b) To make them accessible without needing an instance of the class
c) To protect them from being modified
d) To make them private

Answer:

b) To make them accessible without needing an instance of the class

Explanation:

Static properties and methods can be accessed directly without creating an instance of the class.

14. What is method overloading in PHP?

a) Changing the way a method works
b) Creating multiple methods with the same name but different parameters
c) Increasing the efficiency of a method
d) Replacing an existing method in a parent class

Answer:

b) Creating multiple methods with the same name but different parameters

Explanation:

Method overloading involves creating multiple methods with the same name but different parameters, which is not natively supported in PHP.

15. How is a property or method of a parent class referred to within a child class in PHP?

a) parent::
b) super::
c) base::
d) Parent::

Answer:

a) parent::

Explanation:

The 'parent::' syntax is used to refer to a property or method of a parent class within a child class.

16. What is an object in the context of PHP OOP?

a) A variable
b) An instance of a class
c) A function
d) A data type

Answer:

b) An instance of a class

Explanation:

In object-oriented programming, an object is an instance of a class.

17. What is the correct way to declare a property in a PHP class?

a) var $property;
b) public $property;
c) property $var;
d) $property public;

Answer:

b) public $property;

Explanation:

A property in a PHP class is declared with a visibility keyword (like public) followed by the property name.

18. Can a PHP class have more than one constructor method?

a) Yes
b) No
c) Only if it's a child class
d) Only in PHP 7 and above

Answer:

b) No

Explanation:

A PHP class can only have one constructor method, named __construct.

19. Which visibility keyword allows access to a property or method only within the class itself and its child classes?

a) public
b) private
c) protected
d) internal

Answer:

c) protected

Explanation:

The 'protected' visibility keyword allows access within the class itself and its child classes.

20. What is a trait in PHP?

a) A special kind of class
b) A way to extend multiple classes
c) A mechanism for code reuse in single inheritance languages like PHP
d) A built-in PHP interface

Answer:

c) A mechanism for code reuse in single inheritance languages like PHP

Explanation:

Traits are a mechanism in PHP for reusing code in single inheritance languages, allowing a developer to reuse sets of methods freely in several independent classes.


Comments