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

Structures, Unions, Enums Questions and Answers

71.

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.

72.

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.

73.

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.

74.

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.

75.

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.

76.

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.

77.

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.

78.

A user-defined data type can be created with ___________

Answer: D

Correct answer: all of the mentioned

All three create user-defined types. struct aggregates members of different types; enum names a set of integer constants; and typedef gives a new name to an existing type, which is how struct node * becomes NODEPTR. A union belongs on the same list.

Please log in or register to report this question.

79.

String handling functions such as strcmp(), strcpy() etc can be used with enumerated types.

Answer: B

Correct answer: FALSE

An enumerated value is an integer, not a string, so passing one to strcmp or strcpy would pass a small integer where a char * is expected - a type error, and undefined behaviour if it compiled. The names exist only in the source; nothing of them survives into the program.

Please log in or register to report this question.

80.

Can we have enum containing enum with the same constant?

#include "stdio.h"

enum Yenum{
       C, CPP, Java
};

enum Xenum{
    C, CPP, Java, Yenum
};

int main()
{
  enum Xenum var;
  printf("%d",sizeof(var));
  return 0;
}

 

Answer: A

Correct answer: Enum can contain enum but duplicate constants are not allowed

The two enumerations both declare C, CPP and Java, and enumerators occupy the ordinary identifier namespace at file scope - so the second set are redefinitions and the program will not compile. Naming one enumeration as a member of another is not the difficulty; the repeated constant names are.

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