Home »
Aptitude Questions and Answers »
PHP Aptitude Questions and Answers
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?
- The constructor is a special type of function, which is called automatically when an object gets created.
- The constructor is used to initialize data members of the class.
- The constructor has the same name as the class name.
- The constructor must be public type.
Options:
- A and B
- A and C
- A, B, and C
- A, B, C, and D
Correct answer: 4
A, B, C, and D
Explanation:
All given statements are correct about constructors in PHP.
2) Which of the following are correct types of constructors in PHP?
- Default constructor
- Parameterized constructor
- Conversion constructor
- All of the above
Options:
- A and B
- A and C
- B and C
- D
Correct answer: 1
A and B
Explanation:
In PHP, we can implement default as well as parameterized constructor.
3) Can we overload a constructor in PHP?
- Yes
- No
Correct answer: 2
No
Explanation:
PHP does not support constructor overloading.
4) Which of the following magic function is used to create constructor in PHP?
- __new()
- __construct()
- __constructor()
- __const()
Correct answer: 2
__construct()
Explanation:
The __construct() magic function is used to create constructor in PHP.
5) What is the correct output of the given code snippets?
<?php
class Sample
{
Sample()
{
echo "constructor called";
}
}
$obj = new Sample();
?>
Options:
- Constructor called
- Error
- Nothing will print on the webpage
- None of the above
Correct answer: 2
Error
Explanation:
The above code will generate an error because here we need to use the "function" keyword to define the constructor.
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:
- Constructor called
- Error
- Nothing will print on the webpage
- None of the above
Correct answer: 2
Error
Explanation:
The above code will generate an error because the constructor does not support return type.
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:
- Constructor called
- Error
- Nothing will print on the webpage
- None of the above
Correct answer: 1
Constructor called
Explanation:
The above code will print "constructor called", here we use “return $this” statement inside the constructor but it will not create any problem; this statement is useless and will not generate any error.
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:
- 10, 20
- Error
- Nothing will print on webpage
- None of the above
Correct answer: 2
Error
Explanation:
The above code will generate an error, because we cannot declare data members of class using strict type.
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:
- 10, 20
- Error
- Nothing will print on webpage
- None of the above
Correct answer: 2
Error
Explanation:
The above will generate an error, because we cannot access variable $num1 and $num2 like this, we need to use $this->num1 instead of $this->$num1.
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:
- 10, 20
- Error
- Nothing will print on webpage
- None of the above
Correct answer: 1
10, 20
Explanation:
The above code will print "10, 20" on the web page, here we implemented constructor using __construct() magic function.
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:
- 50, 60
- Error
- Nothing will print on webpage
- None of the above
Correct answer: 1
50, 60
Explanation:
The above code will print "50, 60" on the web page, here we implemented parameterized constructor.
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:
- 50, 60
- 10, 20
- Error
- None of the above
Correct answer: 3
Error
Explanation:
The above code will generate an error because we cannot re-declare a constructor in PHP.
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:
- 50,60
- 10,20
- Error
- None of the above
Correct answer: 1
10, 20
Explanation:
The above code will print "10,20" on the web page, because if __construct() function is present in the class then it will call instead of any another constructor.
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:
- 50,60
- 10,20
- Error
- None of the above
Correct answer: 2
10,20
Explanation:
The above code will print "10,20" on the web page, because if __construct() function is present in the class then it will call always instead of any another constructor.
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:
- 50,60
- 10,20
- Error
- None of the above
Correct answer: 2
10,20
Explanation:
The above code will print "10,20" on the web page, because in the above code, we assign $obj2 to $obj1, then both $obj1 and $obj2 are pointing the same object, modification in anyone will be reflected in other.
16) Which of the following magic function is used to create a copy of an object in PHP?
- __copy()
- __replicate()
- __clone()
- None of the above
Correct answer: 3
__clone()
Explanation:
The magic __clone() function is needed to implement in the class.
17) Which of the following statements are correct about destructor in PHP?
- We cannot implement destructor in PHP.
- Destructor is a special type of member function; it is called automatically when an object gets destroyed.
- Destructor is used to destroy the object.
- None of the above
Options:
- A
- B
- D
- A and B
Correct answer: 2
B
Explanation:
Destructor is a special type of member function; it is called automatically when an object gets destroyed.
18) Which of the following magic function is used to create destructor in PHP?
- __destructor()
- __destruct()
- __destroy()
- None of the above
Correct answer: 1
__destructor()
Explanation:
The __destruct() magic function is used to create destructor in PHP.
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:
- Destructor called
- Error
- Garbage
- None of the above
Correct answer: 2
Error
Explanation:
The above code will create an error. Because we cannot implement destructor using '~' operator in PHP. Here, we need to use magic function __destruct() to implement destructor.
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:
- Destructor called
- Error
- Garbage
- None of the above
Correct answer: 1
Destructor called
Explanation:
The above code will print "Destructor called" because when $obj1 gets destroyed then the __destruct() function will be called and print "Destructor called" on the web page.