PHP Constants MCQs
PHP Constants MCQs : This section focuses on "Constants" in PHP. These Multiple Choice Questions (mcq) should be practiced to improve the PHP skills required for various interviews (campus interview, walk-in interview, company interview), placements, entrance exams and other competitive examinations.
1. A ___________ is a name or an identifier for a simple value.
A. variable
B. data type
C. constant
D. array
View Answer
Ans : C
Explanation: A constant is a name or an identifier for a simple value.
2. Can we change the value of constant during the execution of the script.
A. can
B. can not
C. depends on data type
D. depends on variable
View Answer
Ans : B
Explanation: A constant value cannot change during the execution of the script
3. By default, a constant is _________.
A. case-sensitive
B. case-insensitive.
C. Both A and B
D. None of the above
View Answer
Ans : A
Explanation: By default, a constant is case-sensitive. By convention, constant identifiers are always uppercase. A constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. If you have defined a constant, it can never be changed or undefined.
4. To define a constant you have to use ________ function.
A. const()
B. constant()
C. def()
D. define()
View Answer
Ans : D
Explanation: To define a constant you have to use define() function and to retrieve the value of a constant, you have to simply specifying its name.
5. _________ function is used to read a constant value.
A. read()
B. get()
C. constant()
D. define()
View Answer
Ans : C
Explanation: You can also use the function constant() to read a constant's value if you wish to obtain the constant's name dynamically.
6. There is no need to write a dollar sign ($) before a constant
A. TRUE
B. FALSE
C. Can be true or false
D. Can not say
View Answer
Ans : A
Explanation: True, There is no need to write a dollar sign ($) before a constant, where as in Variable one has to write a dollar sign.
7. Which of the following is true about constant?
A. Once the Constants have been set, may not be redefined or undefined
B. Constants may be defined and accessed anywhere without regard to variable scoping rules
C. Constants cannot be defined by simple assignment, they may only be defined using the define() function
D. All of the above
View Answer
Ans : D
Explanation: All of the above are true about constant.
8. How many magical constants there?
A. 4
B. 5
C. 6
D. 7
View Answer
Ans : B
Explanation: There are five magical constants that change depending on where they are used.
9. What will be the output of the following PHP code?
class myObject { }
define('myObject::CONSTANT', 'test');
echo myObject::CONSTANT;
A. test
B. error
C. myObject::CONSTANT
D. no output
View Answer
Ans : B
Explanation: Class constants cannot be defined outside class
10. What will be the output of the following PHP code?
define('IF', 42);
echo ""IF: "", IF;
A. IF:42
B. No output
C. IF:
D. Error
View Answer
Ans : D
Explanation: Keyword like IF cannot be used as constant names.
Discussion