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

Structures, Unions, Enums Questions and Answers

71.

Consider the following C declaration

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

Assume that 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 considerations, is

Answer: C

Correct answer: 18 bytes

Add the structure’s members, taking the union as its largest member:

short s[5]5 × 2 = 10 bytes
union {float; long}max(4, 8) = 8 bytes
total18 bytes

A structure sums its members; a union takes only the largest of its own.

Please log in or register to report this question.

72.

Which of the following are incorrect syntax for pointer to structure?

(Assuming struct temp{int b;}*my_struct;)

 

Answer: A

Correct answer: *my_struct.b = 10;

The dot binds tighter than the dereference, so *my_struct.b parses as *(my_struct.b) - which tries to take a member of a pointer and then dereference it. The two valid forms are (*my_struct).b, with the parentheses forcing the right order, and its shorthand my_struct->b.

Please log in or register to report this question.

73.

What will be the output of the following C code?

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

 

Answer: A

Correct answer: Error

An enumerator is a constant, not a variable, so c++ is rejected - there is nothing to increment. An enum variable could be incremented; the name of an enumerator could not.

Please log in or register to report this question.

74.

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.

75.

Assume that size of an integer is 32 bit. What is the output of following program?

#include <stdio.h>
struct st
{
    int x;
    static int y;
};
 
int main()
{
    printf("%d", sizeof(struct st));
    return 0;
}

 

Answer: C

Correct answer: Compiler Error

There is no static member in C - that is a C++ feature. A structure in C is a layout of data, with no storage shared across instances, so the declaration is rejected. (In C++ it would compile, and sizeof would be 4, since a static member is not part of each object.)

Please log in or register to report this question.

76.

Which operator connects the structure name to its member name?

Answer: B

Correct answer: .

The dot connects a structure variable to its member: s.member. Through a pointer the arrow is used instead, p->member, which is shorthand for (*p).member.

Please log in or register to report this question.

77.

What is the output of this program?

#include <stdio.h>
 struct 
{
 int i;
 float ft;
}decl;
int main(){
    decl.i = 4;
 decl.ft = 7.96623;
 printf("%d %.2f", decl.i, decl.ft);
 return 0;
}

 

Answer: A

Correct answer: 4 7.97

%.2f prints the float rounded to two decimal places, so 7.96623 comes out as 7.97. The rounding is in the output only - the stored value is unchanged. The structure has no tag, but that is legal because the variable decl is declared with it.

Please log in or register to report this question.

78.

Which of the following accesses a variable in structure *b?

Answer: A

Correct answer: b->var;

*b being the structure means b is a pointer to it, so the member is reached with the arrow: b->var, which is shorthand for (*b).var. The parentheses in that longer form are essential, since . binds tighter than *.

Please log in or register to report this question.

79.

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.

80.

Point out the error( if any) in the following code.

#include <stdio.h>
enum sanfoundry
{
    a,b,c
};
enum sanfoundry g;
main()
{
    g++;
    printf("%d",g);
}

 

Answer: D

The code shown above does not result in any error. This is because, although the value of enum constants cannot be changed, yet it is possible to modify the value of the enum instance variable(that is ‘g’ in this case).

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