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

Structures, Unions, Enums Questions and Answers

12.

The number of distinct nodes the following struct declaration can point to is

struct node
{
   struct node *left;
   struct node *centre;
   struct node *right;
};

 

Answer: A

Correct answer: All of the above

The structure holds three pointers, so at most it points to three nodes - but nothing requires them to be different. Two of the pointers may hold the same address, or all three may, so the number of distinct nodes reached may be 1, 2 or 3. All the counts offered are possible.

Please log in or register to report this question.

13.

#include <stdio.h>

typedef enum{
    Male, Female=-1
}SEX;

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

 

Answer: D

Correct answer: 0 -1

Male takes the default first value, 0, and Female is then explicitly set to -1. Explicit values may be given in any order and may be negative - the compiler only fills in the ones you leave out, continuing from the last value given.

Please log in or register to report this question.

15.

What is the output of this program?

#include <stdio.h>
int main()
{
    union demo {
        int x;
        int y;
    };
 
    union demo a = 100;
    printf("%d %d",a.x,a.y);
}

 

Answer: D

Correct answer: Compilation Error

A union cannot be initialised from a bare value: union demo a = 100; is rejected, because the compiler must be told which member is meant. union demo a = {100}; would initialise the first member, and {.y = 100} would name it explicitly.

Please log in or register to report this question.

16.

What is the output of this program?

#include <stdio.h>
void main()
{
  struct number
  {
    int no;
    char name[20];
  };
  struct number s;
  s.no = 50;
  printf("%d", s.no);
}

 

Answer: D

Correct answer: 50

Declaring the structure type inside a function is legal - it is simply local to that block. The member is set and printed, giving 50.

Please log in or register to report this question.

17.

Is the size of a union the size of its largest member?

Answer: A

Correct answer: Yes

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.

18.

It is not possible to create an array of pointer to structures.

Answer: B

Correct answer: FALSE

An array of pointers to structures is perfectly ordinary - struct node *arr[10]; - and it is common, because swapping pointers is far cheaper than copying whole structures. So the statement that it is impossible is false.

Please log in or register to report this question.

Answer: D

Correct answer: struct temp s; struct temp{}; main(){}

This one is an error. Declaring struct temp s; before the structure has been defined means declaring an object of an incomplete type - the compiler does not yet know how much storage to reserve. The other two are fine: the first defines the type and a variable together, and the second defines the type before using it. (A pointer to an incomplete type would be legal, since a pointer’s size is known.)

Please log in or register to report this question.

Answer: A

Correct answer: An array, each element of which is a pointer to a structure of type node

Subscript binds tighter than the dereference, so struct node *s[10] groups as struct node *(s[10]) - an array of 10 pointers, each to a struct node. A pointer to an array would need parentheses in the other place: struct node (*s)[10].

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