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

Structures, Unions, Enums Questions and Answers

41.

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.

43.

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.

44.

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.

45.

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.

46.

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.

47.

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.

48.

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.

49.

Pick the best statement for the below program:

#include "stdio.h"
 
int main()
{
 union {int i1; int i2;} myVar = {.i2 =100};
 printf("%d %d",myVar.i1, myVar.i2);
 return 0;
}

 

Answer: C

Correct answer: No compile error and it’ll print “100 100”

The two members of a union share the same storage, so writing 100 through i2 makes it visible through i1 as well - and since both are int, the bytes are interpreted the same way. The output is 100 100. (Reading a union through a member other than the one last written is only well defined when the types match like this.)

Please log in or register to report this question.

50.

For the following function call which option is not possible? func(&s.a); //where s is a variable of type struct and a is the member of the struct.

Answer: A

Correct answer: Compiler can access entire structure from the function

func(&s.a) passes the address of one member only. The function receives a pointer to that member and has no way to reach the rest of the structure - it does not even know what structure the member belongs to. To give it the whole thing you would pass &s.

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