PHP Constructors & Destructors Aptitude Questions and Answers

PHP Constructors & Destructors Aptitude: This section contains aptitude questions and answers on PHP Constructors & Destructors. By Nidhi Last updated : December 15, 2023

This section contains Aptitude Questions and Answers on PHP Constructors & Destructors.

1) There are the following statements that are given below, which of them are correct about constructors in PHP?
  1. The constructor is a special type of function, which is called automatically when an object gets created.
  2. The constructor is used to initialize data members of the class.
  3. The constructor has the same name as the class name.
  4. The constructor must be public type.

Options:

  1. A and B
  2. A and C
  3. A, B, and C
  4. A, B, C, and D

2) Which of the following are correct types of constructors in PHP?
  1. Default constructor
  2. Parameterized constructor
  3. Conversion constructor
  4. All of the above

Options:

  1. A and B
  2. A and C
  3. B and C
  4. D

3) Can we overload a constructor in PHP?
  1. Yes
  2. No

4) Which of the following magic function is used to create constructor in PHP?
  1. __new()
  2. __construct()
  3. __constructor()
  4. __const()

5) What is the correct output of the given code snippets?
<?php
class Sample
{
    Sample()
    {
        echo "constructor called";
    }
}
$obj = new Sample();
?>

Options:

  1. Constructor called
  2. Error
  3. Nothing will print on the webpage
  4. None of the above

6) What is the correct output of the given code snippets?
<?php
class Sample
{
    function Sample():
        Sample
        {
            echo "constructor called";
            return $this;
        }
    }

    $obj = new Sample();
?>

Options:

  1. Constructor called
  2. Error
  3. Nothing will print on the webpage
  4. None of the above

7) What is the correct output of the given code snippets?
<?php
class Sample
{
    function Sample()
    {
        echo "constructor called";
        return $this;
    }
}
$obj = new Sample();
?>

Options:

  1. Constructor called
  2. Error
  3. Nothing will print on the webpage
  4. None of the above

8) What is the correct output of the given code snippets?
<?php declare(strict_types = 1);
class Sample
{
    private int $num1;
    private int $num2;

    function Sample()
    {
        $this->num1 = 10;
        $this->num2 = 20;
    }

    function disp()
    {
        echo "$this->num1,$this->num2";
    }
}

$obj = new Sample();
$obj->disp();
?>

Options:

  1. 10, 20
  2. Error
  3. Nothing will print on webpage
  4. None of the above

9) What is the correct output of the given code snippets?
<?php
class Sample
{
    private $num1;
    private $num2;

    function Sample()
    {
        $this->$num1 = 10;
        $this->$num2 = 20;
    }

    function disp()
    {
        echo "$this->$num1,$this->$num2";
    }
}

$obj = new Sample();
$obj->disp();
?>

Options:

  1. 10, 20
  2. Error
  3. Nothing will print on webpage
  4. None of the above

10) What is the correct output of the given code snippets?
<?php declare(strict_types = 1);
class Sample
{
    private $num1;
    private $num2;

    function __construct()
    {
        $this->num1 = 10;
        $this->num2 = 20;
    }

    function disp()
    {
        echo "$this->num1,$this->num2";
    }
}

$obj = new Sample();
$obj->disp();
?>

Options:

  1. 10, 20
  2. Error
  3. Nothing will print on webpage
  4. None of the above

11) What is the correct output of the given code snippets?
<?php declare(strict_types = 1);
class Sample
{
    private $num1;
    private $num2;

    function Sample($num1, $num2)
    {
        $this->num1 = $num1;
        $this->num2 = $num2;
    }

    function disp()
    {
        echo "$this->num1,$this->num2";
    }
}

$obj = new Sample(50, 60);
$obj->disp();
?>

Options:

  1. 50, 60
  2. Error
  3. Nothing will print on webpage
  4. None of the above

