PHP Questions & Answers – Object Advanced Features – 3

This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Object Advanced Features – 3”.

1. Which one of the following method is invoked when a value is assigned to an undefined property?
a) __get()
b) __set()
c) __isset()
d) __call()
View Answer

Answer: b
Explanation: The __set() method is invoked when client code attempts to assign to an undefined property. It is passed two arguments: the name of the property, and the value the client is attempting to set.

2. Which one of the following method is invoked when an undefined method is called by client code?
a) __get()
b) __isset()
c) __unset()
d) __call()
View Answer

Answer: d
Explanation: The __call() method is probably the most useful of all the interceptor methods. The __call() method can be useful for delegation. Delegation is the mechanism by which one object passes method invocations on to a second.

3. Which method introduced in PHP 5, is invoked just before an object is a garbage collected?
a) __collect()
b) __garbage()
c) __destruct()
d) __destructor()
View Answer

Answer: c
Explanation: You can use this method to perform any final cleaning up that might be necessary. Imagine, for example, a class that saves itself to a database when so ordered. I could use the __destruct() method to ensure that an instance saves its data when it is deleted.
advertisement
advertisement

4. Which one of the following PHP statements is true?

  1. class CopyMe {}
  2. $first = new CopyMe();
  3. $second = $first;

a) In PHP 4: $second and $first are 2 distinct objects
b) In PHP 5: $second and $first are 2 distinct objects
c) In PHP 4: $second and $first refer to one object
d) None of the mentioned
View Answer

Answer: a
Explanation: None.

5. Which keyword must be added before $first variable on the third line of the above question to make $second and $first as distinct objects in PHP 5?
a) copy
b) clone
c) cut
d) Can’t add any word to make them distinct
View Answer

Answer: b
Explanation: Clone operates on an object instance, producing a by-value copy.
advertisement

6. What will be the output of the following PHP code? (Before the version PHP 5.2)

  1. class StringThing {}
  2. $st = new StringThing();
  3. print $st;

a) Object Not Found
b) Object id #1
c) PHP Catchable fatal error
d) Cannot initialize object
View Answer

Answer: b
Explanation: Since PHP 5.2, this code will produce an error like this: PHP Catchable fatal error: Object of class StringThing could not be converted to string.
advertisement

7. What will be the output of the following PHP code?

  1. class Person 
  2. {
  3.     function getName() { return "Bob"; }
  4.     function getAge() { return 44; }
  5.     function __toString() {
  6.         $desc = $this->getName();
  7.         $desc .= " (age ".$this->getAge().")";
  8.         return $desc;
  9.     }
  10. }
  11. $person = new Person();
  12. print $person;

a) Object Not Found
b) PHP Catchable fatal error
c) BOB (age 44)
d) BOB
View Answer

Answer: c
Explanation: By implementing a __toString() method, you can control how your objects represent themselves when printed. The method is invoked automatically when your object is passed to print or echo, and its return value is substituted.

8. __clone() is run on the ___ object.
a) original
b) pseudo
c) external
d) copied
View Answer

Answer: d
Explanation: __clone() is run on the copied object and not the original.

9. Which method is invoked when an undefined property is accessed?
a) __get()
b) __isset()
c) __unset()
d) __undefined()
View Answer

Answer: a
Explanation: None.

10. What will be the output of the following PHP code?

  1. class Checkout
  2.  {
  3.     final function totalize() 
  4.     {
  5.         // calculate bill
  6.     }
  7.  }
  8.  
  9. class IllegalCheckout extends Checkout 
  10. {
  11.     final function totalize() 
  12.     {
  13.         // change bill calculation
  14.     }
  15. }

a) PHP Fatal error: Class IllegalCheckout may not inherit from final class
b) Value of the bill calculated
c) PHP Fatal error: Cannot find object
d) PHP Fatal error: Cannot override final method
View Answer

Answer: d
Explanation: A final class cannot be subclassed. Less drastically, a final method cannot be overridden.

Sanfoundry Global Education & Learning Series – PHP Programming.

To practice all questions on PHP Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.