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

Structures, Unions, Enums Questions and Answers

Answer: C

Correct answer: No compile error and two elements of arr[] would be defined and initialized. Output would be “1 0 1 and 2 0 2”.

The initialiser uses designated initialisers with array indices: [0].a = {1}, [1].a = {2} and so on. This is valid C99, and it sizes arr at two elements, since index 1 is the highest named. Everything not explicitly initialised is zero-filled, so a[1] is 0 in both - giving 1 0 1 and 2 0 2.

Please log in or register to report this question.

Answer: D

Correct answer: No compile error and it’ll print 100 A.

Defining a structure type inline with the variable is legal, and designated initialisers may be given in any order, since each names its target. So .c = 'A', .i = 100 is fine and prints 100 A.

Please log in or register to report this question.

23.

Pick the best statement for the below program:

#include "stdio.h"
 
int main()
{
 union {int i1; int i2;} myVar = {.i2 =100};
 printf("%d %d",myVar.i1, myVar.i2);
 return 0;
}

 

Answer: C

Correct answer: No compile error and it’ll print “100 100”

The two members of a union share the same storage, so writing 100 through i2 makes it visible through i1 as well - and since both are int, the bytes are interpreted the same way. The output is 100 100. (Reading a union through a member other than the one last written is only well defined when the types match like this.)

Please log in or register to report this question.

24.

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.

25.

Consider the following declaration :

struct addr {
     char city[10];
     char street[30];
     int pin ;
};

struct {
char name[30];
int gender;
struct addr locate;
} person , *kd = &person ;

Then *(kd -> name +2) can be used instead of

Answer: C

Correct answer: *((*kd).name + 2 )

kd->name is by definition (*kd).name, so the two forms are the same expression written differently, and dereferencing +2 gives the third character. The others fail: person.name + 2 is a pointer, not the character it points to, and kd->(name+2) is not valid syntax at all - the right operand of -> must be a member name.

Please log in or register to report this question.

26.

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.

27.

Which operator connects the structure name to its member name?

Answer: C

Correct answer: .

The dot operator selects a member of a structure variable: s.member. The arrow -> does the same through a pointer, and is exactly equivalent to (*p).member.

Please log in or register to report this question.

28.

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.

29.

What is the correct syntax to declare a function foo() which receives an array of structure in function?

Answer: A

Correct answer: void foo(struct *var);

An array argument decays to a pointer to its first element, so a function receiving an array of structures takes a pointer to structure. (In real code the tag must be named - void foo(struct tagname *var); - since struct alone is not a type.) Passing an array never copies it, so the length must be passed separately.

Please log in or register to report this question.

Answer: B

When a variable is associated with a structure, the compiler allocates the memory for each member. The size of structure is greater than or equal to the sum of sizes of its members. The smaller members may end with unused slack bytes. While in case of Union when a variable is associated with a union, the compiler allocates the memory by considering the size of the largest memory. So, size of union is equal to the size of largest member.

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