Home »
Aptitude Questions and Answers »
PHP Aptitude Questions and Answers
PHP Static Keyword Aptitude Questions and Answers
PHP Static Keyword Aptitude: This section contains aptitude questions and answers on PHP Static Keyword.
By Nidhi Last updated : December 15, 2023
This section contains Aptitude Questions and Answers on PHP Static Keyword.
1) There are the following statements that are given below, which of them are correct about static in PHP?
- "static" is a predefined keyword in PHP.
- "static" is used to create methods inside the class that can be accessed without creating any object.
- We can also create "static" properties of class; those values can be accessed by all objects of the class.
- All of the above
Options:
- A and B
- A and C
- B and C
- D
Correct answer: 4
D
Explanation:
All given statements are correct about "static" in PHP
2) Which of the following operator is used to access the static members of the class?
- ::
- .
- ->
- =>
Correct answer: 1
::
Explanation:
The scope resolution operator "::" is used to access the static members of the class.
3) What is the correct output of the given code snippets?
<?php
class Sample
{
private static function fun()
{
echo "static function called";
}
}
Sample::fun();
?>
- static function called
- Error
- Will not print anything
- None of the above
Correct answer: 2
Error
Explanation:
The above code will generate an error because we cannot access private members outside the class.
4) What is the correct output of the given code snippets?
<?php
class Sample
{
private friendstatic function fun()
{
echo "static function called";
}
}
Sample::fun();
?>
- static function called
- Error
- Will not print anything
- None of the above
Correct answer: 2
Error
Explanation:
The above code will generate an error because there is no concept of a friend in PHP.
5) What is the correct output of the given code snippets?
<?php
class Sample
{
static function fun()
{
echo "static function called";
}
}
Sample::fun();
?>
- static function called
- Error
- Will not print anything
- None of the above
Correct answer: 1
static function called
Explanation:
The above code will print "static function called" on the webpage, because by default functions are public in the class and the fun() is a static function, then it is called by class-name with scope resolution operator.
6) What is the correct output of the given code snippets?
<?php
class Sample
{
protected static function fun()
{
echo "static function called";
}
}
Sample::fun();
?>
- static function called
- Error
- Will not print anything
- None of the above
Correct answer: 2
Error
Explanation:
The above code will generate an error because the protected member can be accessed by the derived class, cannot be accessed outside the class like this.
7) What is the correct output of the given code snippets?
<?php
class Sample
{
static public function fun()
{
echo "static function called";
}
}
Sample::fun();
?>
- static function called
- Error
- Will not print anything
- None of the above
Correct answer: 1
static function called
Explanation:
The above code will print "static function called" on the webpage because public keyword can also be used after the static keyword. Below given function declarations are the same.
public static function fun(){}
static public function fun(){}
8) What is the correct output of the given code snippets?
<?php
class Sample
{
static function fun1()
{
echo "function1 called ";
}
static function fun2()
{
fun1();
echo "function2 called";
}
}
Sample::fun2();
?>
- function1 called
- function1 called
- function1 called funtion2 called
- Error
Correct answer: 4
Error
Explanation:
The above code will generate an error because, if we want to access a static function inside the class then we need to use the class name and scope resolution operator "::", the proper way of classing static function is given below:
Sample::fun1()
9) What is the correct output of the given code snippets?
<?php
class Sample
{
static function fun1()
{
echo "function1 called ";
}
static function fun2()
{
Sample::fun1();
echo "function2 called";
}
}
Sample::fun();
?>
- function1 called
- function1 called
- function1 called funtion2 called
- Error
Correct answer: 4
Error
Explanation:
The above code will generate an error, undefined function Sample::fun(), because we defined fun1() and fun2() in Sample class but we called fun() that is not exists then it will generate an error.
10) Can we call a static function using "self::" inside the class?
- Yes
- No
Correct answer: 1
Yes
Explanation:
Yes, we can call a static function using "self::" inside the class, the example is given below:
<?php
class Sample
{
static function fun1()
{
echo "function1 called ";
}
static function fun2()
{
self::fun1();
echo "function2 called";
}
}
Sample::fun2();
?>
11) What is the correct output of the given code snippets?
<?php
class Sample
{
public static $num = 10;
static function fun()
{
echo Sample::$num;
}
}
Sample::fun();
?>
- 10
- Garbage value
- Error
- None of the above
Correct answer: 1
10
Explanation:
The above code will print 10 on the webpage, here we created a static variable and print it inside the member function fun().
12) What is the correct output of the given code snippets in PHP?
<?php
class Sample
{
public static $num;
Sample()
{
Sample::$num++;
}
static function fun()
{
echo Sample::$num;
}
}
Sample::fun();
?>
- 0
- Error
- Nothing will print
- None of the above
Correct answer: 2
Error
Explanation:
The above code will generate an error because we did not declare constructor properly, we need to use the "function" keyword before the constructor declaration.
13) What is the correct output of the given code snippets?
<?php
class Sample
{
public static $num;
function Sample()
{
Sample::$num++;
}
static function fun()
{
echo Sample::$num;
}
}
Sample::fun();
?>
- 0
- Error
- Nothing will print
- None of the above
Correct answer: 3
Nothing will print
Explanation:
The above code will not print anything, because $num is not initialized.
14) What is the correct output of the given code snippets?
<?php
class Sample
{
public static $num;
function Sample()
{
self::num++;
}
static function fun()
{
echo Sample::$num;
}
}
$obj1 = new Sample();
$obj2 = new Sample();
Sample::fun();
?>
- 0
- 2
- Error
- None of the above
Correct answer: 3
Error
Explanation:
The above code will generate an error, because we used self::num++; inside the constructor, here we need to use self::$num++, here we missed '$' symbol.
15) What is the correct output of the given code snippets?
<?php
class Sample
{
public static $num;
function Sample()
{
Sample::$num++;
}
static function fun()
{
echo Sample::$num;
}
}
$obj1 = new Sample();
$obj2 = new Sample();
Sample::fun();
?>
- 0
- 2
- Error
- None of the above
Correct answer: 2
2
Explanation:
The above code will print "2" on the web page because when we create an object then the constructor gets called and increase the value of $num.