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

Structures, Unions, Enums Questions and Answers

51.

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.

52.

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.

Answer: B

When a variable is associated with a structure, the compiler allocates the memory for each member. The size of structure is greater than or equal to the sum of sizes of its members. The smaller members may end with unused slack bytes. While in case of Union when a variable is associated with a union, the compiler allocates the memory by considering the size of the largest memory. So, size of union is equal to the size of largest member.

Please log in or register to report this question.

54.

#include <stdio.h>

typedef enum{
    Male=-1, Female
}SEX;

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

}

 

Answer: A

Correct answer: -1 0

Male is set to -1 - negative values are perfectly legal - and Female continues from it at 0.

Please log in or register to report this question.

55.

What is the output of this program?

#include <stdio.h>
struct test {
    int x;
    char y;
} test;

int main()
{
    test.x = 10;
    test.y = 'A';
    printf("%d %c", test.x,test.y);
    return 0;
}

 

Answer: A

Correct answer: 10 A

The declaration defines the structure type and a variable, both called test - legal, because tags and ordinary identifiers occupy separate namespaces. The assignments then set the members and the printf prints 10 A.

Please log in or register to report this question.

Answer: C

Correct answer: No compile error and two elements of arr[] would be defined and initialized. Output would be “1 0 1 and 2 0 2”.

The initialiser uses designated initialisers with array indices: [0].a = {1}, [1].a = {2} and so on. This is valid C99, and it sizes arr at two elements, since index 1 is the highest named. Everything not explicitly initialised is zero-filled, so a[1] is 0 in both - giving 1 0 1 and 2 0 2.

Please log in or register to report this question.

Answer: C

Correct answer: Enumeration can have an effect local to the block, if desired

An enum declared inside a block is visible only within it, exactly like any other declaration - so its effect can be local. The other statements are false: the compiler assigns values automatically from 0 unless you override them, the size of an enum variable is the implementation’s choice, and an enum is not necessarily file-wide.

Please log in or register to report this question.

Answer: D

Correct answer: No compile error and it’ll print 100 A.

Defining a structure type inline with the variable is legal, and designated initialisers may be given in any order, since each names its target. So .c = 'A', .i = 100 is fine and prints 100 A.

Please log in or register to report this question.

59.

Which properly declares a variable of struct foo?

Answer: B

Correct answer: struct foo var;

In C the type is struct foo, two words - the keyword cannot be dropped unless a typedef has provided a single-word name. struct foo; alone merely declares that the tag exists.

Please log in or register to report this question.

60.

Point out the error (if any) in the following C code?

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

 

Answer: B

Correct answer: Error in the statement: a,b,c;

The enumerator list must not end with a semicolon inside the braces - a, b, c is a comma-separated list, and the only semicolon belongs after the closing brace. Writing a,b,c; is a syntax error.

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