C Questions and Answers – Enums – 1

«
»

This set of C MCQs focuses 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
View Answer

Answer: d
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.
advertisement

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

Answer: c
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.
advertisement
advertisement

3. Point out the error (if any) in the following C code?

advertisement
#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

Answer: b
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.

advertisement

4. String handling functions such as strcmp(), strcpy() etc can be used with enumerated types.
a) True
b) False
View Answer

Answer: b
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?

advertisement
#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
    3
View Answer
Answer: c
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

Answer: a
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

Answer: d
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

Answer: b
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

Answer: c
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

Answer: a
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.

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!

advertisement
advertisement

Leave a Comment

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter