MCQs on Object-Oriented Programming (OOP) Basics | PHP Intermediate

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

  1. 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
  2. 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();
  3. 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
  4. How do you access a method from an object in PHP?
    • a) object::method()
    • b) object->method()
    • c) method->object()
    • d) method::object()
  5. 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
  6. 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

  1. 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;
  2. 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() {}
  3. How do you access a property from an object in PHP?
    • a) object::property
    • b) object->property
    • c) object->getProperty()
    • d) object->setProperty()
  4. 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
  5. Which of the following is a valid property visibility in PHP?
    • a) private
    • b) protected
    • c) public
    • d) All of the above
  6. 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

  1. 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
  2. What is the correct syntax for defining a constructor in PHP?
    • a) function __construct() {}
    • b) function constructor() {}
    • c) function __init() {}
    • d) function new() {}
  3. 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
  4. 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
  5. 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
  6. 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)

  1. 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
  2. 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
  3. 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
  4. 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
  5. Which access modifier is the default for properties in PHP?
    • a) public
    • b) private
    • c) protected
    • d) None of the above
  6. 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

  1. 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
  2. Which keyword is used to inherit a class in PHP?
    • a) extends
    • b) inherits
    • c) include
    • d) use
  3. 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
  4. 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
  5. 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

  1. 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

QNoAnswer
1a) To define a set of properties and methods
2a) new ClassName();
3b) An instance of a class
4b) object->method()
5b) A class can contain both properties and methods
6b) PHP will automatically call the constructor
7a) public $property;
8a) function methodName() {}
9b) object->property
10a) To define a class’s behavior
11d) All of the above
12a) $this->property = value;
13a) To initialize object properties
14a) function __construct() {}
15c) When an object is destroyed
16b) To clean up after an object
17a) Yes
18a) The destructor is automatically called when the object is destroyed
19a) To make properties and methods accessible from outside the class
20b) It allows access only within the class
21c) protected
22b) An error will occur
23a) public
24b) No
25a) When a class inherits properties from other classes
26a) extends
27b) No, private properties are not accessible
28a) A subclass inherits the properties and methods of the parent class
29a) A subclass can inherit the constructor of the parent class
30a) A class having multiple methods with the same name but different parameters

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top