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?
  1. "static" is a predefined keyword in PHP.
  2. "static" is used to create methods inside the class that can be accessed without creating any object.
  3. We can also create "static" properties of class; those values can be accessed by all objects of the class.
  4. All of the above

Options:

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

2) Which of the following operator is used to access the static members of the class?
  1. ::
  2. .
  3. ->
  4. =>

3) What is the correct output of the given code snippets?
<?php
class Sample
{
    private static function fun()
    {
        echo "static function called";
    }
}
Sample::fun();
?>
  1. static function called
  2. Error
  3. Will not print anything
  4. None of the above

4) What is the correct output of the given code snippets?
<?php
class Sample
{
    private friendstatic function fun()
    {
        echo "static function called";
    }
}

Sample::fun();
?>
  1. static function called
  2. Error
  3. Will not print anything
  4. None of the above

5) What is the correct output of the given code snippets?
<?php
class Sample
{
    static function fun()
    {
        echo "static function called";
    }
}

Sample::fun();
?>
  1. static function called
  2. Error
  3. Will not print anything
  4. None of the above

6) What is the correct output of the given code snippets?
<?php
class Sample
{
    protected static function fun()
    {
        echo "static function called";
    }
}

Sample::fun();
?>
  1. static function called
  2. Error
  3. Will not print anything
  4. None of the above

7) What is the correct output of the given code snippets?
<?php
class Sample
{
    static public function fun()
    {
        echo "static function called";
    }
}

Sample::fun();
?>
  1. static function called
  2. Error
  3. Will not print anything
  4. None of the above

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();
?>
  1. function1 called
  2. function1 called
  3. function1 called funtion2 called
  4. Error

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();
?>
  1. function1 called
  2. function1 called
  3. function1 called funtion2 called
  4. Error

10) Can we call a static function using "self::" inside the class?
  1. Yes
  2. No

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();
?>
  1. 10
  2. Garbage value
  3. Error
  4. None of the above

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();
?>
  1. 0
  2. Error
  3. Nothing will print
  4. None of the above

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();
?>
  1. 0
  2. Error
  3. Nothing will print
  4. None of the above

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();
?>
  1. 0
  2. 2
  3. Error
  4. None of the above

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();
?>
  1. 0
  2. 2
  3. Error
  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.