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

The code shown above results in an error, stating : storage size of t1 and t2 unknown. There is an error in the declaration of these variables in the statement: enum test1 t1,t2;

Please log in or register to report this question.

102.

#include "stdio.h"

typedef enum{
    Male=5, Female
}SEX;

int main()
{
  SEX var = Male;
  SEX var1 = Female;
  printf("%d %d",var, var1);
  return 0;

}

 

Answer: D

Correct answer: 5 6

Male is set to 5, and Female, having no value of its own, continues from it at 6. Enumerators always resume counting from the last value given.

Please log in or register to report this question.

103.

#include <stdio.h>
int main()
{
    struct site
    {
        char name[] = "GeeksQuiz";
        int no_of_pages = 200;
    };
    struct site *ptr;
    printf("%d ", ptr->no_of_pages);
    printf("%s", ptr->name);
    getchar();
    return 0;
}

 

Answer: D

Correct answer: Compiler Error

A structure declaration describes a type, not an object, so its members cannot carry initialisers - both char name[] = "GeeksQuiz"; and int no_of_pages = 200; are rejected. A member array also cannot have an unspecified size. The program never compiles, so the uninitialised ptr never becomes an issue.

Please log in or register to report this question.

104.

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

 

Answer: D

Correct answer: 101

Take each in turn. s1 = c, and in sanfoundry1 c is 0. s = a, and a is 1. s2 = d, which is 1. The prints are in the order s, s1, s2, giving 101. Note that C does not object to assigning a value of one enumeration to a variable of another - they are all just ints.

Please log in or register to report this question.

105.

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.

106.

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.

107.

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.

108.

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…

Other Subjects

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