C MCQs on “Enums – 1”.
1. A user defined data type, which is used to assign names to integral constants is called ____________
a) Union
b) Array
c) Structure
d) Enum
Answer: d
Clarification: Enumeration (enum) is a user defined data type in C. It is used to assign names to integral constants. The names make a program easy to read and maintain.
2. What will be the output of the following C code?
#includeenum colour { blue, red, yellow }; main() { enum colour c; c=yellow; printf("%d",c); }
a) 1
b) 2
c) 0
d) Error
Answer: c
Clarification: Enum variables are automatically assigned values if no value is specified. The compiler by default assigns values starting from 0. Therefore, in the above code, blue gets 0, red gets 1 and yellow gets 2.
3. Point out the error (if any) in the following C code?
#includeenum hello { a,b,c; }; main() { enum hello m; printf("%d",m); }
a) No error
b) Error in the statement: a,b,c;
c) Error in the statement: enum hello m;
d) Error in the statement: printf(“%d”,m);
Answer: b
Clarification: In the above code, there is a semi colon given at the end of the list of variables. This results in an error. Semi colon is to be put only after the closing brace of the enum, not after the list of variables.
4. String handling functions such as strcmp(), strcpy() etc can be used with enumerated types.
a) True
b) False
Answer: b
Clarification: Enumerated types are not strings. Hence it is not possible to use string handling functions with enumerated data types.
5. What will be the output of the following C code?
#includeenum hello { a,b=99,c,d=-1 }; main() { enum hello m; printf("%dn%dn%dn%dn",a,b,c,d); }
a)
1 99 100 -1
b) Error
c)
0 99 100 -1
d)
0 1 2 3
View Answer
Answer: c
Clarification: We can assign values to some of the symbol names in any order. All unassigned names get the value as the value of previous name plus one.
6. Pick the incorrect statement with respect to enums.
a) Two enum symbols cannot have the same value
b) Only integer constants are allowed in enums
c) It is not possible to change the value of enum symbols
d) Enum variables are automatically assigned values if no value is specified
Answer: a
Clarification: The statement that two enum symbols cannot have the same value is incorrect. Any number of enum symbols can have the same value.
7. What will be the output of the following C code?
#includeenum { a=2,b=3.56 }; enum s; main() { printf("%d%d",a,b); }
a) 2 3
b) 0 1
c) 2 3.56
d) Error
Answer: d
Clarification: The above code will result in an error because 3.56 is not an integer constant. Only integer constants are allowed in enums.
8. What will be the output of the following C code?
#includeenum class { a,b,c }; enum class m; main() { printf("%d",sizeof(m)); }
a) 3
b) Same as the size of an integer
c) 3 times the size of an integer
d) Error
Answer: b
Clarification: The output will be the same as the size of an integer, that is 4 on a 32 bit platform.
9. What will be the output of the following C code?
#includeenum hi{a,b,c}; enum hello{c,d,e}; main() { enum hi h; h=b; printf("%d",h); return 0; }
a) 2
b) 1
c) Error
d) 0
Answer: c
Clarification: The code shown above results in an error: re-declaration of enumerator ‘c’. All enumerator constants should be unique in their scope.
10. What will be the output of the following C code?
#includeenum { a,b,c=5 }; enum s; main() { c++; printf("%d",c); }
a) Error
b) 5
c) 6
d) 2
Answer: a
Clarification: The above code results in an error because it is not possible to modify the value of enum constants. In the above code, we have tried to increment the value of c. This results in an error.