Home »
Aptitude Questions and Answers »
PHP Aptitude Questions and Answers
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?
- Class constants are looks like class variables but its value cannot be changed.
- Constants declared inside the class are case sensitive.
- Constants declared inside the class are not case sensitive.
- NmpageTU of the above
Options:
- A and B
- A and C
- C
- 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?
- constant
- const
- pconst
- 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?
- .
- ->
- :
- ::
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;
?>
- 108
- Garbage value
- Error
- 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;
?>
- 108
- Garbage value
- Error
- 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;
?>
- 108
- Garbage value
- Error
- 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?
- inside
- class
- self
- 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;
}
}
?>
- 108
- Garbage value
- Error
- 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();
?>
- 108
- Garbage value
- Error
- 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();
?>
- 108
- Garbage value
- Error
- 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.