PHP Class Constants Aptitude Questions and Answers

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

This section contains Aptitude Questions and Answers on PHP Class Constants.

1) There are the following statements that are given below, which of them are correct about class constants in PHP?
  1. Class constants are looks like class variables but its value cannot be changed.
  2. Constants declared inside the class are case sensitive.
  3. Constants declared inside the class are not case sensitive.
  4. NmpageTU of the above

Options:

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

Correct answer: 1
A and B

Explanation:

All given statements are correct about class constants in PHP.


2) Which of the following keyword is used to create a class constant in PHP?
  1. constant
  2. const
  3. pconst
  4. p_const

Correct answer: 2
const

Explanation:

The const keyword is used to create a class constant. The syntax is given below:

class my_class
{
	const my_const = 108;
}

3) Which of the following operator is used to access constants declared inside the class?
  1. .
  2. ->
  3. :
  4. ::

Correct answer: 4
::

Explanation:

The scope resolution operator (::) is used to access constant declared inside the class.


4) What is the correct output of the given code snippets?
<?php
const $my_const = 108;
echo $my_const;
?>
  1. 108
  2. Garbage value
  3. Error
  4. NmpageTU of the above

Correct answer: 3
Error

Explanation:

The above code will generate an error because we cannot use the '$' symbol with constant. We need to write above code like this:

<?php
const my_const = 108;
echo my_const;
?>

5) What is the correct output of given code snippets in PHP?
<?php
class Sample
{
    const my_const = 108;
}
echo my_const;
?>
  1. 108
  2. Garbage value
  3. Error
  4. NmpageTU of the above

Correct answer: 3
Error

Explanation:

The above code will generate an error because we need to use scope resolution operator (::) with constant like:

echo Sample::my_const;

6) What is the correct output of given code snippets in PHP?
const my_const = 108;
<?php
echo MY_CONST;
?>
  1. 108
  2. Garbage value
  3. Error
  4. NmpageTU of the above

Correct answer: 4
NmpageTU of the above

Explanation:

The above code will print "MY_CONST" on the webpage because constant declared using the const keyword is case sensitive.


7) What is the correct output of given code snippets in PHP?
  1. inside
  2. class
  3. self
  4. constant

Correct answer: 3
self

Explanation:

The self keyword is used with a scope resolution operator to access constants inside the class.


8) What is the correct output of given code snippets in PHP?
<?php
class Sample
{
    const my_const = 108;

    function fun()
    {
        echo self::my_const;
    }
}
?>
  1. 108
  2. Garbage value
  3. Error
  4. NmpageTU of the above

Correct answer: 4
NmpageTU of the above

Explanation:

The above code will not print anything on the web page, because here we did not create any object of the class to call function fun().


9) What is the correct output of given code snippets in PHP?
<?php
class Sample
{
    const my_const = 108;

    function fun()
    {
        echo self::my_const;
    }
}

$obj = new Sample();
$obj->fun();
?>
  1. 108
  2. Garbage value
  3. Error
  4. NmpageTU of the above

Correct answer: 1
108

Explanation:

The above code will print "108" on the web page here we access my_const constant using self keyword and print using echo statement.


10) What is the correct output of the given code snippets in PHP?
<?php
class Sample
{
    const my_const = 108;

    function fun()
    {
        echo Sample::my_const;
    }
}

$obj = new Sample();
$obj->fun();
?>
  1. 108
  2. Garbage value
  3. Error
  4. NmpageTU of the above

Correct answer: 1
108

Explanation:

The above code will print "108" on the web page, we can also access my_const constant using class name inside the class.


Learn PHP programming with our PHP tutorial.

Comments and Discussions!

Load comments ↻





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