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

Structures, Unions, Enums Questions and Answers

81.

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.

82.

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.

83.

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.

84.

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.

86.

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.

88.

What is the output of this program?

#include <stdio.h>
int main()
{
    enum days {MON=-1, TUE, WED=4, THU, FRI, SAT};
    printf("%d, %d, %d, %d, %d, %d", MON, TUE, WED, THU, FRI, SAT);
    return 0;
}

 

Answer: A

Correct answer: -1 0 4 5 6 7

Enumerators continue from the last value given. MON is set to -1, so TUE is 0; WED is set to 4, so THU, FRI and SAT are 5, 6, 7. Values may be repeated or given out of order - the compiler only fills in what you leave out.

Please log in or register to report this question.

89.

Predict the output of following C program

#include <stdio.h>
struct Point
{
  int x, y, z;
};
 
int main()
{
  struct Point p1 = {.y = 0, .z = 1, .x = 2};
  printf("%d %d %d", p1.x, p1.y, p1.z);
  return 0;
}

 

Answer: B

Correct answer: 2 0 1

These are designated initialisers, introduced in C99: each value names the member it initialises, so their order in the braces is irrelevant. .x = 2, .y = 0, .z = 1 whatever order they are written in, and the program prints 2 0 1.

Please log in or register to report this question.

90.

What will be the output of the following C code?

#include <stdio.h>
enum sanfoundry
{
    a,b,c=5
};
int main()
{
    enum sanfoundry s;
    b=10;
    printf("%d",b);
}

 

Answer: A

Correct answer: Error

b is an enumerator - a constant - so b = 10; is an assignment to something that is not an lvalue, and the compiler rejects it. Only an enum variable can be assigned to.

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