Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

Which magic method is used to implement overloading in PHP?(a) __call(b) __invoke(c) __wakeup(d) __unsetI have been asked this question by my college professor while I was bunking the class.This intriguing question originated from Advanced Object-Oriented PHP topic in division Object-Oriented PHP of PHP

Answer»

The correct ANSWER is (a) __call

Best explanation: When a class IMPLEMENTS __call(), then an object of that class is called with a method that doesn’t EXIST, __call() is called INSTEAD.

2.

Which feature allows us to call more than one method or function of the class in single instruction?(a) Typecasting(b) Method Including(c) Method adding(d) Method chainingThis question was posed to me during an interview.Question is from Advanced Object-Oriented PHP topic in division Object-Oriented PHP of PHP

Answer»

Right option is (d) Method chaining

To elaborate: When MANY METHODS are called in a single INSTRUCTION in PHP, it is called method chaining. FOLLOWING is a basic example of method chaining in php:

3.

If your object must inherit behavior from a number of sources you must use a/an(a) Interface(b) Object(c) Abstract class(d) Static classThe question was asked in class test.Question is from Advanced Object-Oriented PHP topic in chapter Object-Oriented PHP of PHP

Answer»

Correct answer is (a) INTERFACE

Easy explanation: An interface in PHP consists of METHODS that have no implementations, i.e. the interface methods are abstract methods. The methods in the interfaces must have public VISIBILITY scope. The interfaces are DIFFERENT from classes as the class can inherit from one class only whereas the class can IMPLEMENT one or more interfaces.

4.

Which method is used to tweak an object’s cloning behavior?(a) clone()(b) __clone()(c) _clone(d) object_clone()This question was posed to me in unit test.Question is taken from Advanced Object-Oriented PHP in chapter Object-Oriented PHP of PHP

Answer»

Right CHOICE is (b) __clone()

EASIEST EXPLANATION: A COPY of an OBJECT is created by using the clone keyword, which calls the object’s __clone() method.

5.

If one intends to create a model that will be assumed by anumber of closely related objects, which class must be used?(a) Normal class(b) Static class(c) Abstract class(d) InterfaceI got this question in an international level competition.This is a very interesting question from Advanced Object-Oriented PHP in chapter Object-Oriented PHP of PHP

Answer»

Correct choice is (c) Abstract class

To explain: The abstract classes are the classes in which at least ONE method need to be abstract. Abstract classes in PHP are declared with the help of KEYWORD abstract. The use of class abstract are that all base classes implementing abstract class should give implementation of abstract methods declared in parent class.

6.

Which one of the following is the right way to clone an object?(a) _clone(targetObject);(b) destinationObject = clone targetObject;(c) destinationObject = _clone(targetObject);(d) destinationObject = clone(targetObject);This question was addressed to me in exam.I'm obligated to ask this question of Advanced Object-Oriented PHP topic in chapter Object-Oriented PHP of PHP

Answer»

Correct choice is (b) destinationObject = clone targetObject;

Easy EXPLANATION: You can clone an OBJECT by prefacing it with the clone KEYWORD. A copy of an object is created by using the clone keyword. $copy_of_object = clone $object;

7.

Which version of PHP introduced the advanced concepts of OOP?(a) PHP 4(b) PHP 5(c) PHP 5.3(d) PHP 6I had been asked this question during an internship interview.This question is from Advanced Object-Oriented PHP in chapter Object-Oriented PHP of PHP

Answer»

The correct choice is (b) PHP 5

For explanation: ADVANCED concepts of OOP were introduced in PHP version 5.

8.

Which keyword allows class members (methods and properties) to be used without needing to instantiate a new instance of the class?(a) protected(b) final(c) static(d) privateThe question was asked in exam.This intriguing question originated from Basics of Object-Oriented PHP- 2 in division Object-Oriented PHP of PHP

Answer»

The correct answer is (c) static

