PHP Multiple Choice Questions – Object

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

1. Which one of the following is not a valid class name?
a) ShopProduct
b) Shopproduct
c) Shopproduct1
d) 1shopproduct
View Answer

Answer: d
Explanation: You declare a class with the class keyword and an arbitrary class name. Class names can be any combination of numbers and letters, although they must not begin with a number.

2. Fill in the blank with the best option. An Object is a/an ________ of a class.
a) type
b) prototype
c) instance
d) object
View Answer

Answer: c
Explanation: An object is said to be an instance of its class. It is of the type defined by the class.

3. There are two objects-
$product1 = new Shop();
$product2 = new Shop();
Which one of the following statements is right about them?
a) $product1 and $product2 are same objects of the same type generated from a single class
b) $product1 and $product2 are different objects of the same type generated from a single class
c) $product1 and $product2 are same objects of the different type generated from a single class
d) $product1 and $product2 are different objects of the different type generated from a single class
View Answer

Answer: b
Explanation: In PHP object is created using new operator. $product1 and $product2 are different objects of the same type generated from a single class.
advertisement
advertisement

4. Which version of PHP introduced the visibility keywords i.e public, private, and protected?
a) PHP 4
b) PHP 5
c) PHP 5.1
d) PHP 5.3
View Answer

Answer: b
Explanation: In PHP 4, all properties were declared with var keyword, which is identical in effect to using public. For the sake of backward compatibility, PHP 5 accepts var in place of public for properties.

5. Which characters is used to access property variables on an object-by-object basis?
a) ::
b) =
c) ->
d) .
View Answer

Answer: c
Explanation: Example: $product1->title=”My Life”;

6. Code that uses a class, function, or method is often described as the ____________
a) client code
b) user code
c) object code
d) class code
View Answer

Answer: a
Explanation: Code that uses a class, function, or method is often described as the class’s, function, or method client or as client code.

7. Which keyword precedes a method name?
a) method
b) function
c) public
d) protected
View Answer

Answer: b
Explanation: A method declaration resembles a function declaration. The function keyword precedes a method name, followed by an optional list of argument variables in parentheses.
advertisement

8. If you omit the visibility keyword in your method declaration, by default the method will be declared as ____________
a) public
b) private
c) protected
d) friendly
View Answer

Answer: a
Explanation: By declaring a method public, you ensure that it can be invoked from outside of the current object.

9. Which function is used to determine whether the variable’s value is either TRUE or FALSE?
a) boolean()
b) is_boolean()
c) bool()
d) is_bool()
View Answer

Answer: d
Explanation: None.
advertisement

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

  1. <?php
  2.     class ShopProductWriter
  3.     {
  4.         public function write( $shopProduct )
  5.         {
  6.             $str = "{$shopProduct->title}: " .$shopProduct->getProducer() ." ({$shopProduct->price})\n";
  7.             print $str;
  8.         }
  9.     }
  10.     $product1 = new ShopProduct( "My Antonia", "Willa", "Cather", 5.99 );
  11.     $writer = new ShopProductWriter();
  12.     $writer->write( $product1 );
  13. ?>

a) Error
b) Cather: Willa My Antonia (5.99)
c) Willa: Cather My Antonia (5.99)
d) My Antonia: Willa Cather (5.99)
View Answer

Answer: d
Explanation: None.

More MCQs on PHP Object:

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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

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.