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
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
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
Explanation: 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
View Answer
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
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
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
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.
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
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
Explanation: None.
10. What will be the output of the following PHP code?
<?php
class ShopProduct
{
// Define properties
public $title;
public $firstName;
public $lastName;
public $price;
// Constructor to initialize the properties
public function __construct($title, $firstName, $lastName, $price)
{
$this->title = $title;
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->price = $price;
}
// Method to return the full name of the producer
public function getProducer()
{
return "{$this->firstName} {$this->lastName}";
}
}
// Existing classes and objects
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)
View Answer
Explanation: None.
More MCQs on PHP Object:
Sanfoundry Global Education & Learning Series – PHP Programming.
To practice all areas of PHP Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.
- Check PHP Books
- Check MCA Books
- Apply for Programming Internship
- Practice MCA MCQs
- Check Information Technology Books