Here is a listing of online C++ test questions on “Constants” along with answers, explanations and/or solutions:
1. The constants are also called as _____________
a) const
b) preprocessor
c) literals
d) variables
Answer: c
Clarification: Other name for Constants are literals.
2. What are the parts of the literal constants?
a) integer numerals
b) floating-point numerals
c) strings and boolean values
d) all of the mentioned
Answer: d
Clarification: Because these are the types used to declare variables and so these can be declared as constants.
3. How are the constants declared?
a) const keyword
b) #define preprocessor
c) both const keyword and #define preprocessor
d) $define
Answer: c
Clarification: The const will declare with a specific type value and #define is used to declare user-defined constants.
4. What will be the output of the following C++ code?
-
#include -
using namespace std;
-
int main()
-
{ -
int const p = 5;
-
cout << ++p;
-
return 0;
-
}
a) 5
b) 6
c) Error
d) 8
Answer: c
Clarification: We cannot modify a constant integer value.
5. What will be the output of the following C++ code?
-
#include -
using namespace std;
-
#define PI 3.14159 -
int main ()
-
{ -
float r = 2;
-
float circle;
-
circle = 2 * PI * r;
-
cout << circle;
-
return 0;
-
}
a) 12.5664
b) 13.5664
c) 10
d) 15
Answer: a
Clarification: In this program, we are finding the area of the circle by using concern formula.
Output:
$ g++ cons.cpp $ a.out 12.5664
6. Which of the following statement is not true about preprocessor directives?
a) These are lines read and processed by the preprocessor
b) They do not produce any code by themselves
c) These must be written on their own line
d) They end with a semicolon
Answer: d
Clarification: No terminating character required for preprocessor directives statements.
7. Regarding the following statement which of the statements is true?
const int a = 100;
a) Declares a variable a with 100 as its initial value
b) Declares a construction a with 100 as its initial value
c) Declares a constant a whose value will be 100
d) Constructs an integer type variable with an as identifier and 100 as the value
Answer: c
Clarification: Because the const is used to declare non-changeable values only.
8. The difference between x and ‘x’ is?
a) The first one refers to a variable whose identifier is x and the second one refers to the character constant x
b) The first one is a character constant x and the second one is the string literal x
c) Both are same
d) Both are string literal
Answer: a
Clarification: In a C++ code, names with quotes like ‘x’ represent a character or string(in case of a collection of characters) whereas without quotes they represent an identifier.
9. How to declare a wide character in the string literal?
a) L prefix
b) l prefix
c) W prefix
d) Z prefix
Answer: a
Clarification: It can turn this as the wide character instead of narrow characters.