Explanation: Sometimes it is very handy to access the methods and properties in terms of a CLASS RATHER than an object. But this can be done through static keyword. Any method declared as ‘static’ can be ACCESSED WITHOUT the CREATION of an object.

9.

Which keyword is used to refer to properties or methods within the class itself?(a) private(b) public(c) protected(d) $thisThis question was posed to me in an interview for internship.This question is from Basics of Object-Oriented PHP- 2 in section Object-Oriented PHP of PHP

Answer»

Correct choice is (d) $this

Easy explanation: In PHP, the SELF and ‘this’ keyword are used to refer the class MEMBERS within the SCOPE of a class itself. The class members can be EITHER variables or functions.

10.

Which one of the following keyword is used to inherit our subclass into a superclass?(a) extends(b) implements(c) inherit(d) includeI got this question in semester exam.This question is from Basics of Object-Oriented PHP- 2 topic in section Object-Oriented PHP of PHP

Answer»

The correct option is (a) extends

Explanation: When we EXTEND a CLASS then the subclass will INHERIT all the public and PROTECTED methods from the parent class.

The keyword IMPLEMENTS are used with interfaces. With inheritance, we use the keyword extends.

11.

Which one of the following functions is used to determine object type?(a) obj_type()(b) type()(c) is_a()(d) is_obj()The question was posed to me by my college director while I was bunking the class.I want to ask this question from Basics of Object-Oriented PHP- 2 topic in division Object-Oriented PHP of PHP

Answer»

The correct OPTION is (c) is_a()

EASY explanation: The is_a() function returns true if object belongs to a class TYPE or if it belongs to a class that is a child of that class. Or else false is RETURNED.

12.

Which one of the following functions is used to determine whether a class exists?(a) exist()(b) exist_class()(c) class_exist()(d) __exist()This question was addressed to me by my college professor while I was bunking the class.My enquiry is from Basics of Object-Oriented PHP- 2 in chapter Object-Oriented PHP of PHP

Answer» RIGHT CHOICE is (c) class_exist()

To elaborate: The class_exist() FUNCTION returns true or false according to whether the CLASS exists within the currently EXECUTING script content.
13.

Which method scope prevents a method from being overridden by a subclass?(a) Abstract(b) Protected(c) Final(d) StaticI got this question in an online interview.The origin of the question is Basics of Object-Oriented PHP- 2 in chapter Object-Oriented PHP of PHP

Answer»

The correct ANSWER is (C) Final

The BEST I can EXPLAIN: When we declare a method is as final then it is not possible to override that method. Methods should not be overridden DUE to some security or any other reasons.

14.

Which version of PHP introduced the instanceof keyword?(a) PHP 4(b) PHP 5(c) PHP 5.3(d) PHP 6I had been asked this question during an interview for a job.Question is from Basics of Object-Oriented PHP- 2 topic in portion Object-Oriented PHP of PHP

Answer»

Correct answer is (B) PHP 5

Explanation: Using instanceof keyword we can determine WHETHER an object is an instance of a CLASS. $manager = new Employee() … if ($manager instanceof Employee) ECHO “True”;

15.

PHP recognizes constructors by the name _________(a) classname()(b) _construct()(c) function _construct()(d) function __construct()This question was addressed to me in my homework.This is a very interesting question from Basics of Object-Oriented PHP- 2 topic in section Object-Oriented PHP of PHP

Answer»

The correct OPTION is (d) function __construct()

Best explanation: A DOUBLE underscore FOLLOWED by the CONSTRUCT keyword. Its syntax is function __construct ([ argument1, argument2,…..]) { Class Initialization code }.

16.

Which one of the following is the right way to invoke a method?(a) $object->methodName();(b) object->methodName();(c) object::methodName();(d) $object::methodName();This question was addressed to me by my school teacher while I was bunking the class.This intriguing question comes from Basics of Object-Oriented PHP- 1 topic in portion Object-Oriented PHP of PHP

