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

Structures, Unions, Enums Questions and Answers

Answer: D

Correct answer: struct temp s; struct temp{}; main(){}

This one is an error. Declaring struct temp s; before the structure has been defined means declaring an object of an incomplete type - the compiler does not yet know how much storage to reserve. The other two are fine: the first defines the type and a variable together, and the second defines the type before using it. (A pointer to an incomplete type would be legal, since a pointer’s size is known.)

Please log in or register to report this question.

Answer: A

Correct answer: An array, each element of which is a pointer to a structure of type node

Subscript binds tighter than the dereference, so struct node *s[10] groups as struct node *(s[10]) - an array of 10 pointers, each to a struct node. A pointer to an array would need parentheses in the other place: struct node (*s)[10].

Please log in or register to report this question.

23.

Anyone of the followings can be used to declare a node for a singly linked list. If we use the first declaration, “struct node * nodePtr;” would be used to declare pointer to a node. If we use the second declaration, “NODEPTR nodePtr;” can be used to declare pointer to a node.

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

 

Answer: B

Correct answer: FALSE

The second declaration is broken: NODEPTR is used inside the structure to declare nextPtr, but the typedef that defines NODEPTR is not complete until the closing brace. A name cannot be used before it exists, so the compiler rejects it. The self-reference has to be written as struct node *nextPtr;.

Please log in or register to report this question.

24.

What is the output of this program?

void main()
  {
  struct bitfields {
  int bits_1: 2;
  int bits_2: 9;
  int bits_3: 6;
  int bits_4: 1;
  }bit;
  printf("%d", sizeof(bit));
}

 

Answer: B

Correct answer: 3

The bit-fields total \(2 + 9 + 6 + 1 = 18\) bits. Rounded up to whole bytes that is 3 under the packing this question assumes. (In practice most compilers align bit-fields to their underlying type and would report 4, which is why bit-field layout is implementation-defined and not portable.)

Please log in or register to report this question.

Answer: B

Correct answer: unique in their scope

Enumerator names must be unique within their scope, because they share the ordinary identifier namespace - two enumerations at file scope cannot both declare a constant called c. Their values, by contrast, may repeat freely.

Please log in or register to report this question.

26.

Predict the output of the program below. Assume that the size of an integer is 4 bytes and size of character is 1 byte. Also assume that there is no alignment needed.

union test
{
    int x;
    char arr[8];
    int y;
};
 
int main()
{
    printf("%d", sizeof(union test));
    return 0;
}

 

Answer: C

Correct answer: 8

A union is as large as its largest member. Here int x and int y are 4 bytes each, and char arr[8] is 8 - so the union is 8 bytes. That the two ints are separate members makes no difference: all three overlay the same storage.

Please log in or register to report this question.

27.

What is the output of this program?

#include <stdio.h>
int main(){
    struct leader
 {
 char *lead;
 int born;
 };
 struct leader l1 = {"AbdulKalam", 1931};
 struct leader l2 = l1;
 printf("%s %d", l2.lead, l1.born);
}

 

Answer: C

Correct answer: AbdulKalam 1931

l2 = l1 copies the structure member by member - the born value and the lead pointer. That is a shallow copy: both structures now point at the same string literal. Here it does no harm, since neither modifies it.

Please log in or register to report this question.

28.

What is the output of this program?

#include <stdio.h>
struct employee
{
  char *empname;
  int salary;
};
int main()
{
  struct employee e, e1;
  e.empname = "Sridhar";
  e1 = e;
  printf("%s %s", e.empname, e1.empname);
  return 0;
}

 

Answer: C

Correct answer: Sridhar Sridhar

Assigning one structure to another copies its members, and for a pointer member that means copying the address, not the text. So e and e1 both point at the same string literal and both print Sridhar. Copying the text would require strcpy into separately allocated storage.

Please log in or register to report this question.

Answer: D

Correct answer: struct a_struct {int a;};

A structure definition is a declaration and must end with a semicolon - which is what the others omit. (The first form, without a tag, is legal if a variable is declared with it, but as written it declares nothing and is useless.)

Please log in or register to report this question.

Answer: A

Correct answer: enum flag{constant1, constant2, constant3, ....... };

The syntax is the keyword enum, an optional tag, the constants in braces separated by commas, and a closing semicolon. Parentheses and square brackets are not used, and the keyword must be separated from the tag.

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