C++ Programming MCQ – Constants

This section on online C++ test focuses on “Constants”. One shall practice these test questions to improve their C++ programming skills needed for various interviews (campus interviews, walk-in interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our online C++ test questions come with the detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ 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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: d
Explanation: 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
View Answer

Answer: c
Explanation: The const will declare with a specific type value and #define is used to declare user-defined constants.
advertisement
advertisement

4. What will be the output of the following C++ code?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int  const  p = 5;
  6.         cout << ++p;
  7.         return 0;
  8.     }

a) 5
b) 6
c) Error
d) 8
View Answer

Answer: c
Explanation: We cannot modify a constant integer value.
Note: Join free Sanfoundry classes at Telegram or Youtube

5. What will be the output of the following C++ code?

advertisement
  1.     #include <iostream>
  2.     using namespace std;
  3.     #define PI 3.14159
  4.     int main ()
  5.     {
  6.         float r = 2;
  7.         float circle;
  8.         circle = 2 * PI * r;
  9.         cout << circle;
  10.         return 0;
  11.     }

a) 12.5664
b) 13.5664
c) 10
d) 15
View Answer

Answer: a
Explanation: In this program, we are finding the area of the circle by using concern formula.
Output:

advertisement
$ 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
View Answer

Answer: d
Explanation: 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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: a
Explanation: It can turn this as the wide character instead of narrow characters.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.