250+ TOP MCQs on Enums and Answers

C Multiple Choice Questions & Answers on “Enums – 2”.

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

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

a) error
b) pass
c) fail
d) 0
Answer: a
Clarification: 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;

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

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

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

#include
#define MAX 4
enum 
{
    a,b=3,c
};
main()
{
    if(MAX!=c)
        printtf("hello");
    else
        printf("welcome");
}

a) error
b) hello
c) welcome
d) 2
Answer: c
Clarification: 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.

4. Arithmetic operations such as addition, subtraction, multiplication and division are allowed on enumerated constants.
a) True
b) False
Answer: a
Clarification: 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
enum 
{
    a,b,c
};
enum  g;
main()
{
    g++;
    printf("%d",g);
}

a) Error in the statement: a,b,c
b) Error in the statement: enum g;
c) Error in the statement: g++
d) No error
Answer: d
Clarification: 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
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
Answer: d
Clarification: 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 
enum 
 {
    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
Answer: c
Clarification: 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
enum 
{
    a=1,b,c,d,e
};
int main()
{
    printf("%d",b*c+e-d);
}

a) Error
b) 7
c) 2
d) 4
Answer: b
Clarification: 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
enum 
{
    a,b,c=5
};
int main()
{
    enum  s;
    b=10;
    printf("%d",b);
}

a) Error
b) 10
c) 1
d) 4
Answer: a
Clarification: 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
enum 
{
    a=1,b
};
enum 1
{
    c,d
};
int main()
{
    enum 1 s1=c;
    enum 1 s=a;
    enum  s2=d;
    printf("%d",s);
    printf("%d",s1);
    printf("%d",s2);
}

a) Error
b) 011
c) 110
d) 101
Answer: d
Clarification: 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.

Leave a Reply

Your email address will not be published. Required fields are marked *