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

Structures, Unions, Enums Questions and Answers

81.

A user-defined data type can be created with ___________

Answer: D

Correct answer: all of the mentioned

All three create user-defined types. struct aggregates members of different types; enum names a set of integer constants; and typedef gives a new name to an existing type, which is how struct node * becomes NODEPTR. A union belongs on the same list.

Please log in or register to report this question.

82.

String handling functions such as strcmp(), strcpy() etc can be used with enumerated types.

Answer: B

Correct answer: FALSE

An enumerated value is an integer, not a string, so passing one to strcmp or strcpy would pass a small integer where a char * is expected - a type error, and undefined behaviour if it compiled. The names exist only in the source; nothing of them survives into the program.

Please log in or register to report this question.

83.

Can we have enum containing enum with the same constant?

#include "stdio.h"

enum Yenum{
       C, CPP, Java
};

enum Xenum{
    C, CPP, Java, Yenum
};

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

 

Answer: A

Correct answer: Enum can contain enum but duplicate constants are not allowed

The two enumerations both declare C, CPP and Java, and enumerators occupy the ordinary identifier namespace at file scope - so the second set are redefinitions and the program will not compile. Naming one enumeration as a member of another is not the difficulty; the repeated constant names are.

Please log in or register to report this question.

84.

What is the output of this program?

#include <stdio.h>
struct result{
  char sub[20];
  int marks;
};
void main()
{
      struct result res[] = { {"Maths",100},
      {"Science",90},
      {"English",85}
      };
    printf("%s ", res[1].sub);
    printf("%d", (*(res+2)).marks);
}

 

Answer: B

Correct answer: Science 85

res[1].sub is the second element’s subject, Science. (*(res+2)).marks is the same as res[2].marks - the third element - which is 85. Both subscripts count from 0.

Please log in or register to report this question.

85.

What will be the output of the following code?

#include <stdio.h>
enum week { Mon, Tue, Wed };

int main()
{
    enum week today;
    today = Wed;
    Wed = 5;
    printf("%d", today);
    return 0;
}

Answer: A

Correct answer: error

Wed is an enumerator, a constant, so Wed = 5; is an assignment to something that cannot be assigned to. The compiler rejects it. Assigning to the variable, today = 5;, would be allowed.

Please log in or register to report this question.

86.

Consider the following C declaration

struct {
short s[5];
union {
float y;
long z;
}u;
}t;

Assume that the objects of the type short, float and long occupy 2 bytes, 4 bytes and 8 bytes, respectively. The memory requirement for variable t, ignoring alignment consideration, is

Answer: B

Correct answer: 18 bytes

The array contributes \(5 \times 2 = 10\) bytes, and the union takes the size of its largest member, \(\max(4, 8) = 8\). The structure sums them, giving 18 bytes. The union’s members overlap, which is why the float costs nothing extra beyond the long.

Please log in or register to report this question.

87.

Which of the following cannot be a structure member?

Answer: A

Correct answer: Function

A structure member must be an object with storage, and a function is not - so a function cannot be a member of a C structure. What can be a member is a pointer to a function, which is how C programs implement dispatch tables and something resembling methods. Arrays and nested structures are ordinary members.

Please log in or register to report this question.

Answer: D

Correct answer: Biggest member in the union

A union’s members all occupy the same storage, so its size is that of its largest member - enough to hold whichever one is in use. A structure, by contrast, gives every member its own space and so is at least the sum of them.

Please log in or register to report this question.

89.

What is the correct syntax for accessing a member of the ith structure in an array of structures?

Assuming: 
struct temp
{
  int b;
}s[50];

 

Answer: D

Correct answer: s[i].b;

s is an array of structures, so subscript first to select the element and then use the dot to select its member: s[i].b. The other forms subscript the member or put the operators the wrong way round.

Please log in or register to report this question.

Answer: C

Correct answer: No compile error and it’ll create array arr of 1 element. Each of the element of arr contain a struct field of int array of 2 elements. arr[0]. a[0] would be 1 and arr[0].a[1] would be 2.

Note the missing inner braces: {1,2} is a flat list, so the values are used to fill the aggregate in order. Both go into the first structure - a[0] = 1 and a[1] = 2 - and nothing is left for a second element, so arr has just one. Writing {{1},{2}} would have given two elements instead.

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