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

Structures, Unions, Enums Questions and Answers

Answer: A

Correct answer: enum flag{constant1, constant2, constant3, ....... };

The syntax is the keyword enum, an optional tag, the constants in braces separated by commas, and a closing semicolon. Parentheses and square brackets are not used, and the keyword must be separated from the tag.

Please log in or register to report this question.

32.

#include <stdio.h>

typedef enum{
    Male, Female=0
}SEX;

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

 

Answer: A

Correct answer: 0 0

Male takes the default first value, 0, and Female is explicitly set to 0 as well. Duplicate values are legal - the restriction is on duplicate names, not on duplicate values - so both print 0.

Please log in or register to report this question.

33.

What is the output of this C code?

#include <stdio.h>
    struct student
    {
        int no;
        char name[20];
    }
    void main()
    {
        struct student s;
        s.no = 8;
        printf("hello");
    }

 

Answer: A

Correct answer: Compile time error

The structure definition is missing its terminating semicolon after the closing brace. Without it the compiler reads on and tries to make sense of void main() as part of the declaration, and the program fails to compile.

Please log in or register to report this question.

Answer: A

Correct answer: Two enum symbols cannot have the same value

The false statement is that two enumerators cannot share a value: they can. enum {a = 1, b = 1}; is perfectly legal, and it is sometimes useful for giving one constant two names. The other three are true: enumerator values must be integer constant expressions, they cannot be assigned to at run time, and any you leave unspecified are filled in automatically, continuing from the last value given.

Please log in or register to report this question.

35.

Arithmetic operations such as addition, subtraction, multiplication and division are allowed on enumerated constants.

Answer: A

Correct answer: True

An enumerator is an integer constant and an enum variable an integer, so all the arithmetic operators apply to them. That is a strength and a weakness: convenient, but it means the compiler will not stop you adding a colour to a weekday.

Please log in or register to report this question.

36.

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

 

Answer: B

Correct answer: 2

Enumerators are numbered from 0 in order unless given explicit values, so blue is 0, red 1 and yellow 2. An enum variable is an integer, so %d prints the number, not the name.

Please log in or register to report this question.

37.

Assume that this code is running on 64 bit system
 

#include <stdio.h>

enum Xenum{
    C, CPP, Java
};

int main()
{
enum Xenum var;
printf("%d",sizeof(var));
return 0;

}

 

Answer: C

Correct answer: 4

An enumeration variable has the size of an int - 4 bytes on the usual platforms - because the compiler stores the constant itself, not a list of them. How many enumerators the type has makes no difference. So on this system it prints 4 - and it would still be 4 if the enumeration had thirty members.

Please log in or register to report this question.

38.

Which of the following are themselves a collection of different data types?

Answer: B

Correct answer: Structures

A structure groups members of different types under one name - an int, a float and a character array together. An array requires every element to be the same type, and a string is an array of characters.

Please log in or register to report this question.

39.

Arithmetic operations are not allowed on enumerated constants.

Answer: B

Correct answer: FALSE

The statement is false. Enumerated constants are integers, so arithmetic on them is perfectly legal - b * c + e - d compiles and computes. C provides no type safety here at all.

Please log in or register to report this question.

40.

What is the output of this program?

#include <stdio.h>         
struct student
{
  char *c;
};
void main()
{
  struct student s[2];
  printf("%d", sizeof(s));
}

 

Answer: D

Correct answer: 8

s is an array of two structures, each containing a single pointer. On a machine with 4-byte pointers that is \(2 \times 4 = 8\) bytes. Note that sizeof reports the size of the array itself, not the length of anything the pointers reach.

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