Sample questions
Question 1Medium
Which method is used to tweak an object's cloning behavior?
clone()__clone()_cloneobject_clone()
Answer: B. __clone()
Explanation
An object copy is created by using the clone keyword (which calls the object's __clone() method if possible).
e.g.
void __clone ( void )
Step-by-step solution
- The question asks for the method that customises what happens when an object is cloned.
- PHP calls the object's clone method when the clone keyword is used.
- The correct answer is option B, clone.
Why the other options are wrong
- A. Clone is a keyword, not the method name.
- C. _clone lacks the double underscore that marks a magic method.
- D. Object_clone is not the magic method.
Tip: PHP magic methods start with a double underscore.
Question 2Medium
Which of the following advanced OOP features is/are not supported by PHP?
- i) Method overloading
- ii) Multiple Inheritance
- iii) Namespaces
- iv) Object Cloning
- All of the mentioned
- None of the mentioned
- i) and ii)
- iii) and iv)
Answer: C. i) and ii)
Explanation
1.When two classes have a common function which are totally different then there will be a confusion for choosing which function to use. To avoid this confusion there is no multiple inheritance in PHP.
2. Class method overloading is different in PHP than in many other languages. PHP uses the same word but it describes a different pattern.
The advanced OOP features are: Object cloning, Inheritance, Interfaces, Abstract classes, and Namespaces.
Step-by-step solution
- PHP does not support method overloading as other languages do.
- PHP also does not support multiple inheritance directly.
- Namespaces are supported in PHP.
- Object cloning is supported in PHP.
- So, the answer is i) and ii).
Why the other options are wrong
- A. All of the mentioned is wrong because namespaces and object cloning are supported by PHP.
- B. None of the mentioned is wrong because method overloading and multiple inheritance are not supported.
- D. iii) and iv) are both supported in PHP, namespaces and object cloning, so they are not unsupported.
Tip: Remember: PHP doesn't support method overloading or multiple inheritance.
Question 3Medium
How many predefined exceptions does SPL provide access to?
- 13
- 14
- 15
- 16
Answer: A. 13
Explanation
SPL provides 13 predefined exceptions: BadFunctionCallException, BadMethodCallException, DomainException, InvalidArgumentException, LengthException, LogicException, OutOfBoundsException, OutOfRangeException, OverflowException, RangeException, RuntimeException, UnderflowException and UnexpectedValueException.
Step-by-step solution
- SPL (Standard PHP Library) defines 13 predefined exception classes.
- Logic family (7): LogicException, BadFunctionCallException, BadMethodCallException, DomainException, InvalidArgumentException, LengthException, OutOfRangeException.
- Runtime family (6): RuntimeException, OutOfBoundsException, OverflowException, RangeException, UnderflowException, UnexpectedValueException.
- 7 + 6 = 13, so the answer is 13.
Why the other options are wrong
- B. 14 counts one class more than SPL has.
- C. 15 is too many; SPL has 13 exception classes.
- D. 16 is too many; SPL has 13 exception classes.
Tip: Count the two families: 7 logic exceptions and 6 runtime exceptions.
Question 4Medium
Which version of PHP introduced the advanced concepts of OOP?
- PHP 4
- PHP 5
- PHP 5.3
- PHP 6
Answer: B. PHP 5
Explanation
Object oriented programming (OOP) was first introduced in php4. Area for oop in php version 4 was not very vast. There were only few features available in php4. Major concept of the object oriented programming in PHP is introduced from version 5(we commonly known as php5).
Step-by-step solution
- OOP was present in PHP 4, but with only a few features.
- The major OOP features came with PHP 5, as the explanation states.
- The answer is option B, PHP 5.
Why the other options are wrong
- A. PHP 4 had only a few OOP features, not the advanced concepts.
- C. PHP 5.3 is a later release within PHP 5, not the version that introduced the advanced OOP concepts.
- D. PHP 6 is not the version; the advanced OOP concepts came with PHP 5.
Tip: For PHP history questions, remember PHP 5 is the big OOP release.
Question 5Medium
Which one of the following is the right way to clone an object?
- _clone(targetObject);
- destinationObject = clone targetObject;
- destinationObject = _clone(targetObject);
- destinationObject = clone(targetObject);
Answer: B. destinationObject = clone targetObject;
Explanation
An object copy is created by using the clone keyword.
$copy_of_object = clone $object;
When an object is cloned, PHP 5 will perform a shallow copy of all of the object's properties. Any properties that are references to other variables, will remain references.
Step-by-step solution
- To copy an object in PHP, use the clone keyword.
- The clone keyword goes before the object, as in clone targetObject.
- destinationObject = clone targetObject; is the valid form, option B, the correct answer.
Why the other options are wrong
- A. _clone is not a way to copy an object; the clone keyword is used.
- C. _clone is not a function; the clone keyword is used.
- D. Clone is a keyword, not a function, so the brackets form is not the copy syntax.
Tip: Copy an object with the clone keyword; the copy is a shallow copy of its properties.
Question 6Medium
Which of the following statements invoke the exception class?
throws new Exception();throw new Exception();new Exception();new throws Exception();
Answer: B. throw new Exception();
Explanation
throw new Exception() statement invokes exception class.
Step-by-step solution
- The question asks which statement throws an exception.
- The throw keyword followed by new Exception creates the exception and throws it.
- The correct answer is option B, throw new Exception();
Why the other options are wrong
- A. Throws is not the keyword; the keyword is throw.
- C. New Exception alone creates an object but does not throw it.
- D. New throws Exception is not valid syntax.
Tip: Throwing needs the throw keyword; creating an exception object alone does not throw it.
Question 7Medium
Which version of PHP was added with Exception handling?
- PHP 4
- PHP 5
- PHP 5.3
- PHP 6
Answer: B. PHP 5
Explanation
Exception handling was added to PHP with the version 5 release, and further enhanced with version 5.3.
Step-by-step solution
- PHP 5 introduced exception handling for error management.
- Exception handling was later improved in PHP 5.3.
- So the correct answer is PHP 5.
- Final Answer: B (PHP 5).
Why the other options are wrong
- A. PHP 4 did not add exception handling; that came in PHP 5.
- C. PHP 5.3 improved exception handling, but PHP 5 is where it was first added.
- D. PHP 6 is not the version that added exception handling; that came in PHP 5.
Tip: Remember PHP 5 added exception handling.
Question 8Medium
Which one of the following keyword is used to implement an interface?
- interface
- get
- inherit
- implements
Answer: D. implements
Explanation
A class can implement an interface using the implements keyword in its declaration.
Step-by-step solution
- Implementing an interface requires using the implements keyword.
- The implements keyword links a class to an interface.
- This allows the class to adopt the interface's methods.
- Thus, the correct keyword is implements.
- Final answer: implements.
Why the other options are wrong
- A. interface declares an interface; it does not link a class to one.
- B. get is not a keyword for implementing an interface; it is used for reading properties.
- C. inherit is not a PHP keyword; a class extends another class with the extends keyword.
Tip: Remember: implements connects classes to interfaces, while extends links classes to other classes.
Question 9Medium
At least how many abstract methods must an abstract class contain?
- None
- One
- Two
- Five
Answer: A. None
Explanation
An abstract class may have no abstract methods, so the least is None.
Step-by-step solution
- An abstract class may have no abstract methods at all.
- So the least number of abstract methods it can contain is zero.
- The answer is None, option A.
Why the other options are wrong
- B. One is more than the least; zero is allowed.
- C. Two is more than the least; zero is allowed.
- D. Five is more than the least; zero is allowed.
Tip: A class is abstract when it is declared abstract, even with no abstract methods.
Question 10Medium
Which feature allows us to call more than one method or function of the class in single instruction?
- Typecasting
- Method Including
- Method adding
- Method chaining
Answer: D. Method chaining
Explanation
With the help of php method chaining or PHP function chaining we can call more than one method or function of the class in single instruction. Method chaining works because our function or method of the class always returns object which further call another function.
Following is a basic example of method chaining in php:
$a = new Order();
$a->CreateOrder()->sendOrderEmail()->createShipment();
Step-by-step solution
- Method chaining lets us call multiple methods in one line.
- Each method returns an object to call the next method.
- This is why we can write $a->CreateOrder()->sendOrderEmail()...
- The process continues until all methods are called.
- So, the correct answer is Method chaining.
Why the other options are wrong
- A. Typecasting converts a value from one type to another; it does not call several methods in one line.
- B. Method including is not a feature for chaining calls on one object.
- C. Method adding is not the feature that lets one line call several methods.
Tip: Remember: method chaining needs each method to return 'this' or the object itself.
Practise all 28 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →