Home »
Aptitude Questions and Answers »
PHP Aptitude Questions and Answers
PHP Constants Aptitude Questions and Answers
PHP Constants Aptitude Questions and Answers: This section contains aptitude questions and answers on PHP Constants.
By Nidhi Last updated : December 15, 2023
This section contains Aptitude Questions and Answers on PHP Constants.
1) There are the following statements that are given below, which of them are correct about constants in PHP?
- Constants are just like variables, but its value cannot be changed.
- A valid constant name starts with an alphabet or underscore.
- There is no need to use the '$' symbol with constants.
- All the above
Options:
- A and B
- B and C
- A and C
- D
Correct answer: 4
D
All given statements are correct about the constants in PHP.
2) Which of the following function is used to define a constant in PHP?
- defconst()
- define()
- const()
- def()
Correct answer: 2
define()
The define() function is used to define a constant in PHP.
3) Can we create a case-sensitive constant in PHP?
- Yes
- No
Correct answer: 1
Yes
Yes, we can create a case-sensitive constant in PHP.
4) What is the correct output of the given code snippets?
<?php
define("COUNTRY", "INDIA");
echo COUNTRY;
?>
- INDIA
- COUNTRY
- Garbage value
- Error
Correct answer: 1
INDIA
The above code will print "INDIA" on the webpage.
5) What is the correct output of the given code snippets?
<?php
define("COUNTRY", "INDIA");
echo country;
?>
- INDIA
- country
- Garbage value
- Error
Correct answer: 2
country
The above code will print the "country" on the webpage.
6) What is the correct output of the given code snippets?
<?php
define("COUNTRY", "INDIA", true);
echo country;
?>
- INDIA
- country
- Garbage value
- Error
Correct answer: 1
INDIA
The above code will print INDIA on the webpage.
7) What is the correct output of the given code snippets?
<?php
define("COUNTRY", "INDIA", true);
define("COUNTRY", "BHARAT", true);
echo country;
?>
- INDIA
- country
- BHARAT
- Error
Correct answer: 1
INDIA
The above code will print "INDIA" on the webpage.
8) What is the correct output of the given code snippets?
<?php
define("COUNTRY", "INDIA", true);
redefine("COUNTRY", "BHARAT", true);
echo country;
?>
- INDIA
- country
- BHARAT
- Error
Correct answer: 4
Error
The above code will generate an error because redefine() is not an inbuilt function in PHP.
9) What is the correct output of the given code snippets?
<?php
define("COUNTRIES", {"INDIA","AUSTRALIA","USA"});
echo COUNTRIES[1];
?>
- INDIA
- AUSTRALIA
- Error
- Garbage value
Correct answer: 3
Error
The above code will generate an error because it is not the correct way to define a constant array.
10) What is the correct output of the given code snippets?
<?php
define("COUNTRIES", ["INDIA", "AUSTRALIA", "USA"]);
echo COUNTRY[1];
?>
- INDIA
- AUSTRALIA
- 0
- Error
Correct answer: 3
0
The above code will print "0" on the webpage because the COUNTRY constant is not defined.
11) What is the correct output of the given code snippets?
<?php
define("COUNTRIES", ["INDIA", "AUSTRALIA", "USA"]);
echo COUNTRIES[1];
?>
- INDIA
- AUSTRALIA
- 0
- Error
Correct answer: 2
AUSTRALIA
The above code will print "AUSTRALIA" on the webpage.
12) What is the correct output of the given code snippets?
<?php
define("COUNTRY", "INDIA");
function fun()
{
echo COUNTRY;
}
?>
- INDIA
- 0
- Error
- None of the above
Correct answer: 4
None of the above
The above code will not print anything on the webpage because fun() is not called anywhere in the code.
13) What is the correct output of the given code snippets?
<?php
define("COUNTRY", "INDIA");
function fun()
{
echo COUNTRY;
}
fun();
?>
- INDIA
- 0
- Error
- None of the above
Correct answer: 1
INDIA
The above code will print "INDIA" on the webpage.
14) What is the correct output of the given code snippets?
<?php
define("COUNTRY", "INDIA");
function fun()
{
define("COUNTRY", "USA");
echo COUNTRY;
}
fun();
?>
- INDIA
- USA
- Error
- None of the above
Correct answer: 1
INDIA
The above code will print "INDIA" on the webpage.
15) What is the correct output of the given code snippets?
<?php
define("POLICE", 100);
function fun()
{
echo POLICE;
}
fun();
?>
- 100
- POLICE
- Error
- None of the above