C Questions and Answers – Enums – 2

«
»

This set of C Multiple Choice Questions & Answers focuses on “Enums – 2”.

1. What will be the output of the following C code?

advertisement
main()
{
    enum resut {pass, fail};
    enum result s1,s2;
    s1=pass;
    s2=fail;
    printf("%d",s1);
}

a) error
b) pass
c) fail
d) 0
View Answer

Answer: a
Explanation: The code shown above results in an error, stating : storage size of s1 and s2 unknown. There is an error in the declaration of these variables in the statement: enum result s1,s2;
advertisement
advertisement

2. What will be the output of the following C code?

#include <stdio.h>
enum example {a = 1, b, c};
enum example example1 = 2;
enum example answer() 
{
    return example1;
}
 
int main()
{
   (answer() == a)? printf("yes"): printf("no");
   return 0;
}

a) yes
b) no
c) 2
d) error
View Answer

Answer: b
Explanation: In the code shown above, the value of example1 is returned by the function answer. The ternary statement prints yes if this value is equal to that of ‘a’ and no if the value is not equal to that of ‘a’. Since the value of ‘a’ is 1 and that returned by the function is 2, therefore no is printed.
advertisement

3. What will be the output of the following C code?

advertisement
#include<stdio.h>
#define MAX 4
enum sanfoundry
{
    a,b=3,c
};
main()
{
    if(MAX!=c)
        printtf("hello");
    else
        printf("welcome");
}

a) error
b) hello
c) welcome
d) 2
View Answer

Answer: c
Explanation: The output will be welcome. The value of the macro MAX is 4. Since 4 is equal to the value of c, welcome is printed.
advertisement

4. Arithmetic operations such as addition, subtraction, multiplication and division are allowed on enumerated constants.
a) True
b) False
View Answer

Answer: a
Explanation: Arithmetic operations such as addition, subtraction, multiplication and division are allowed on enumerated constants. Valid arithmetic operators are +, -, *, / and %.

5. Point out the error( if any) in the following code.

#include<stdio.h>
enum sanfoundry
{
    a,b,c
};
enum sanfoundry g;
main()
{
    g++;
    printf("%d",g);
}

a) Error in the statement: a,b,c
b) Error in the statement: enum sanfoundry g;
c) Error in the statement: g++
d) No error
View Answer

Answer: d
Explanation: The code shown above does not result in any error. This is because, although the value of enum constants cannot be changed, yet it is possible to modify the value of the enum instance variable(that is ‘g’ in this case).

6. What will be the output of the following C code if input given is 2?

#include<stdio.h>
enum day
{
    a,b,c=5,d,e
};
main()
{
    printf("Enter the value for a");
    scanf("%d",a);
    printf("%d",a);
}

a) 2
b) 0
c) 3
d) Error
View Answer

Answer: d
Explanation: The code shown above results in an error. This is because memory is not allocated for constants in enum. Thereore ampersand(&) cannot be used with them. Also, a value has already been assigned to ‘a’, which cannot be changed.

7. What will be the output of the following C code if the code is executed on a 32 bit platform?

#include <stdio.h>
enum sanfoundry
 {
    c = 0,
    d = 10,
    h = 20,
    s = 3
} a;
 
int main()
{
        a = c;
	printf("Size of enum variable = %d bytes", sizeof(a));
	return 0;
}

a) Error
b) Size of enum variable = 2 bytes
c) Size of enum variable = 4 bytes
d) Size of enum variables = 8 bytes
View Answer

Answer: c
Explanation: The size of an enum variable is equal to the size of an integer. The size of an integer on a 32 bit platform is equal to 4 bytes.

8. What will be the output of the following C code?

#include<stdio.h>
enum sanfoundry
{
    a=1,b,c,d,e
};
int main()
{
    printf("%d",b*c+e-d);
}

a) Error
b) 7
c) 2
d) 4
View Answer

Answer: b
Explanation: Since arithmetic operations are allowed on enum constants, hence the expression given is evaluates. b*c+e-d = 2*3+5-4 = 6+5-4 = 7

9. What will be the output of the following C code?

#include<stdio.h>
enum sanfoundry
{
    a,b,c=5
};
int main()
{
    enum sanfoundry s;
    b=10;
    printf("%d",b);
}

a) Error
b) 10
c) 1
d) 4
View Answer

Answer: a
Explanation: The above code results in an error. This is because it is not possible to change the values of enum constants. In the code shown above, the statement: b=10; causes the error.

10. What will be the output of the following C code?

#include<stdio.h>
enum sanfoundry
{
    a=1,b
};
enum sanfoundry1
{
    c,d
};
int main()
{
    enum sanfoundry1 s1=c;
    enum sanfoundry1 s=a;
    enum sanfoundry s2=d;
    printf("%d",s);
    printf("%d",s1);
    printf("%d",s2);
}

a) Error
b) 011
c) 110
d) 101
View Answer

Answer: d
Explanation: The output of the code shown above is 101. This code shows that it is possible to store the symbol of one enum in another enum variable.

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