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

Structures, Unions, Enums Questions and Answers

41.

#include <stdio.h> 
struct st 
{ 
    int x; 
    struct st next; 
}; 
   
int main() 
{ 
    struct st temp; 
    temp.x = 10; 
    temp.next = temp; 
    printf("%d", temp.next.x); 
    return 0; 
}

 

Answer: A

Correct answer: Compiler Error

A structure cannot contain an instance of itself - the type would be infinitely large, since each copy would contain another. The compiler rejects struct st next;. What is allowed is a pointer to the same type, struct st *next;, since a pointer has a fixed size; that is how linked lists are built.

Please log in or register to report this question.

42.

Find the output of below C program

#include "stdio.h"

typedef enum{
    Male, Female
}SEX;

int main()
{
  SEX var = Male;
  printf("%d",var);
  return 0;

}

 

Answer: D

Correct answer: 0

Male is the first enumerator with no explicit value, so it is 0, and %d prints the number. The name exists only in the source: nothing in the compiled program remembers that 0 was called Male.

Please log in or register to report this question.

43.

What will be the output of the following C code if the code is executed on a 32 bit platform?

#include <stdio.h>
enum sanfoundry
 {
    c = 0,
    d = 10,
    h = 20,
    s = 3
} a;
 
int main()
{
        a = c;
 printf("Size of enum variable = %d bytes", sizeof(a));
 return 0;
}

 

Answer: C

Correct answer: Size of enum variable = 4 bytes

An enum variable is stored as an int, which is 4 bytes on a 32-bit platform. The values of the enumerators do not affect it - the variable holds one of them, not all.

Please log in or register to report this question.

Answer: D

Correct answer: No compile error and it’ll print 1 0 2 0

The structure type is defined inline, which is legal, and arr[] takes its length from the initialiser - two elements. Each inner brace initialises one structure, and within it {1} sets a[0] to 1 while the remaining members are zero-filled, as always in a partly initialised aggregate. So the output is 1 0 2 0.

Please log in or register to report this question.

45.

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[4];
    int y;
};
 
int main()
{
    union test t;
    t.x = 0;
    t.arr[1] = 'G';
    printf("%s", t.arr);
    return 0;
}

 

Answer: A

Correct answer: Nothing is printed

t.x = 0 writes zeros over all four bytes, so arr[0] becomes '\0'. Setting arr[1] to 'G' changes the second byte only, leaving the first still null. %s stops at the first null, so it prints nothing at all - the 'G' is there in memory but never reached.

Please log in or register to report this question.

46.

Which of the following cannot be a structure member?

Answer: B

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.

47.

What is the output of this program?

#include <stdio.h>
struct test {
    int x = 0;
    char y = 'A';
};
int main()
{
    struct test t;
    printf("%d, %c", s.x, s.y);
    return 0;
}

 

Answer: A

Correct answer: Error

Two faults, either fatal. C structures cannot have member initialisers - the declaration describes a type, not an object - and the printf refers to s.x and s.y when the variable declared is t.

Please log in or register to report this question.

48.

Enumeration (enum) is a ______ data type in C.

Answer: A

Correct answer: user defined

An enumeration is a user-defined type: the programmer names the type and lists its constants, and the compiler then treats it as a type in its own right - even though its representation is an int.

Please log in or register to report this question.

49.

Which of the following share a similarity in syntax? 1. Union 2. Structure 3. Arrays 4. Pointers

Answer: B

Correct answer: 1 and 2

Unions and structures are declared with identical syntax - keyword, optional tag, a braced list of members - and their members are accessed the same way, with . and ->. Only the storage differs: a structure lays its members out separately, a union overlays them.

Please log in or register to report this question.

50.

What will be the output of the following C code?

#include <stdio.h>
enum hi{a,b,c};
enum hello{c,d,e};
main()
{
    enum hi h;
    h=b;
    printf("%d",h);
    return 0;
}

 

Answer: C

Correct answer: Error

Both enumerations declare an enumerator called c, and enumerators share the ordinary identifier namespace at file scope - so the second declaration is a redefinition and the compiler rejects it. Unlike structure tags, enumerators are not scoped to their own type in C. (C++'s enum class was introduced to fix exactly this.)

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