Machine-translated by Google. Questions are authored in English.
Top

Structures, Unions, Enums Questions and Answers

101.

What will be output for the following code?

Answer: A

Correct answer: Error, though the code this depends on is missing

The program listing this question asks about is absent from the record - only the words “What will be output for the following code?” survive, with nothing following them. The four options are “Error”, 5, 6 and 2, and the item is keyed to “Error”, so the original code presumably contained a mistake the compiler would reject. Which mistake cannot be recovered from what remains. The topic is structures, unions and enumerations, where the commonest sources of a compile error are using the dot operator on a pointer to a structure instead of ->, omitting the tag or the semicolon that must follow a struct definition, assigning to an enumeration constant, or taking sizeof an incomplete type. The item needs its listing restored before it can be worked.

Please log in or register to report this question.

102.

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;
}

 

Answer: B

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.

Please log in or register to report this question.

103.

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);
}

 

Answer: D

Correct answer: Error

An enumerator’s value must be an integer constant expression. b = 3.56 is a floating-point constant, so the declaration is rejected. Enumerations are a way of naming integers, and nothing else.

Please log in or register to report this question.

104.

If we do not explicitly assign values to enum names, the compiler by default starts assigning values from

Answer: D

Correct answer: 0

Unless told otherwise the compiler starts at 0 and adds one for each following enumerator. Where a value is given explicitly, counting resumes from there - so {a, b = 5, c} gives 0, 5, 6.

Please log in or register to report this question.

Loading…
Tags: Structures, Unions, Enums Questions and Answers || Structures, Unions, Enums MCQ Questions and Answers || Structures, Unions, Enums GK Questions and Answers || Structures, Unions, Enums GK MCQ Questions || Structures, Unions, Enums Multiple Choice Questions and Answers || Structures, Unions, Enums GK || GK on Structures, Unions, Enums || C Programming Questions and Answers || C Programming MCQ Questions and Answers || C Programming GK Questions and Answers || GK on C Programming