C++ Programming Questions and Answers – Enumerations

This section on C++ interview questions and answers focuses on “Enumerations”. One shall practice these interview 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 C++ interview 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 “Enumerations” along with answers, explanations and/or solutions:

1. Identify the incorrect option.
a) enumerators are constants
b) enumerators are user-defined types
c) enumerators are same as macros
d) enumerator values start from 0 by default
View Answer

Answer: c
Explanation: Enumerators are used in order to create our own types whereas macros are textual substitutions.

2. In which type do the enumerators are stored by the compiler?
a) string
b) integer
c) float
d) string & float
View Answer

Answer: b
Explanation: In C++, enumerations are stored as integers by the compiler starting with 0.

3. To which of these enumerators can be assigned?
a) integer
b) negative
c) enumerator
d) all of the mentioned
View Answer

Answer: d
Explanation: Since enumerators evaluate to integers, and integers can be assigned to enumerators, enumerators can be assigned to other enumerators.
advertisement
advertisement

4. What will happen when defining the enumerated type?
a) it will not allocate memory
b) it will allocate memory
c) it will not allocate memory to its variables
d) allocate memory to objects
View Answer

Answer: a
Explanation: Enumerator will allocate the memory when its variables are defined.

5. Which variable does equals in size with enum variable?
a) int variable
b) float variable
c) string variable
d) float & string variable
View Answer

Answer: a
Explanation: The enum variable is converted to an integer and stored by the compiler. So both are equal in size.
Note: Join free Sanfoundry classes at Telegram or Youtube

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     enum  cat 
  4.     {
  5.         temp = 7
  6.     };
  7.     int main()
  8.     {
  9.         int age = 14;
  10.         age /= temp;
  11.         cout << "If you were cat, you would be " << age << endl;
  12.         return 0;
  13.     }

a) If you were cat, you would be 5
b) If you were cat, you would be 2
c) If you were cat, you would be 7
d) If you were cat, you would be 9
View Answer

Answer: b
Explanation: The age will be divided by using compound assignment operator and so it will return the age of the cat according to your age.

advertisement
$ g++ enum1.cpp
$ a.out
If you were cat, you would be 2

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

advertisement
  1.     #include <iostream>
  2.     using namespace std;
  3.     enum test 
  4.     {
  5.         A = 32, B, C
  6.     };
  7.     int main()
  8.     {
  9.         cout << A << B<< C;
  10.         return 0;
  11.     }

a) 323334
b) 323232
c) 323130
d) 323134
View Answer

Answer: a
Explanation: If we not assigned any value to enum variable means, then the next number to initialized number will be allocated to the variable.
Output:

$ g++ enum2.cpp
$ a.out
323334

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     enum colour 
  4.     {
  5.         green, red, blue, white, yellow, pink
  6.     };
  7.     int main()
  8.     {
  9.         cout << green<< red<< blue<< white<< yellow<< pink;
  10.         return 0;
  11.     }

a) 012345
b) 123456
c) compile time error
d) runtime error
View Answer

Answer: a
Explanation: The enumerator values start from zero if it is unassigned.
Output:

$ g++ enum3.cpp
$ a.out
012345

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         enum channel {star, sony, zee};
  6.         enum symbol {hash, star};
  7.         int i = 0;
  8.         for (i = star; i <= zee; i++) 
  9.         {
  10.             printf("%d ", i);
  11.         }
  12.         return 0;
  13.     }

a) 012
b) 123
c) compile time error
d) runtime error
View Answer

Answer: c
Explanation: Enumeration variable ‘star’ appears two times in main() which causes the error. An enumaration constant must be unique within the scope.

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int i;
  6.         enum month 
  7.         {
  8.             JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC
  9.         };
  10.         for (i = MAR; i <= NOV; i++)
  11.             cout << i;
  12.         return 0;
  13.     }

a) 01234567891011
b) 123456789101112
c) 34567891011
d) 123456789
View Answer

Answer: c
Explanation: We are getting the values from march to november and printing its concern number.

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.