Answer»

Correct choice is (a) $OBJECT->methodName();

The EXPLANATION is: “->” is a dynamic class METHOD INVOCATION in PHP.

17.

Which one of the following is the right way to call a class constant, given that the class is mathFunction?(a) echo PI;(b) echo mathFunction->PI;(c) echo mathFunction::PI;(d) echo mathFunction=PI;I got this question during an online exam.My doubt stems from Basics of Object-Oriented PHP- 1 topic in section Object-Oriented PHP of PHP

Answer» RIGHT answer is (C) ECHO mathFunction::PI;

To elaborate: The SCOPE Resolution Operator “::” is a token that allows access to static, constant, and overridden properties or methods of a CLASS.
18.

Which one of the following is the right way to define a constant?(a) constant PI = “3.1415”;(b) const $PI = “3.1415”;(c) constant PI = ‘3.1415’;(d) const PI = ‘3.1415’;I had been asked this question by my school teacher while I was bunking the class.I need to ask this question from Basics of Object-Oriented PHP- 1 topic in section Object-Oriented PHP of PHP

Answer»

Correct answer is (d) CONST PI = ‘3.1415’;

Explanation: Class CONSTANTS are created LIKE: const NAME = ‘VALUE’;

19.

Which one of the following can be used to instantiate an object in PHP assuming class name to be Foo?(a) $obj = new $foo;(b) $obj = new foo;(c) $obj = new foo ();(d) obj = new foo ();The question was posed to me in homework.This intriguing question comes from Basics of Object-Oriented PHP- 1 topic in division Object-Oriented PHP of PHP

Answer»

Correct answer is (c) $obj = NEW foo ();

BEST explanation: To create a new OBJECT in PHP we can USE the new statement to instantiate a CLASS.

20.

Which one of the following property scopes is not supported by PHP?(a) friendly(b) final(c) public(d) staticThe question was posed to me in an interview.Question is taken from Basics of Object-Oriented PHP- 1 in section Object-Oriented PHP of PHP

Answer»

Right option is (a) friendly

To explain: PHP supports FIVE class property SCOPES: PUBLIC, private, protected, final and STATIC.

21.

The practice of creating objects based on predefined classes is often referred to as ______________(a) class creation(b) object creation(c) object instantiation(d) class instantiationThis question was addressed to me in an online quiz.I want to ask this question from Basics of Object-Oriented PHP- 1 topic in chapter Object-Oriented PHP of PHP

Answer»

Correct ANSWER is (d) class INSTANTIATION

To EXPLAIN: In object-oriented programming, classes are the blueprints of php OBJECTS. Classes do not actually become objects until instantiation is done. When someone instantiates a class, it creates an INSTANCE of it, thus creating the object. In other words, instantiation is the process of creating an instance of an object in memory.

22.

Which of the following term originates from the Greek language that means “having multiple forms,” defines OOP’s ability to redefine, a class’s characteristics?(a) Abstraction(b) Polymorphism(c) Inheritance(d) DifferentialI have been asked this question in an international level competition.The doubt is from Basics of Object-Oriented PHP- 1 topic in chapter Object-Oriented PHP of PHP

Answer»

Correct choice is (b) Polymorphism

For explanation I would say: The word polymorphism is DERIVED from Greek word poly which MEANS “many” and morphism which means the property which helps US to assign more than ONE property.

23.

The practice of separating the user from the true inner workings of an application through well-known interfaces is known as _________(a) Polymorphism(b) Inheritance(c) Encapsulation(d) AbstractionI have been asked this question in an interview for internship.The query is from Basics of Object-Oriented PHP- 1 topic in portion Object-Oriented PHP of PHP

Answer»

Correct CHOICE is (C) ENCAPSULATION

The best I can explain: In object-oriented PHP encapsulation is a CONCEPT of wrapping up or binding up the data MEMBERS and methods in a single module.