Home »
Aptitude Questions and Answers »
PHP Aptitude Questions and Answers
PHP Magic Constants Aptitude Questions and Answers
PHP Magic Constants Aptitude: This section contains aptitude questions and answers on PHP Magic Constants.
By Nidhi Last updated : December 15, 2023
This section contains Aptitude Questions and Answers on PHP Magic Constants.
1) There are the following statements that are given below, which of them are correct about magic constants in PHP?
- Magic constants are defined by the user started with an underscore.
- Magic constants are predefined constants that can be changed according to their use.
- Magic constants started with a double underscore (__) and end with a double underscore (__).
- All of the above
Options:
- A and B
- A and C
- B and C
- D
Correct answer: 3
B and C
Explanation:
Magic constants are predefined constants that can be changed according to their use. These constants started and ended with a double underscore (__).
2) Which of the following are the predefined magic constants in PHP?
- __LINE__
- __TRAIT__
- __FILE__
- __USER__
Options:
- A and B
- A and C
- A, B, and C
- A, B, C, and D
Correct answer: 3
A, B, and C
Explanation:
The __LINE__, __TRAIT__, and __FILE__ are predefined magic constants. But __USER__ is not predefined magic constant.
3) Magic constants are case sensitive?
- Yes
- No
Correct answer: 1
Yes
Explanation:
Magic constants are case sensitive; __FILE__ and __file__ are not the same.
4) Magic constants are resolved at?
- Compile-time
- Runtime
Correct answer: 1
Compile-time
Explanation:
All magic constants are resolved at compile-time.
5) What is the correct output of the given code snippets?
<?php
$num = 10 + 20;
echo __LINE__;
?>
- 30
- 3
- __LINE__
- Error
Correct answer: 2
3
Explanation:
The __LINE__ magic constant contains the current line number of the code, where it was used. In the above code, the __LINE__ constant is used in line number 3.
6) What is the correct output of the given code snippets?
<?php
$num = 10 + 20;
echo "__LINE__";
?>
- 30
- 3
- __LINE__
- Error
Correct answer: 3
__LINE__
Explanation:
The above code will print "__LINE__" on the webpage because, we enclosed constant __LINE__ within the double quotes that it will not use like a constant, and it will be treated as a string.
7) Which of the following magic constant is used to return the complete path of the file with the extension, where it is used?
- __PATH__
- __FILE__
- __DIR__
- __FILE_LOC__
Correct answer: 2
__FILE__
Explanation:
The __FILE__ magic constant is used to return the complete path of the file with the extension.
8) Which of the following magic constant is used to return the complete directory path, where it is used?
- __PATH__
- __DIRECTORY__
- __DIR__
- __LOC__
Correct answer: 3
__DIR__
Explanation:
The __DIR__ magic constant is used to return the complete directory path.
9) Which of the following are not predefined magic constant?
- __PATH__
- __DIR__
- __LINE__
- __CLASS__
Correct answer: 1
__PATH__
Explanation:
The __PATH__ is not predefined magic constant in PHP.
10) Which of the following magic constant is used to return the name of the function, where it is used?
- __FUN__
- __FUNCTION__
- __METHOD__
- __FUNC__
Correct answer: 2
__FUNCTION__
Explanation:
The __FUNCTION__ magic constant is used to return the name of the function, where it is used.
11) What is the correct output of the given code snippets?
<?php
function fun()
{
echo __FUNCTION__;
}
fun();
?>
- fun
- fun()
- function fun()
- None of the above
Correct answer: 1
fun
Explanation:
The __FUNCTION__ magic constant returns name of the function that is fun().
12) What is the correct output of the given code snippets in PHP?
<?php
echo __FUNCTION__;
?>
- It will print the program name.
- It will not print anything.
- Error
- None of the above
Correct answer: 2
It will not print anything.
Explanation:
It will not print anything, because, in the above code, __FUNCTION__ is not used in any function. As we know that the __FUNCTION__ constant returns name of the function where it is used.
Learn: PHP echo statement
13) Which of the following magic constant is used to return the trait name, where it is used?
- __TRIAT__
- __TRAIT_NAME__
- __TRAIT__
- __TR__
Correct answer: 3
__TRAIT__
Explanation:
The __TRAIT__ constant is used to return the trait name, where it is used.
14) Which of the following magic constant is used to return the name of the member function of class, where it is used?
- __MEMBER__
- __FUNCTION__
- __METHOD__
- None of the above
Correct answer: 3
__METHOD__
Explanation:
The __METHOD__ magic constant is used to return the name of the member function or method of the class, where it is used.
15) Is there any magic constant, that’s name does not start with a double underscore?
- Yes
- No
Correct answer: 1
Yes
Explanation:
The ClassName::class magic constant, that's name does not start with a double underscore. It is used to return the fully qualified name of the class.