This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Enums – 1”.
Pre-requisite for C Enums MCQ set: Video Tutorial on C Enums (Enumeration).
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
View Answer
Explanation: 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?
#include<stdio.h> enum colour { blue, red, yellow }; main() { enum colour c; c=yellow; printf("%d",c); }
a) 1
b) 2
c) 0
d) Error
View Answer
Explanation: 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?
#include<stdio.h> enum 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);
View Answer
Explanation: 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
View Answer
Explanation: 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?
#include<stdio.h> enum hello { a,b=99,c,d=-1 }; main() { enum hello m; printf("%d\n%d\n%d\n%d\n",a,b,c,d); }
a)
1 99 100 -1
b) Error
c)
0 99 100 -1
d)
0 1 2 3View Answer
Explanation: 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
View Answer
Explanation: 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?
#include<stdio.h> enum sanfoundry { a=2,b=3.56 }; enum sanfoundry s; main() { printf("%d%d",a,b); }
a) 2 3
b) 0 1
c) 2 3.56
d) Error
View Answer
Explanation: 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?
#include<stdio.h> enum 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
View Answer
Explanation: 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?
#include<stdio.h> enum 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
View Answer
Explanation: 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?
#include<stdio.h> enum sanfoundry { a,b,c=5 }; enum sanfoundry s; main() { c++; printf("%d",c); }
a) Error
b) 5
c) 6
d) 2
View Answer
Explanation: 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.
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 Computer Science Books
- Practice Computer Science MCQs
- Apply for Computer Science Internship
- Practice BCA MCQs
- Check C Books