MCQs on Class 20: Introduction to Object-Oriented Programming (OOP) - Part 2

Rashmi Mishra
0

 MCQs on Class 20

 Introduction to Object-Oriented Programming (OOP) Part 2


  1. What does OOP stand for?
    • A) Object-Oriented Programming
    • B) Object-Oriented Procedure
    • C) Object Oriented Protocol
    • D) Object-Oriented Project
      Answer: A
  2. Which of the following is NOT a core principle of OOP?
    • A) Encapsulation
    • B) Inheritance
    • C) Compilation
    • D) Polymorphism
      Answer: C
  3. 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
  4. 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
  5. Which access modifier allows a property to be accessed only within its own class?
    • A) public
    • B) private
    • C) protected
    • D) static
      Answer: B
  6. 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
  7. 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
  8. Which method is automatically called when an object is created?
    • A) Destructor
    • B) Getter
    • C) Constructor
    • D) Setter
      Answer: C
  9. 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
  10. 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
  11. Which symbol is commonly used to denote inheritance in UML diagrams?
    • A) Arrow
    • B) Solid line
    • C) Dashed line
    • D) Circle
      Answer: A
  12. 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
  13. 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
  14. Which keyword is used to inherit a class in PHP?
    • A) extends
    • B) inherits
    • C) implements
    • D) uses
      Answer: A
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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
  28. 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
  1. Which keyword is used to define an interface in PHP?
    • A) interface
    • B) abstract
    • C) class
    • D) implements
      Answer: A
  2. 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
  3. 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
  4. 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
  1. 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
  2. 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
  3. 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
  4. 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
  1. 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
  2. How do you use a trait in PHP?
    • A) use TraitName;
    • B) include TraitName;
    • C) extends TraitName;
    • D) implements TraitName;
      Answer: A
  3. 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
  4. Which of the following can be a property of a class?
    • A) Method
    • B) Variable
    • C) Constant
    • D) All of the above
      Answer: D
  5. 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
  6. 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
  7. Which method will be called when an object is destroyed?
    • A) __construct()
    • B) __destruct()
    • C) finalize()
    • D) terminate()
      Answer: B
  8. 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
  1. 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
  2. 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
  1. 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
  1. Which access modifier is most restrictive?
    • A) public
    • B) protected
    • C) private
    • D) static
      Answer: C
  2. What is the default access modifier for class properties in PHP?
    • A) public
    • B) protected
    • C) private
    • D) No default
      Answer: A
  3. Which method cannot be inherited?
    • A) Public method
    • B) Protected method
    • C) Private method
    • D) Static method
      Answer: C
  4. 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
  1. 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
  2. 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
  3. 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
  1. Which method would you override to perform actions before an object is destroyed?
    • A) __construct()
    • B) __destruct()
    • C) finalize()
    • D) terminate()
      Answer: B
  2. How can you call a static method in a class?
    • A) object->method();
    • B) method();
    • C) ClassName::method();
    • D) ClassName->method();
      Answer: C
  3. 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
  1. 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
  2. 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
  3. Which of the following can an interface NOT contain?
    • A) Method declarations
    • B) Constants
    • C) Properties
    • D) Abstract methods
      Answer: C
  4. 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
  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  1. 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
  2. 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
  1. 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
  2. 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
  3. How do you declare a property in a class?
    • A) public $propertyName;
    • B) property $propertyName;
    • C) define($propertyName);
    • D) var $propertyName;
      Answer: A
  4. What is the default visibility of class methods and properties?
    • A) public
    • B) private
    • C) protected
    • D) None
      Answer: A
  5. 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
  6. 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
  7. 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
  8. 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
  1. Which keyword is used to define a constant in a class?
    • A) static
    • B) define
    • C) const
    • D) final
      Answer: C
  2. 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
  3. 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
  4. Which method is used to get the properties of an object in PHP?
    • A) getProperties()
    • B) __get()
    • C) retrieve()
    • D) fetchProperties()
      Answer: B
  5. 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
  1. 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
  2. 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
  1. 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
  1. 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
  2. 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
  1. 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
  2. Which keyword is used to create a singleton pattern in PHP?
    • A) static
    • B) private
    • C) final
    • D) protected
      Answer: B
  3. 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
  4. 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
  1. 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
  2. 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
  3. 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
  1. 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
  1. 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
  2. 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
  3. 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.


Tags

Post a Comment

0Comments

Post a Comment (0)