• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests

C Structure & Union

Question 11

Pick the best statement for the below program: C
#include \"stdio.h\"

int main()
{
 struct {int a[2];} arr[] = {{1},{2}};

 printf(\"%d %d %d %d\",arr[0].a[0],arr[0].a[1],arr[1].a[0],arr[1].a[1]);

 return 0;
}
  • Compile error because arr has been defined using struct type incorrectly. First struct type should be defined using tag and then arr should be defined using that tag.
  • Compile error because apart from definition of arr, another issue is in the initialization of array of struct i.e. arr[].
  • Compile error because of initialization of array of struct i.e. arr[].
  • No compile error and it’ll print 1 2 0 0
  • No compile error and it’ll print 1 0 2 0

Question 12

Pick the best statement for the below program: C
#include \"stdio.h\"

int main()
{
 struct {int a[2], b;} arr[] = {[0].a = {1}, [1].a = {2}, [0].b = 1, [1].b = 2};

 printf(\"%d %d %d and\",arr[0].a[0],arr[0].a[1],arr[0].b);
 printf(\"%d %d %d\\n\",arr[1].a[0],arr[1].a[1],arr[1].b);

 return 0;
}
  • Compile error because struct type (containing two fields i.e. an array of int and an int) has been specified along with the definition of array arr[] of this struct type.
  • Compile error because of incorrect syntax for initialization of array arr[].
  • No compile error and two elements of arr[] would be defined and initialized. Output would be “1 0 1 and 2 0 2”.
  • No compile error and two elements of arr[] would be defined and initialized. Output would be “1 X 1 and 2 X 2” where X is some garbage random number.

Question 13

Pick the best statement for the below program: 

C
#include <stdio.h>

int main()
{
    struct {
        int i;
        char c;
    } myVar = {.i = 100, .c = 'A'};
    
    printf("%d %c", myVar.i, myVar.c);
    
    return 0;
}
  • Compile error because struct type (containing two fields of dissimilar type i.e. an int and a char) has been mentioned along with definition of myVar of that struct type.

  • Compile error because of incorrect syntax of initialization of myVar. Basically, member of operator (i.e. dot .) has been used without myVar.

  • Compile error for not only B but for incorrect order of fields in myVar i.e. field c has been initialized first and then field i has been initialized.

  • No compile error and it’ll print 100 A.

Question 14

Pick the best statement for the below program: 

C
#include <stdio.h>

int main()
{
    union {
        int i1;
        int i2;
    } myVar = {.i2 = 100};
    
    printf("%d %d", myVar.i1, myVar.i2);
    
    return 0;
}
  • Compile error due to incorrect syntax of initialization.

  • No compile error and it’ll print “0 100”.

  • No compile error and it’ll print “100 100”.

Question 15

Consider the following declaration :

struct addr {
     char city[10];
     char street[30];
     int pin ;
};

struct {
char name[30];
int gender;
struct addr locate;
} person , *kd = &person ;

Then *(kd -> name +2) can be used instead of

  • person.name +2

  • kd -> (name +2 )

  • *((*kd).name + 2 )

  • either (A) or (B), but not (C)

Question 16

Consider the following ANSI C program: 
 

int main () {
    Integer x;
    return 0;
}


Which one of the following phases in a seven-phase C compiler will throw an error?
 

  • Lexical analyzer
     

  • Syntax analyzer
     

  • Semantic analyzer
     

  • Machine dependent optimizer
     

Question 17

Consider the following C declaration 
 

struct (
short s[5];
union {
float y;
long z;
}u;
}t;


Assume that the 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 consideration, is
 

  • 22 bytes
     

  • 18 bytes
     

  • 14 bytes
     

  • 10 bytes
     

Question 18

Consider the following C declaration 
 

C
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 (GATE CS 2000)
 

  • 22 bytes
     

  • 14 bytes
     

  • 18 bytes
     

  • 10 bytes
     

Question 19

Which of the following operators can be applied on structure variables?
 

  • Equality comparison ( == )
     

  • Assignment ( = )
     

  • Both of the above
     

  • None of the above
     

Question 20

Pick the best statement for the below program snippet: 
 

C
struct {int a[2];} arr[] = {1,2};
  • No compile error and it’ll create array arr of 2 elements. Each of the element of arr contain a struct field of int array of 2 elements. arr[0]. a[0] would be 1 and arr[1].a[0] would be 2.
     

  • No compile error and it’ll create array arr of 2 elements. Each of the element of arr contain a struct field of int array of 2 elements. arr[0]. a[0] would be 1 and arr[0].a[1] would be 2. The second element arr[1] would be ZERO i.e. arr[1].a[0] and arr[1].a[1] would be 0.
     

  • No compile error and it’ll create array arr of 1 element. Each of the element of arr contain a struct field of int array of 2 elements. arr[0]. a[0] would be 1 and arr[0].a[1] would be 2.
     

  • None of the above

There are 22 questions to complete.

Last Updated :
Take a part in the ongoing discussion