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
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
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
Explanation: Since enumerators evaluate to integers, and integers can be assigned to enumerators, enumerators can be assigned to other enumerators.
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
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
Explanation: The enum variable is converted to an integer and stored by the compiler. So both are equal in size.
6. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
enum cat
{
temp = 7
};
int main()
{
int age = 14;
age /= temp;
cout << "If you were cat, you would be " << age << endl;
return 0;
}
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
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.
$ g++ enum1.cpp $ a.out If you were cat, you would be 2
7. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
enum test
{
A = 32, B, C
};
int main()
{
cout << A << B<< C;
return 0;
}
a) 323334
b) 323232
c) 323130
d) 323134
View Answer
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?
#include <iostream>
using namespace std;
enum colour
{
green, red, blue, white, yellow, pink
};
int main()
{
cout << green<< red<< blue<< white<< yellow<< pink;
return 0;
}
a) 012345
b) 123456
c) compile time error
d) runtime error
View Answer
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?
#include <iostream>
using namespace std;
int main()
{
enum channel {star, sony, zee};
enum symbol {hash, star};
int i = 0;
for (i = star; i <= zee; i++)
{
printf("%d ", i);
}
return 0;
}
a) 012
b) 123
c) compile time error
d) runtime error
View Answer
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?
#include <iostream>
using namespace std;
int main()
{
int i;
enum month
{
JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC
};
for (i = MAR; i <= NOV; i++)
cout << i;
return 0;
}
a) 01234567891011
b) 123456789101112
c) 34567891011
d) 123456789
View Answer
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.
- Check Programming Books
- Check C++ Books
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Practice Programming MCQs