PHP Multiple Choice Questions on “Object Basics-1”.
1. Which one of the following is not a valid class name?
a) ShopProduct
b) Shopproduct
c) Shopproduct1
d) 1shopproduct
Answer: d
Clarification: 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
Answer: c
Clarification: 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
Answer: b
Clarification: In PHP object is created using new operator. $product1 and $product2 are different objects of the same type generated from a single class.
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
Answer: b
Clarification: 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) .
Answer: c
Clarification: 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
Answer: a
Clarification: 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
Answer: b
Clarification: A method declaration resembles a function declaration. The function keyword precedes a method name, followed by an optional list of argument variables in parentheses.
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
Answer: a
Clarification: 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()
Answer: d
Clarification: None.
10. What will be the output of the following PHP code?
-
-
class ShopProductWriter -
{ -
public function write( $shopProduct )
-
{ -
$str = "{$shopProduct->title}: " .$shopProduct->getProducer() ." ({$shopProduct->price})n";
-
print $str;
-
} -
} -
$product1 = new ShopProduct( "My Antonia", "Willa", "Cather", 5.99 );
-
$writer = new ShopProductWriter();
-
$writer->write( $product1 );
-
?>
a) Error
b) Cather: Willa My Antonia (5.99)
c) Willa: Cather My Antonia (5.99)
d) My Antonia: Willa Cather (5.99)
Answer: d
Clarification: None.
