MCQ

PHP Questions and Answers – Object-Oriented OOP – Part 2

This collection of PHP Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Object-Oriented OOP”.
 

1. Which of the following can be used to instantiate an object in PHP supposing class name to be MyClass?

A $obj = new $MyClass;

B $obj = new MyClass;

C $obj = new MyClass();

D obj = new MyClass();

C
$obj = new MyClass(); can be used to instantiate an object in PHP assuming class name to be MyClass

 

 

2. In the following source code, what is/are the properties?
<?php 
    class Person
    {
        public $name;
        
        function showName()
        {
            echo "My name is $name";
        }
    } 
?>

A echo "My name is $name";

B public $name;

C class Person

D function showName()

B
Properties are the variables in the class.

 

 

3. A member function usually access to members of the _______ object only

A previous

B next

C current

D All of the above

C
A member function usually access to members of current object only.

 

 

4. What is the result of the following PHP code?
<?php
    class Person 
    {
        public $name = "Person"; 

        public function __construct($name) 
        { 
            $this->name = $name; 
        } 
    } 
    
    $p = new Person("Alex");  
    echo $p->name; 
?>

A Person

B Alex

C Error

D No Output

B
The above example will display “Alex” instead of “Person”, because the name property was modified in the constructor.

 

 

5. Which is/are the correct way to declare a method?

A function functionName() { function body }

B method methodName() { method body }

C scope function functionName() { function body }

D scope method methodName() { method body }

A, C
In case of public methods, you can ignore declaring the scope and just declare the method like a function.

 

 

6. Which of the following scopes is not available in PHP?

A static

B abstract

C friendly

D private

C
Here is the scopes that are supported by PHP : static, final, public, private, protected and abstract. But it does not support friendly.

 

 

7. Which PHP version provided the “instanceof” keyword?

A PHP 6

B PHP 5.3

C PHP 5

D PHP 4

C
instanceof keyword allow us to determine whether an object is an instance of a class. The following example will output “True”:

<?php
    class Person 
    {     
        public $name; 

        public function __construct($name) 
        { 
            $this->name = $name; 
        } 
    } 
    
    $p = new Person("Alex");  
    
    if ($p instanceof Person) 
        echo "True";
?>

 

 

8. Which of the following built-in functions is used to determine whether a class exists?

A exist_class()

B class_exist()

C exist()

D __exist()

B
The built-in function class_exist() returns true or false according to whether the class exists or not. The following example will output “True”:

<?php
    class Person 
    {     

    } 
    
    if (class_exists('Person')) 
        echo "True";
?>

 

 

9. Which is the correct way to call a method?

A $object->methodName();

B object->methodName();

C object::methodName();

D $object::methodName();

A
The operator “->” is used to call a method in PHP.

 

 

10. PHP know constructors by the name.

A classname()

B _construct()

C function _construct()

D function __construct()

D
A double underscore __ followed by the “construct” keyword.

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *