MCQs on Class 20
Introduction to Object-Oriented Programming (OOP) Part 2
- What does OOP stand for?
- A) Object-Oriented Programming
- B) Object-Oriented Procedure
- C) Object Oriented Protocol
- D) Object-Oriented Project
Answer: A - Which of the following is NOT a core principle of OOP?
- A) Encapsulation
- B) Inheritance
- C) Compilation
- D) Polymorphism
Answer: C - What is a class in OOP?
- A) A blueprint for creating objects
- B) A collection of data
- C) A function that returns data
- D) An instance of an object
Answer: A - What is an object in OOP?
- A) A blueprint
- B) An instance of a class
- C) A data type
- D) A programming language
Answer: B - Which access modifier allows a property to be accessed only within its own class?
- A) public
- B) private
- C) protected
- D) static
Answer: B - What does the protected access modifier mean?
- A) Accessible only within the class
- B) Accessible within the class and by derived classes
- C) Accessible from anywhere
- D) Not accessible at all
Answer: B - What is the purpose of constructors in a class?
- A) To initialize object properties
- B) To create new classes
- C) To execute code upon destruction
- D) To return values from methods
Answer: A - Which method is automatically called when an object is created?
- A) Destructor
- B) Getter
- C) Constructor
- D) Setter
Answer: C - What is method overloading?
- A) Defining multiple methods with the same name but different parameters
- B) Using a method multiple times
- C) Calling a method within itself
- D) Defining methods with different names
Answer: A - What is inheritance in OOP?
- A) The ability to create objects from classes
- B) The ability to define new classes based on existing ones
- C) The ability to access private properties
- D) The ability to execute code in parallel
Answer: B - Which symbol is commonly used to denote inheritance in UML diagrams?
- A) Arrow
- B) Solid line
- C) Dashed line
- D) Circle
Answer: A - What is polymorphism in OOP?
- A) The ability to change the state of an object
- B) The ability of different classes to be treated as instances of the same class
- C) The ability to hide data
- D) The ability to create multiple objects from a single class
Answer: B - Which of the following is an example of polymorphism?
- A) Overriding methods in derived classes
- B) Creating a new class
- C) Initializing an object
- D) Accessing properties
Answer: A - Which keyword is used to inherit a class in PHP?
- A) extends
- B) inherits
- C) implements
- D) uses
Answer: A - What does the final keyword do when applied to a class?
- A) It cannot be instantiated
- B) It cannot be inherited
- C) It cannot be modified
- D) It cannot be destroyed
Answer: B - What is a destructor in OOP?
- A) A method that initializes properties
- B) A method that is called when an object is destroyed
- C) A method that returns values
- D) A method that creates objects
Answer: B - What does encapsulation mean in OOP?
- A) Hiding the implementation details of a class
- B) Accessing all class properties freely
- C) Creating multiple classes
- D) Executing code in isolation
Answer: A - Which of the following is an example of a constructor in PHP?
- A) function __destruct() {}
- B) function __construct() {}
- C) function create() {}
- D) function initialize() {}
Answer: B - What is the purpose of getter methods?
- A) To set the value of a property
- B) To retrieve the value of a property
- C) To delete a property
- D) To modify the property type
Answer: B - What is the purpose of setter methods?
- A) To get the value of a property
- B) To retrieve the class name
- C) To set the value of a property
- D) To create a new method
Answer: C - What does the static keyword indicate?
- A) The property or method can only be accessed within the class
- B) The property or method belongs to the class itself rather than instances
- C) The property is read-only
- D) The method is an abstract method
Answer: B - What is an abstract class?
- A) A class that cannot be instantiated
- B) A class that can only have one object
- C) A class that contains only abstract methods
- D) A class that has no properties
Answer: A - What is an interface in OOP?
- A) A way to implement methods from a base class
- B) A blueprint for classes that defines methods without implementation
- C) A type of variable
- D) A mechanism for inheritance
Answer: B - What happens if a class does not have a constructor?
- A) It cannot be instantiated
- B) A default constructor is automatically provided
- C) It causes a syntax error
- D) It cannot have properties
Answer: B - How do you define an abstract method in PHP?
- A) abstract function methodName();
- B) function abstract methodName();
- C) abstract method methodName();
- D) method abstract methodName();
Answer: A - What does the instanceof operator do in PHP?
- A) Checks if an object is an instance of a specific class
- B) Creates a new instance of a class
- C) Destroys an object
- D) Copies an object
Answer: A - Which method is used to call the parent class's constructor?
- A) parent::__construct();
- B) self::__construct();
- C) this::__construct();
- D) base::__construct();
Answer: A - What will be the output of the following code?
class A {
protected function display() {
return "Class A";
}
}
class B extends A {
public function show() {
return $this->display();
}
}
$obj = new B();
echo $obj->show();
- A) Class A
- B) Error: Cannot access protected method
- C) Class B
- D) NULL
Answer: A - Which keyword is used to define an interface in PHP?
- A) interface
- B) abstract
- C) class
- D) implements
Answer: A - Which of the following is true about abstract classes?
- A) They can be instantiated
- B) They must implement all methods
- C) They can contain both abstract and non-abstract methods
- D) They cannot contain properties
Answer: C - What will happen if you try to instantiate an abstract class?
- A) The code will execute without errors
- B) A fatal error will occur
- C) It will create an object
- D) It will use a default implementation
Answer: B - What is the output of the following code?
class X {
public function show() {
return "Hello from X";
}
}
class Y extends X {
public function show() {
return "Hello from Y";
}
}
$obj = new Y();
echo $obj->show();
- A) Hello from X
- B) Hello from Y
- C) Error: Method not found
- D) NULL
Answer: B - What does data hiding in OOP achieve?
- A) It prevents unauthorized access to data
- B) It speeds up data processing
- C) It reduces code complexity
- D) It increases memory usage
Answer: A - What is a class constant in PHP?
- A) A variable whose value can change
- B) A fixed value that cannot be changed
- C) A property that can be accessed by instances
- D) A method that cannot return a value
Answer: B - How do you define a class constant in PHP?
- A) const NAME = "value";
- B) static NAME = "value";
- C) define(NAME, "value");
- D) constant NAME = "value";
Answer: A - What will the following code output?
class Vehicle {
public static $type = "Car";
public static function getType() {
return self::$type;
}
}
echo Vehicle::getType();
- A) Car
- B) Error: Cannot access static property
- C) NULL
- D) Vehicle
Answer: A - What is a trait in PHP?
- A) A way to inherit methods from a parent class
- B) A mechanism for code reuse in single inheritance
- C) A type of interface
- D) A collection of properties
Answer: B - How do you use a trait in PHP?
- A) use TraitName;
- B) include TraitName;
- C) extends TraitName;
- D) implements TraitName;
Answer: A - What happens if a method is defined in both a trait and a class?
- A) The method from the trait is ignored
- B) The method from the class is used
- C) It will throw an error
- D) The method cannot be used
Answer: B - Which of the following can be a property of a class?
- A) Method
- B) Variable
- C) Constant
- D) All of the above
Answer: D - What does the abstract keyword indicate about a method?
- A) It must have a body
- B) It cannot have any parameters
- C) It has no implementation in the abstract class
- D) It can only be public
Answer: C - How can you prevent a class from being extended?
- A) By declaring the class as final
- B) By declaring the class as abstract
- C) By not providing a constructor
- D) By not defining any methods
Answer: A - Which method will be called when an object is destroyed?
- A) __construct()
- B) __destruct()
- C) finalize()
- D) terminate()
Answer: B - What is the result of the following code?
php
Copy code
class Animal {
public function sound() {
return "Animal sound";
}
}
class Dog extends Animal {
public function sound() {
return "Bark";
}
}
$dog = new Dog();
echo $dog->sound();
- A) Animal sound
- B) Bark
- C) NULL
- D) Error
Answer: B - Which of the following correctly creates an instance of a class named Car?
- A) Car myCar = new Car();
- B) new Car myCar();
- C) $myCar = new Car();
- D) Car: myCar();
Answer: C - What does the following code demonstrate?
class A {
public $x = 5;
public function add($y) {
return $this->x + $y;
}
}
$obj = new A();
echo $obj->add(10);
- A) 5
- B) 10
- C) 15
- D) Error
Answer: C - What is the output of the following code?
class Base {
public function show() {
return "Base Class";
}
}
class Derived extends Base {
public function show() {
return "Derived Class";
}
}
$obj = new Base();
echo $obj->show();
- A) Base Class
- B) Derived Class
- C) NULL
- D) Error
Answer: A - Which access modifier is most restrictive?
- A) public
- B) protected
- C) private
- D) static
Answer: C - What is the default access modifier for class properties in PHP?
- A) public
- B) protected
- C) private
- D) No default
Answer: A - Which method cannot be inherited?
- A) Public method
- B) Protected method
- C) Private method
- D) Static method
Answer: C - What will the following code output?
class Base {
protected function display() {
return "Hello";
}
}
class Derived extends Base {
public function show() {
return $this->display();
}
}
$obj = new Derived();
echo $obj->show();
- A) Hello
- B) Error: Cannot access protected method
- C) NULL
- D) Derived
Answer: A - What is the purpose of the final keyword in a method?
- A) The method cannot be overridden
- B) The method must be abstract
- C) The method can be static
- D) The method can have a body
Answer: A - Which of the following statements about constructors is FALSE?
- A) Constructors can be overloaded
- B) Constructors are called when an object is created
- C) Constructors have the same name as the class
- D) Constructors can take parameters
Answer: A - What is the output of the following code?
class Test {
public static function printMessage() {
return "Hello World!";
}
}
echo Test::printMessage();
- A) Hello World!
- B) Error: Method not found
- C) NULL
- D) Test
Answer: A - Which method would you override to perform actions before an object is destroyed?
- A) __construct()
- B) __destruct()
- C) finalize()
- D) terminate()
Answer: B - How can you call a static method in a class?
- A) object->method();
- B) method();
- C) ClassName::method();
- D) ClassName->method();
Answer: C - What will be the output of the following code?
class MyClass {
public function test() {
return "Test method called";
}
}
$obj = new MyClass();
echo $obj->test();
- A) Test method called
- B) Error: Method not found
- C) NULL
- D) MyClass
Answer: A - What is the main benefit of using OOP?
- A) Improved performance
- B) Reusability of code
- C) Reduced lines of code
- D) Simpler syntax
Answer: B - What will happen if you try to access a private property from a derived class?
- A) It will work without issues
- B) It will throw a fatal error
- C) It will use the parent class's property
- D) It will return NULL
Answer: B - Which of the following can an interface NOT contain?
- A) Method declarations
- B) Constants
- C) Properties
- D) Abstract methods
Answer: C - What will be the output of the following code?
class Alpha {
public function greet() {
return "Hello from Alpha";
}
}
class Beta extends Alpha {
public function greet() {
return "Hello from Beta";
}
}
$obj = new Beta();
echo $obj->greet();
- A) Hello from Alpha
- B) Hello from Beta
- C) NULL
- D) Error
Answer: B - How can you implement multiple inheritance in PHP?
- A) By using classes
- B) By using interfaces
- C) By using traits
- D) By using functions
Answer: C - What does the static keyword signify in PHP?
- A) The method cannot be inherited
- B) The method can only be accessed statically
- C) The property belongs to the class, not instances
- D) Both B and C
Answer: D - Which statement is true about a destructor?
- A) It cannot take parameters
- B) It must have the same name as the class
- C) It can be called multiple times
- D) It is executed at the beginning of a script
Answer: A - What does the keyword interface define in PHP?
- A) A template for classes
- B) A type of class
- C) An abstract method
- D) A property of a class
Answer: A - Which of the following is an example of encapsulation?
- A) Using public properties
- B) Hiding class details from outside
- C) Allowing unrestricted access to all methods
- D) Using multiple inheritance
Answer: B - What is the purpose of parent:: in a child class?
- A) To call a parent class's constructor
- B) To refer to a static method in the parent class
- C) To access a property in the parent class
- D) All of the above
Answer: D - How do you define an abstract class in PHP?
- A) class ClassName { }
- B) abstract class ClassName { }
- C) class ClassName: abstract { }
- D) abstract ClassName { }
Answer: B - What will be the output of the following code?
abstract class Shape {
abstract public function area();
}
class Circle extends Shape {
public function area() {
return pi() * 4; // radius is 2
}
}
$circle = new Circle();
echo $circle->area();
- A) 12.56
- B) 25.13
- C) 6.28
- D) NULL
Answer: B - Which of the following is a valid interface declaration in PHP?
- A) interface MyInterface { }
- B) interface MyInterface { public function myMethod(); }
- C) interface MyInterface extends AnotherInterface { }
- D) All of the above
Answer: D - What is the output of the following code?
class Counter {
private static $count = 0;
public static function increment() {
self::$count++;
}
public static function getCount() {
return self::$count;
}
}
Counter::increment();
Counter::increment();
echo Counter::getCount();
- A) 1
- B) 2
- C) 0
- D) Error: Cannot access private property
Answer: B - What will happen if you do not define a constructor in a class?
- A) An error will occur
- B) A default constructor will be provided
- C) The class cannot be instantiated
- D) Nothing, it will work normally
Answer: B - What does the implements keyword signify in PHP?
- A) A class is inheriting another class
- B) A class is implementing an interface
- C) A class is defining a new method
- D) A class is extending multiple classes
Answer: B - How do you declare a property in a class?
- A) public $propertyName;
- B) property $propertyName;
- C) define($propertyName);
- D) var $propertyName;
Answer: A - What is the default visibility of class methods and properties?
- A) public
- B) private
- C) protected
- D) None
Answer: A - How can you create a class that cannot be instantiated?
- A) By declaring the class as abstract
- B) By using the final keyword
- C) By not defining any methods
- D) By creating a private constructor
Answer: A - What is the purpose of the instanceof operator?
- A) To check if a variable is an object
- B) To check if an object is an instance of a class
- C) To create an instance of a class
- D) To declare an interface
Answer: B - What does the final keyword do when applied to a class?
- A) It prevents the class from being instantiated
- B) It prevents the class from being extended
- C) It prevents the class from having methods
- D) It prevents the class from having properties
Answer: B - What is the output of the following code?
class Vehicle {
public function type() {
return "Vehicle";
}
}
class Car extends Vehicle {
public function type() {
return "Car";
}
}
$obj = new Car();
echo $obj->type();
- A) Vehicle
- B) Car
- C) NULL
- D) Error
Answer: B - Which keyword is used to define a constant in a class?
- A) static
- B) define
- C) const
- D) final
Answer: C - What does the keyword protected mean for class members?
- A) They can be accessed anywhere
- B) They can only be accessed within the class and by derived classes
- C) They cannot be accessed outside the class
- D) They can only be accessed within the same package
Answer: B - What will happen if you try to call a method that doesn't exist in a class?
- A) It will return NULL
- B) It will throw a fatal error
- C) It will ignore the method call
- D) It will return false
Answer: B - Which method is used to get the properties of an object in PHP?
- A) getProperties()
- B) __get()
- C) retrieve()
- D) fetchProperties()
Answer: B - What will be the output of the following code?
class Sample {
public function show() {
return "Hello, World!";
}
}
$obj = new Sample();
echo $obj->show();
- A) Hello, World!
- B) Error: Method not found
- C) NULL
- D) Sample
Answer: A - How do you define an interface that extends another interface?
- A) interface NewInterface extends OldInterface { }
- B) interface NewInterface implements OldInterface { }
- C) interface NewInterface: OldInterface { }
- D) interface NewInterface (OldInterface) { }
Answer: A - What is the output of the following code?
class ParentClass {
public function show() {
return "Parent Class";
}
}
class ChildClass extends ParentClass {
public function show() {
return parent::show() . " - Child Class";
}
}
$obj = new ChildClass();
echo $obj->show();
- A) Parent Class
- B) Parent Class - Child Class
- C) Child Class
- D) NULL
Answer: B - What will the following code output?
class Person {
private $name;
public function setName($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
$person = new Person();
$person->setName("John");
echo $person->getName();
- A) John
- B) NULL
- C) Error: Cannot access private property
- D) Error: Method not found
Answer: A - Which of the following can be an abstract method?
- A) A method with a body
- B) A method without a body
- C) A method with parameters
- D) All of the above
Answer: B - What will be the output of the following code?
interface MyInterface {
public function myFunction();
}
class MyClass implements MyInterface {
public function myFunction() {
return "Hello, World!";
}
}
$obj = new MyClass();
echo $obj->myFunction();
- A) Hello, World!
- B) Error: Method not implemented
- C) NULL
- D) MyClass
Answer: A - What will happen if you try to extend a final class?
- A) It will work normally
- B) It will throw an error
- C) It will inherit the properties
- D) It will return NULL
Answer: B - Which keyword is used to create a singleton pattern in PHP?
- A) static
- B) private
- C) final
- D) protected
Answer: B - How can you access a parent class property from a child class?
- A) By using $this->propertyName
- B) By using parent::propertyName
- C) By using self::propertyName
- D) It is not possible
Answer: B - What is the output of the following code?
class A {
protected $value = 10;
public function getValue() {
return $this->value;
}
}
class B extends A {
public function getValue() {
return $this->value + 5;
}
}
$obj = new B();
echo $obj->getValue();
- A) 10
- B) 15
- C) Error: Cannot access protected property
- D) NULL
Answer: B - What is the purpose of the clone keyword in PHP?
- A) To create a new instance of a class
- B) To copy an object
- C) To define a constant
- D) To destroy an object
Answer: B - How do you declare a method that cannot be overridden?
- A) By using final
- B) By using private
- C) By using protected
- D) By using abstract
Answer: A - What will be the output of the following code?
class Example {
public $data = "Hello";
public function __clone() {
$this->data = "Cloned";
}
}
$original = new Example();
$clone = clone $original;
echo $clone->data;
- A) Hello
- B) Cloned
- C) NULL
- D) Error: Cannot access property
Answer: B - What is the result of the following code?
class Test {
public static $count = 0;
public static function increment() {
self::$count++;
}
}
Test::increment();
echo Test::$count;
- A) 0
- B) 1
- C) NULL
- D) Error
Answer: B - Which of the following is not a valid way to define a class in PHP?
- A) class ClassName { }
- B) class ClassName extends ParentClass { }
- C) class ClassName implements InterfaceName { }
- D) class ClassName inherits ParentClass { }
Answer: D - What is the purpose of the __construct method?
- A) To define a property
- B) To define a method
- C) To initialize an object
- D) To destroy an object
Answer: C - Which of the following is the correct way to call a parent class constructor? - A) parent::__construct(); - B) self::__construct(); - C) static::__construct(); - D) class::__construct();
Answer: A
Summary
This quiz covers a broad range of topics related to Object-Oriented Programming (OOP) in PHP, including concepts such as inheritance, encapsulation, polymorphism, interfaces, and constructors. Each question is designed to assess understanding of PHP OOP principles, syntax, and best practices.