Object-Oriented Programming (OOP) is a fundamental concept in PHP, providing a structured approach to code organization and reuse. This guide covers key OOP concepts like classes, objects, inheritance, polymorphism, and encapsulation through 30 multiple-choice questions to enhance your understanding.
Object-Oriented Programming (OOP) Basics – MCQs
1. Classes and Objects
What is the purpose of a class in PHP OOP?
a) To define a set of properties and methods
b) To create a variable
c) To store a function
d) To execute code
Which of the following is the correct way to instantiate a class in PHP?
a) new ClassName();
b) create ClassName();
c) instance ClassName();
d) make ClassName();
What is an object in PHP OOP?
a) A function inside a class
b) An instance of a class
c) A variable in a class
d) A method in a class
How do you access a method from an object in PHP?
a) object::method()
b) object->method()
c) method->object()
d) method::object()
Which of the following is true about classes in PHP?
a) A class cannot have multiple constructors
b) A class can contain both properties and methods
c) A class is a global variable
d) A class can only contain variables
What will happen if you try to instantiate a class that does not have a constructor?
a) An error will occur
b) PHP will automatically call the constructor
c) PHP will create a default constructor
d) The object will not be created
2. Properties and Methods
What is the correct way to define a property in a PHP class?
a) public $property;
b) var $property;
c) property $property;
d) $property public;
Which of the following is the correct way to define a method in a PHP class?
a) function methodName() {}
b) method methodName() {}
c) def methodName() {}
d) method functionName() {}
How do you access a property from an object in PHP?
a) object::property
b) object->property
c) object->getProperty()
d) object->setProperty()
What is the purpose of a method in a class?
a) To define a class’s behavior
b) To store a variable’s value
c) To initialize the class
d) To modify properties
Which of the following is a valid property visibility in PHP?
a) private
b) protected
c) public
d) All of the above
How can you set the value of a property inside a method?
a) $this->property = value;
b) this->property = value;
c) object->property = value;
d) $this::property = value;
3. Constructor and Destructor Methods
What is the purpose of a constructor in PHP?
a) To initialize object properties
b) To destroy an object
c) To invoke a method
d) To create a class
What is the correct syntax for defining a constructor in PHP?
a) function __construct() {}
b) function constructor() {}
c) function __init() {}
d) function new() {}
When is the destructor method called in PHP?
a) When an object is created
b) When a class is defined
c) When an object is destroyed
d) When a method is invoked
What is the purpose of a destructor in PHP?
a) To initialize object properties
b) To clean up after an object
c) To set object properties to default
d) To execute a function
Can a class have both a constructor and a destructor?
a) Yes
b) No
c) Only one can be defined
d) Only in abstract classes
What is the correct way to call the destructor of an object in PHP?
a) The destructor is automatically called when the object is destroyed
b) You call the destructor manually using a special method
c) You call the destructor with $this->destructor()
d) You cannot call the destructor manually
4. Access Modifiers (public, private, protected)
What is the purpose of the public access modifier in PHP?
a) To make properties and methods accessible from outside the class
b) To restrict access to properties and methods
c) To make the property read-only
d) To hide the property from the class
What does the private access modifier do in PHP?
a) It allows access from anywhere
b) It allows access only within the class
c) It allows access only from subclasses
d) It restricts access to the class
Which of the following access modifiers allows access to properties and methods only within the class and its subclasses?
a) private
b) public
c) protected
d) static
What will happen if a private method is called from outside its class?
a) The method will execute without any issues
b) An error will occur
c) The method will execute but with restricted access
d) None of the above
Which access modifier is the default for properties in PHP?
a) public
b) private
c) protected
d) None of the above
Can a method in a subclass override a private method from its parent class?
a) Yes
b) No
c) Only if the method is public
d) Only if the method is protected
5. Inheritance
What is inheritance in PHP OOP?
a) When a class inherits properties from other classes
b) When an object creates another object
c) When a class defines multiple methods
d) When a class has multiple constructors
Which keyword is used to inherit a class in PHP?
a) extends
b) inherits
c) include
d) use
Can a child class access the private properties of a parent class?
a) Yes, if the child class overrides the property
b) No, private properties are not accessible
c) Yes, with the public keyword
d) Yes, if the property is protected
What is the result of inheritance in PHP?
a) A subclass inherits the properties and methods of the parent class
b) A subclass completely replaces the parent class
c) A subclass cannot inherit from the parent class
d) A subclass ignores the parent class
Which of the following is true about constructor inheritance in PHP?
a) A subclass can inherit the constructor of the parent class
b) A subclass must define its own constructor
c) Constructors cannot be inherited
d) A subclass can only call the constructor manually
6. Polymorphism
What is polymorphism in PHP OOP?
a) A class having multiple methods with the same name but different parameters
b) A class having multiple constructors
c) A method that is inherited from a parent class
d) A class that inherits from multiple classes
Answers
QNo
Answer
1
a) To define a set of properties and methods
2
a) new ClassName();
3
b) An instance of a class
4
b) object->method()
5
b) A class can contain both properties and methods
6
b) PHP will automatically call the constructor
7
a) public $property;
8
a) function methodName() {}
9
b) object->property
10
a) To define a class’s behavior
11
d) All of the above
12
a) $this->property = value;
13
a) To initialize object properties
14
a) function __construct() {}
15
c) When an object is destroyed
16
b) To clean up after an object
17
a) Yes
18
a) The destructor is automatically called when the object is destroyed
19
a) To make properties and methods accessible from outside the class
20
b) It allows access only within the class
21
c) protected
22
b) An error will occur
23
a) public
24
b) No
25
a) When a class inherits properties from other classes
26
a) extends
27
b) No, private properties are not accessible
28
a) A subclass inherits the properties and methods of the parent class
29
a) A subclass can inherit the constructor of the parent class
30
a) A class having multiple methods with the same name but different parameters