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

Structures, Unions, Enums Questions and Answers

61.

What will be the output of the following C code?

#include <stdio.h>
enum class
{
    a,b,c
};
enum class m;
main()
{
    printf("%d",sizeof(m));
}

 

Answer: B

Correct answer: Same as the size of an integer

An enumeration variable has the size of an int - 4 bytes on the usual platforms - because the compiler stores the constant itself, not a list of them. How many enumerators the type has makes no difference.

Please log in or register to report this question.

62.

The size of the following union, where an int occupies 4 bytes of memory is

union demo
{
  float x;
  int y;
  char z[10];
};

 

Answer: C

Correct answer: 10 byte

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. Here the largest member is char z[10], so the union is 10 bytes - not the 18 the members would occupy in a structure.

Please log in or register to report this question.

63.

Anyone of the following can be used to declare a node for a singly linked list and “NODEPTR nodePtr;” can be used to declare pointer to a node using any of the following

/* First declaration */
typedef struct node
{
 int data;
 struct node *nextPtr;
}* NODEPTR;
 
/* Second declaration */
struct node
{
 int data;
 struct node * nextPtr;
};
typedef struct node * NODEPTR;

 

Answer: A

Correct answer: TRUE

Both work. In the first, the member is declared as struct node *nextPtr - the tag is available inside its own definition, even though the typedef name is not - and NODEPTR is defined by the same declaration. In the second, the structure is completed first and the typedef written afterwards. Either way NODEPTR nodePtr; declares a pointer to a node.

Please log in or register to report this question.

64.

A user defined data type, which is used to assign names to integral constants is called ____________

Answer: D

Correct answer: Enum

An enumeration gives names to a set of integer constants: enum colour {red, green, blue}; makes red 0, green 1 and blue 2. It exists for readability - the compiler still stores an integer, but the source says what the value means.

Please log in or register to report this question.

Answer: B

Correct answer: All of them let you define new data types

Structures, unions and enumerations are all ways of defining a new type - a structure aggregating members, a union overlaying them, an enumeration naming a set of integer constants. Once defined, each can be used to declare variables like any built-in type.

Please log in or register to report this question.

66.

Which of the following is an incorrect syntax to pass by reference a member of a structure in a function?

(Assume: struct temp{int a;}s;)

 

Answer: D

Correct answer: None of the above

All three are valid. &s.a works because . binds tighter than &; &(s.a) makes that grouping explicit; and &(s).a is the same again, since the parentheses round s change nothing. So none of the forms is incorrect.

Please log in or register to report this question.

67.

In the following program snippet, both s1 and s2 would be variables of structure type defined as below and there won't be any compilation issue.

typedef struct Student
{
 int rollno;
 int total;
} Student;
 
Student s1;
struct Student s2;

 

Answer: A

Correct answer: TRUE

Both spellings are valid. The typedef creates the name Student, usable on its own; and because the structure also has the tag Student, struct Student is valid too. Tags and ordinary identifiers live in separate namespaces in C, so the same word can serve as both without conflict.

Please log in or register to report this question.

68.

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.

69.

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.

70.

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.

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