Consider the following declaration: enum fruitType{ORANGE,APPLE,BANANA,GRAPE,STR
ID: 3912294 • Letter: C
Question
Consider the following declaration:
enum fruitType{ORANGE,APPLE,BANANA,GRAPE,STRAWBERRY,MANGO,GUAVA,
PINEAPPLE, KIWI};
fruitType fruit;
Answer the following questions.
a. What is the value of static_cast<int>(STRAWBERRY)?
b. What is the value, if any, of the following expression? static_cast<fruitType>(static_cast<int>(MANGO) - 2)
c. What is the value, if any, of the following expression? static_cast<fruitType>(static_cast<int>(GRAPE) + 2)
d. What is the value, if any, of the expression:
BANANA <= KIWI
e. What is the output, if any, of the following code?
for (fruit = BANANA; fruit < PINEAPPLE; fruit++)
cout << static_cast<int>(fruit) << ", ";
cout << endl;
I need help with c, d, & e, please.
Explanation / Answer
a.
4
The enum numeric values starts from 0,1,2,3,..... if not initialized. So casting STRAWBERRY to int will igive value 4
b.
3
int value of MANGO -2 = 3(of GRAPE)
an integer value cannot be casted to fruitType so will output 3
c.
5
int value of GRAPE + 2 = 5 ( of MANGO)
an integer value cannot be casted back to fruitType so will output 5
d.
1
The expression BANANA <= KIWI returns true and the output will be 1 corresponding to true.
bool x = (BANANA <= KIWI);
cout<<x; // will print 1
e.
compilation error. fruit is an enum of type fruitType, it is not an integer to be used in for loop .
Do ask if any doubt. Please upvote.