Home »
Aptitude Questions and Answers »
PHP Aptitude Questions and Answers
PHP OOPs Concept Aptitude Questions and Answers
PHP OOPs Concept Aptitude: This section contains aptitude questions and answers on OOPs Concept.
By Nidhi Last updated : December 15, 2023
This section contains Aptitude Questions and Answers on PHP OOPs Concept.
1) There are the following statements that are given below, which of them are correct about OOPS in PHP?
- OOPS stands for Object-Oriented Programming System.
- OOPS provides a clear structure for the program.
- OOPS is a programming technique that provides us better management of projects compared to procedural oriented programming.
- All of the above
Options:
- A and B
- A and C
- B and C
- D
Correct answer: 4
D
Explanation:
OOPS is a programming technique that provides us better management of projects. There are the following languages support OOPS: C++, JAVA, C#, etc.
2) What is the full form of OOBS?
- Object-Oriented Basic System
- Object-Oriented Based System
- Object-Oriented Bound System
- Object-Oriented Begin System
Correct answer: 2
Object-Oriented Based System
Explanation:
The OOBS stands for Object-Oriented Based System.
3) Does PHP support function overloading?
- Yes
- No
Correct answer: 1
Yes
Explanation:
Yes, PHP support function overloading using magic function __call().
4) Which of the following access specifiers are used in PHP?
- private
- public
- protected
- All of the above
Correct answer: 4
All of the above
Explanation:
In the PHP; public, private, and protected access specifiers are used.
5) What is the correct way to create an object of Sample class?
- Sample $obj;
- Sample $obj = new Sample();
- $obj = new Sample()
- None of the above
Correct answer: 3
$obj = new Sample()
Explanation:
To create an object of $obj= new Sample() is correct way.
6) By default, member functions of a class are?
- private
- public
Correct answer: 2
public
Explanation:
By default, member functions of a class are public.
7) Which of the following operator is used to access members of a class using an object?
- ->
- .
Correct answer: 1
->
Explanation:
The '->' operator is used to access members of a class using an object.
8) What is the correct output of given code snippets in PHP?
<?php
class Sample
{
public $name;
function set($n)
{
$name = $n;
}
function display()
{
echo $name;
}
}
$obj = new Sample();
$obj->set(10);
$obj->display();
?>
- 10
- Garbage value
- Error
- None of the above
Correct answer: 3
Error
Explanation:
The above code will generate an error, because we cannot access data members of class without $this, in the above code we need to use $this with $name like this,
$this->$name;
9) What is the correct output of given code snippets in PHP?
<?php
class Sample
{
public $name;
function set($n)
{
$this->$name = $n;
}
function print ()
{
echo $this->$name;
}
}
$obj = new Sample();
$obj->set(10);
$obj->print();
?>
- 10
- Garbage value
- Error
- None of the above
Correct answer: 3
Error
Explanation:
The above code will generate an error because in the above code we defined print() function, but print() is a built-in function so that we cannot define it.
10) What is the correct output of the given code snippets in PHP?
<?php
class Sample
{
private $name;
function set($n)
{
$this->$name = $n;
}
function display()
{
echo $this->$name;
}
}
$obj = new Sample();
$obj->set(10);
$obj->display();
?>
- 10
- Garbage value
- Error
- None of the above
Correct answer: 1
10
Explanation:
The above code will print 10 on the web page.
11) Which of the following statements are correct about $this in PHP?
- The $this object points to the current object of the class.
- The $this object can be used inside the class.
- We can use $this object outside the class.
- All of the above
Options:
- A and B
- A and C
- B and C
- D
Correct answer: 1
A and B
Explanation:
The $this object points to the current object of the class, it can be used within the class only.
12) What is the correct output of the given code snippets in PHP?
<?php
class Sample
{
function fun1()
{
echo "fun1 ";
return $this;
}
function fun2()
{
echo "fun2 ";
}
}
$obj = new Sample();
$obj->fun1()
->fun2();
?>
- fun1
- fun2
- fun1 fun2
- Error
Correct answer: 1
fun1
Explanation:
The above code will print "fun1" on the web page, because in the above code fun1() function will be called but fun()2 will not call, to call cascaded functions we need to use $this.
13) What is the correct output of the given code snippets?
<?php
class Sample
{
function fun1():
Sample
{
echo "fun1 ";
return $this;
}
function fun2()
{
echo "fun2 ";
}
}
$obj = new Sample();
$obj->fun1()
->fun2();
?>
- fun1
- fun2
- fun1 fun2
- Error
Correct answer: 3
fun1 fun2
Explanation:
The above code will print "fun1 fun2" on the web page because in the above code fun1() function will return the object of Sample class, then the fun2() method will be called by the returned object.
14) Which of the following keyword is used to check a specific object belongs to a specific class?
- objectof
- instanceof
- classof
- None of the above
Correct answer: 2
instanceof
Explanation:
The instanceof keyword is used to check an object belongs to the specific class.
15) What is the correct output of the given code snippets in PHP?
<?php
class Sample
{
function fun()
{
echo "fun1 ";
}
}
$obj = new Sample();
var_dump($obj instanceof Sample);
?>
- bool(true)
- bool(false)
- Error
- None of the above
Correct answer: 1
bool(true)
Explanation:
The above code will print bool(true) on the web page. Because instanceof keyword is used to check an object belongs to the specific class and returns a boolean value.