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

Structures, Unions, Enums Questions and Answers

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.

12.

What is the output of this program?

#include <stdio.h>         
void main()
{
struct demo{
    char * a;
    int n;
};

struct demo p = {"hello", 2015};
struct demo q = p;
printf("%d", printf("%s",q.a));
}

 

Answer: C

Correct answer: hello5

The inner printf runs first, writing hello and returning 5 - the number of characters it wrote. The outer printf then prints that return value. So the output is hello5, in that order. (The assignment q = p copies the structure, including the pointer, so both point at the same string.)

Please log in or register to report this question.

13.

What will be the output of the program if it is executed like below?
cmd> sample

/* sample.c */
#include <stdio.h>

int main(int argc, char **argv)
{
    printf("%s\n", argv[argc-1]);
    return 0;
}

 

Answer: A

Correct answer: sample

argv[0] is the program name as invoked, and with no further arguments argc is 1 - so argv[argc-1] is argv[0], which prints sample. The array always has argc entries plus a null pointer at argv[argc].

Please log in or register to report this question.

15.

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.

16.

#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.

18.

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.

19.

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.

20.

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.

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