MCQs on Class 19 - Introduction to Object-Oriented Programming (OOP) - Part 1
MCQs on Object-Oriented Programming in PHP
- What does OOP stand for?
- A) Object-Oriented Programming
- B) Open-Oriented Programming
- C) Original Object Programming
- D) Objective Object Programming
Answer: A - Which of the following is NOT a core principle of OOP?
- A) Inheritance
- B) Encapsulation
- C) Abstraction
- D) Compilation
Answer: D - In PHP, how do you declare a class?
- A) class MyClass {};
- B) Class MyClass {};
- C) myclass {};
- D) declare class MyClass {};
Answer: A - What keyword is used to create a new instance of a class?
- A) class
- B) new
- C) create
- D) instance
Answer: B - Which of the following correctly demonstrates inheritance?
- A) class Dog extends Animal {}
- B) class Dog inherits Animal {}
- C) class Dog is Animal {}
- D) class Dog extends from Animal {}
Answer: A - What is encapsulation?
- A) Hiding the implementation details of a class.
- B) Sharing code between classes.
- C) Using interfaces to interact with classes.
- D) Creating new classes from existing ones.
Answer: A - How do you declare a property in a class?
- A) public $propertyName;
- B) $propertyName public;
- C) property public $propertyName;
- D) var $propertyName;
Answer: A - Which of the following is the correct way to define a method in PHP?
- A) function myMethod() {}
- B) method myMethod() {}
- C) define myMethod() {}
- D) create myMethod() {}
Answer: A - What is polymorphism in OOP?
- A) The ability to store multiple types of data.
- B) The ability to define methods in multiple ways.
- C) The ability to change the behavior of an object.
- D) The ability to extend classes.
Answer: B - What is the purpose of the constructor method in a class?
- A) To create a static method.
- B) To initialize object properties.
- C) To destroy an object.
- D) To create a new class.
Answer: B - How do you define a constructor in PHP?
- A) function __construct() {}
- B) constructor() {}
- C) function construct() {}
- D) class constructor() {}
Answer: A - What does the parent keyword do?
- A) Accesses a child class.
- B) Calls a method from the parent class.
- C) Creates a new parent class.
- D) Declares a property in the parent class.
Answer: B - Which of the following is an example of method overriding?
- A) A subclass has a method with the same name as a method in its parent class.
- B) A subclass adds new properties to its parent class.
- C) A class inherits multiple properties from multiple classes.
- D) A class creates an instance of another class.
Answer: A - What is an abstract class?
- A) A class that cannot be instantiated directly.
- B) A class that contains only abstract methods.
- C) A class that cannot be inherited.
- D) A class that has a constructor.
Answer: A - What keyword is used to define an abstract method?
- A) abstract
- B) virtual
- C) static
- D) define
Answer: A - What is an interface in PHP?
- A) A class that contains only static methods.
- B) A blueprint for classes with only abstract methods.
- C) A type of class that cannot be extended.
- D) A class that defines properties and methods.
Answer: B - How do you implement an interface in a class?
- A) class MyClass implements MyInterface {}
- B) class MyClass extends MyInterface {}
- C) class MyClass inherits MyInterface {}
- D) class MyClass uses MyInterface {}
Answer: A - Which of the following allows a class to use properties and methods of another class?
- A) Inheritance
- B) Encapsulation
- C) Polymorphism
- D) Abstraction
Answer: A - What is a static method?
- A) A method that cannot be accessed without an object.
- B) A method that belongs to the class rather than any object instance.
- C) A method that can only be used in abstract classes.
- D) A method that cannot return a value.
Answer: B - How do you declare a static property in a class?
- A) public static $propertyName;
- B) static public $propertyName;
- C) var static $propertyName;
- D) static $propertyName;
Answer: A - Which of the following is NOT a feature of OOP?
- A) Data hiding
- B) Code reusability
- C) Global variables
- D) Method overloading
Answer: C - What does the final keyword do?
- A) Prevents a class from being instantiated.
- B) Prevents a method from being overridden.
- C) Prevents a property from being modified.
- D) Marks a class as abstract.
Answer: B - Which of the following is true about constructors?
- A) A class can have multiple constructors.
- B) A constructor is called when an object is created.
- C) A constructor cannot have parameters.
- D) A constructor is defined with the function keyword.
Answer: B - How do you declare a method that cannot be overridden?
- A) final function methodName() {}
- B) static function methodName() {}
- C) private function methodName() {}
- D) abstract function methodName() {}
Answer: A - Which of the following correctly represents encapsulation?
- A) public methods accessing private properties.
- B) Private properties accessed directly by any class.
- C) Public properties accessed only through public methods.
- D) Methods being reused in multiple classes.
Answer: A - What is the purpose of the self keyword in PHP?
- A) To refer to the current instance of an object.
- B) To call a static method of the class.
- C) To access parent class methods.
- D) To declare static properties.
Answer: B - Which of the following is NOT a benefit of OOP?
- A) Improved code maintainability
- B) Increased performance
- C) Enhanced code reusability
- D) Better data protection
Answer: B - What happens if a child class does not implement an abstract method from its parent class?
- A) The child class will compile successfully.
- B) The child class will throw an error at runtime.
- C) The child class will be abstract.
- D) The parent class will be modified.
Answer: C - Which of the following is an example of a concrete class?
- A) A class that contains only abstract methods.
- B) A class that can be instantiated.
- C) A class that has at least one abstract method.
- D) A class that cannot have properties.
Answer: B - How do you access a static method from outside the class?
- A) MyClass::myMethod();
- B) MyClass->myMethod();
- C) MyClass.myMethod();
- D) myMethod(MyClass);
Answer: A - What will be the output of the following code?
php
Copy code
class A {
public function hello() {
return "Hello from A";
}
}
class B extends A {
public function hello() {
return "Hello from B";
}
}
$b = new B();
echo $b->hello();
- A) Hello from A
- B) Hello from B
- C) Error
- D) Hello
Answer: B - Which of the following statements is true about interfaces?
- A) An interface can contain properties.
- B) A class can implement multiple interfaces.
- C) An interface can provide method implementations.
- D) Interfaces can have constructors.
Answer: B - What keyword is used to indicate that a class cannot be extended?
- A) abstract
- B) final
- C) static
- D) private
Answer: B - What is a trait in PHP?
- A) A way to create multiple instances of a class.
- B) A mechanism for code reuse in single inheritance.
- C) A class that cannot be instantiated.
- D) A type of abstract class.
Answer: B - How do you use a trait in a class?
- A) class MyClass uses MyTrait {}
- B) class MyClass implements MyTrait {}
- C) class MyClass extends MyTrait {}
- D) class MyClass { use MyTrait; }
Answer: D - What is the purpose of the __destruct() method?
- A) To initialize class properties.
- B) To clean up resources when an object is destroyed.
- C) To override a parent method.
- D) To define an interface method.
Answer: B - Which of the following is true about dynamic binding?
- A) Method calls are resolved at compile time.
- B) Method calls are resolved at runtime.
- C) Static methods cannot use dynamic binding.
- D) All method calls are statically bound.
Answer: B - Which of the following is true about constructor overloading in PHP?
- A) PHP supports constructor overloading.
- B) PHP does not support constructor overloading.
- C) Constructor overloading is achieved through interfaces.
- D) Constructor overloading is achieved through traits.
Answer: B - What does the static keyword do when applied to a property?
- A) Makes the property non-static.
- B) Makes the property accessible from the class only.
- C) Allows the property to be shared among all instances of the class.
- D) Prevents the property from being accessed.
Answer: C - Which of the following best describes "composition"?
- A) A class containing instances of other classes.
- B) A class extending another class.
- C) A class implementing an interface.
- D) A class using traits.
Answer: A - What is the result of attempting to instantiate an abstract class?
- A) It will throw an error.
- B) It will create an instance of the class.
- C) It will execute the constructor.
- D) It will call the destructor.
Answer: A - Which of the following is a feature of the protected access modifier?
- A) It can be accessed from outside the class.
- B) It can be accessed by subclasses and the class itself.
- C) It can be accessed by any class.
- D) It is the same as public.
Answer: B - How can you prevent a class from being instantiated?
- A) Declare it as final.
- B) Declare it as abstract.
- C) Declare it as static.
- D) Declare it as private.
Answer: B - Which of the following is NOT an example of polymorphism?
- A) Method overloading
- B) Method overriding
- C) Using different classes with the same method name
- D) Static method calls
Answer: D - What will the following code output?
php
Copy code
class Base {
public function show() {
return "Base";
}
}
class Derived extends Base {
public function show() {
return "Derived";
}
}
$obj = new Derived();
echo $obj->show();
- A) Base
- B) Derived
- C) Error
- D) NULL
Answer: B - What is the use of the abstract keyword?
- A) To create a class that cannot be instantiated.
- B) To create an interface.
- C) To define a final method.
- D) To declare a static property.
Answer: A - What happens when you call the parent constructor?
- A) It initializes the parent class properties.
- B) It overrides the parent class methods.
- C) It prevents the parent class from being instantiated.
- D) It creates a new instance of the parent class.
Answer: A - What will be the output of the following code?
php
Copy code
class A {
protected function test() {
return "A";
}
}
class B extends A {
public function test() {
return "B";
}
}
$b = new B();
echo $b->test();
- A) A
- B) B
- C) Error
- D) NULL
Answer: B - Which of the following describes "method chaining"?
- A) Calling methods of multiple classes in a single line.
- B) Returning the object from a method to call another method on it.
- C) Using multiple constructors in a class.
- D) Declaring multiple methods in a class.
Answer: B - How do you define a method in a trait?
- A) function myMethod() {}
- B) public function myMethod() {}
- C) trait myMethod() {}
- D) method myMethod() {}
Answer: A - What is the purpose of get_class() function in PHP?
- A) To get the name of the parent class.
- B) To get the name of a class of an object.
- C) To get the properties of a class.
- D) To get the methods of a class.
Answer: B - What does the __call() magic method do?
- A) It is triggered when an object is created.
- B) It is called when an inaccessible method is invoked.
- C) It is called when an object is destroyed.
- D) It is called when a static method is invoked.
Answer: B - What is an instance variable?
- A) A variable defined at the class level.
- B) A variable that holds the state of an object.
- C) A variable that is static.
- D) A variable that is shared among all instances.
Answer: B - What will the following code output?
php
Copy code
class C {
public function say() {
return "Hello";
}
}
$c = new C();
echo $c->say();
- A) Hello
- B) Error
- C) NULL
- D) "say"
Answer: A - What is method overloading?
- A) Defining multiple methods with the same name and different parameters.
- B) Defining multiple classes with the same name.
- C) Defining multiple constructors in a class.
- D) Modifying existing methods.
Answer: A - Which of the following is true about the static keyword when used in a method?
- A) It can be accessed through an instance of the class.
- B) It can be accessed directly through the class name.
- C) It can be overridden by child classes.
- D) It cannot return values.
Answer: B - How do you access the properties of a parent class from a child class?
- A) $this->propertyName;
- B) parent::propertyName;
- C) $parent->propertyName;
- D) parent::$propertyName;
Answer: D - What will be the output of the following code?
php
Copy code
class Test {
public $var = "Test";
public function __construct() {
$this->var = "Constructed";
}
}
$obj = new Test();
echo $obj->var;
- A) Test
- B) Constructed
- C) NULL
- D) Error
Answer: B - What is a singleton class?
- A) A class that can have multiple instances.
- B) A class that restricts instantiation to a single object.
- C) A class that cannot be inherited.
- D) A class with only static methods.
Answer: B - Which of the following is NOT a type of relationship in OOP?
- A) Association
- B) Aggregation
- C) Inheritance
- D) Function
Answer: D - What does the final keyword prevent?
- A) It prevents a method from being static.
- B) It prevents a class from being inherited.
- C) It prevents a property from being public.
- D) It prevents a method from being called.
Answer: B - Which of the following describes "inheritance"?
- A) A way to define a new class based on an existing class.
- B) A way to combine multiple classes into one.
- C) A way to create an interface.
- D) A way to encapsulate class data.
Answer: A - What will be the output of the following code?
php
Copy code
class Example {
public static function test() {
return "Hello from static";
}
}
echo Example::test();
- A) Hello from static
- B) Error
- C) NULL
- D) "test"
Answer: A - What will happen if you call a non-static method statically?
- A) It will execute normally.
- B) It will throw an error.
- C) It will call the method on the parent class.
- D) It will create a new instance of the class.
Answer: B - What is an interface in PHP?
- A) A class that cannot be instantiated.
- B) A contract that defines methods that implementing classes must have.
- C) A way to define static methods.
- D) A way to create traits.
Answer: B - How do you declare an interface in PHP?
- A) interface MyInterface {}
- B) class MyInterface {}
- C) trait MyInterface {}
- D) function MyInterface {}
Answer: A - What happens if a class implements an interface but does not define all its methods?
- A) The code will run without issues.
- B) It will throw a runtime error.
- C) It will throw a compile-time error.
- D) It will automatically define the methods.
Answer: C - What is the use of the parent keyword?
- A) To refer to static properties of the parent class.
- B) To access methods and properties of the parent class.
- C) To prevent a class from being inherited.
- D) To create a new instance of the parent class.
Answer: B - Which of the following will NOT create an instance of a class?
- A) new ClassName();
- B) ClassName::create();
- C) ClassName $obj;
- D) ClassName::instance();
Answer: C - How do you declare a static method in a class?
- A) static function methodName() {}
- B) public static function methodName() {}
- C) function static methodName() {}
- D) method static methodName() {}
Answer: B - What will happen if you try to use the static keyword in a method not defined as static?
- A) It will throw an error.
- B) It will work normally.
- C) It will return NULL.
- D) It will create a new instance of the class.
Answer: A - Which of the following is NOT a characteristic of object-oriented programming?
- A) Encapsulation
- B) Polymorphism
- C) Inheritance
- D) Recursion
Answer: D - What does encapsulation mean in OOP?
- A) Hiding the internal state of an object.
- B) Inheriting properties from another class.
- C) Using multiple constructors in a class.
- D) Defining multiple methods with the same name.
Answer: A - How can you prevent a property from being accessible outside of a class?
- A) Declare it as public.
- B) Declare it as protected.
- C) Declare it as private.
- D) Declare it as static.
Answer: C - What does the __get() magic method do?
- A) It is called when an inaccessible property is accessed.
- B) It is called when an object is created.
- C) It is called when an object is destroyed.
- D) It is called when a static method is invoked.
Answer: A - Which of the following cannot be inherited?
- A) Public methods
- B) Private methods
- C) Protected methods
- D) Static methods
Answer: B - What will happen if you declare a method as final?
- A) It can be overridden in child classes.
- B) It cannot be overridden in child classes.
- C) It can only be called statically.
- D) It can only be accessed by the parent class.
Answer: B - Which of the following is an example of an abstract class?
- A) class Shape { abstract function area(); }
- B) class Circle { public function area() {} }
- C) class Square { private function area() {} }
- D) class Triangle { final function area() {} }
Answer: A - What is the main advantage of using interfaces?
- A) They allow multiple inheritance.
- B) They provide a blueprint for classes.
- C) They allow the definition of static methods.
- D) They can contain properties.
Answer: B - What will be the output 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) Error
- D) NULL
Answer: B - What is the purpose of the __set() magic method?
- A) It is called when an object is created.
- B) It is called when an inaccessible property is set.
- C) It is called when an object is destroyed.
- D) It is called when a static method is invoked.
Answer: B - How do you create a copy of an object in PHP?
- A) Using the clone keyword.
- B) Using the copy keyword.
- C) Using the new keyword.
- D) Using the duplicate keyword.
Answer: A - Which of the following is true about the final keyword when used in methods?
- A) It prevents the method from being called.
- B) It allows the method to be overridden.
- C) It prevents the method from being overridden.
- D) It requires the method to be static.
Answer: C - What is the main benefit of using inheritance?
- A) Code reuse and organization.
- B) Faster execution time.
- C) Improved security.
- D) Easier debugging.
Answer: A - What will happen if you try to access a private method from a child class?
- A) It will execute normally.
- B) It will throw an error.
- C) It will return NULL.
- D) It will call the parent class's method.
Answer: B - Which of the following correctly defines a constructor in PHP?
- A) function __construct() {}
- B) function constructor() {}
- C) public __construct() {}
- D) public function __construct() {}
Answer: D - What is the purpose of the __toString() magic method?
- A) To convert an object to an array.
- B) To convert an object to a string.
- C) To define a static method.
- D) To define an interface method.
Answer: B - How do you call a parent class method from a child class?
- A) parent::methodName();
- B) $this->methodName();
- C) $parent->methodName();
- D) methodName();
Answer: A - Which of the following can be used to define an abstract method?
- A) In an abstract class.
- B) In a concrete class.
- C) In a trait.
- D) In an interface.
Answer: A - What does the self keyword refer to?
- A) The parent class.
- B) The current instance of a class.
- C) The current class context.
- D) The static method of a class.
Answer: C - What will be the output of the following code?
php
Copy code
class Foo {
public function bar() {
return "Foo";
}
}
class Baz extends Foo {
public function bar() {
return "Baz";
}
}
$baz = new Baz();
echo $baz->bar();
- A) Foo
- B) Baz
- C) Error
- D) NULL
Answer: B - What is the purpose of the __clone() magic method?
- A) To create a new instance of a class.
- B) To initialize the object.
- C) To handle cloning of objects.
- D) To destruct an object.
Answer: C - Which of the following is true about traits?
- A) Traits can be instantiated.
- B) Traits can contain properties.
- C) Traits can define constructors.
- D) Traits support multiple inheritance.
Answer: D - How can you include a trait in a class?
- A) use TraitName;
- B) include TraitName;
- C) import TraitName;
- D) require TraitName;
Answer: A - What will be the output of the following code?
php
Copy code
trait Hello {
public function sayHello() {
return "Hello";
}
}
class Greeter {
use Hello;
}
$greeter = new Greeter();
echo $greeter->sayHello();
- A) Hello
- B) Error
- C) NULL
- D) "sayHello"
Answer: A - Which of the following statements about objects is FALSE?
- A) Objects can contain properties and methods.
- B) Objects are instances of classes.
- C) Objects can be copied by reference.
- D) Objects cannot be serialized.
Answer: D - How do you define a final class?
- A) final class ClassName {}
- B) class final ClassName {}
- C) class ClassName final {}
- D) class ClassName { final }
Answer: A - What is a characteristic of a constructor?
- A) It cannot be overloaded.
- B) It is called before an object is created.
- C) It must return a value.
- D) It is called only once per class.
Answer: A - Which of the following will NOT be inherited by a child class?
- A) Public methods
- B) Protected methods
- C) Static methods
- D) Private methods
Answer: D - What will be the output of the following code? ```php class ParentClass { public function display() { return "Hello from ParentClass"; } }
java
Copy code
class ChildClass extends ParentClass {
php
Copy code
public function display() {
return "Hello from ChildClass";
}
}
$child = new ChildClass();
echo $child->display();
```
- A) Hello from ParentClass
- B) Hello from ChildClass
- C) Error
- D) NULL
**Answer:** B