12) What is the correct output of the given code snippets?
<?php declare(strict_types = 1);
class Sample
{
    private $num1;
    private $num2;

    function Sample()
    {
        $this->num1 = 10;
        $this->num2 = 20;
    }

    function Sample($num1, $num2)
    {
        $this->num1 = $num1;
        $this->num2 = $num2;
    }

    function disp()
    {
        echo "$this->num1,$this->num2";
    }
}

$obj = new Sample(50, 60);
$obj->disp();
?>

Options:

  1. 50, 60
  2. 10, 20
  3. Error
  4. None of the above

13) What is the correct output of the given code snippets?
<?php declare(strict_types = 1);
class Sample
{
    private $num1;
    private $num2;

    function __construct()
    {
        $this->num1 = 10;
        $this->num2 = 20;
    }

    function Sample()
    {
        $this->num1 = 50;
        $this->num2 = 60;
    }

    function disp()
    {
        echo "$this->num1,$this->num2";
    }
}

$obj = new Sample();
$obj->disp();
?>

Options:

  1. 50,60
  2. 10,20
  3. Error
  4. None of the above

14) What is the correct output of the given code snippets?
<?php declare(strict_types = 1);
class Sample
{
    private $num1;
    private $num2;

    function __construct()
    {
        $this->num1 = 10;
        $this->num2 = 20;
    }

    function Sample($num1, $num2)
    {
        $this->num1 = $num1;
        $this->num2 = $num2;
    }

    function disp()
    {
        echo "$this->num1,$this->num2";
    }
}

$obj = new Sample(50, 60);
$obj->disp();
?>

Options:

  1. 50,60
  2. 10,20
  3. Error
  4. None of the above

15) What is the correct output of the given code snippets?
<?php declare(strict_types = 1);
class Sample
{
    private $num1;
    private $num2;

    function Sample()
    {
        $this->num1 = 50;
        $this->num2 = 60;
    }

    function set($n1, $n2)
    {
        $this->num1 = $n1;
        $this->num2 = $n2;
    }
    function disp()
    {
        echo "$this->num1,$this->num2";
    }
}

$obj1 = new Sample();
$obj2 = $obj1;

$obj2->set(10, 20);
$obj1->disp();
?>

Options:

  1. 50,60
  2. 10,20
  3. Error
  4. None of the above

16) Which of the following magic function is used to create a copy of an object in PHP?
  1. __copy()
  2. __replicate()
  3. __clone()
  4. None of the above

17) Which of the following statements are correct about destructor in PHP?
  1. We cannot implement destructor in PHP.
  2. Destructor is a special type of member function; it is called automatically when an object gets destroyed.
  3. Destructor is used to destroy the object.
  4. None of the above

Options:

  1. A
  2. B
  3. D
  4. A and B

18) Which of the following magic function is used to create destructor in PHP?
  1. __destructor()
  2. __destruct()
  3. __destroy()
  4. None of the above

19) What is the correct output of the given code snippets?
<?php declare(strict_types = 1);
class Sample
{
    private $num1;
    private $num2;

    function Sample()
    {
        $this->num1 = 50;
        $this->num2 = 60;
    }

    function disp()
    {
        echo "$this->num1,$this->num2";
    }

    function ~Sample()
    {
        echo "Destructor called";
    }
}

$obj1 = new Sample();
?>

Options:

  1. Destructor called
  2. Error
  3. Garbage
  4. None of the above

20) What is the correct output of the given code snippets?
<?php declare(strict_types = 1);
class Sample
{
    private $num1;
    private $num2;

    function Sample()
    {
        $this->num1 = 50;
        $this->num2 = 60;
    }

    function disp()
    {
        echo "$this->num1,$this->num2";
    }

    function __destruct()
    {
        echo "Destructor called";
    }
}

$obj1 = new Sample();
?>

Options:

  1. Destructor called
  2. Error
  3. Garbage
  4. None of the above

Learn PHP programming with our PHP tutorial.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.