C Technical Interview Questions on Structures and Unions

These C Technical Interview Questions On Structures and Union post providing basic, intermediate and Hard level examples in C Structures and Unions. The Practice of these examples makes you good in C programming.


What is the Output of following Code?
#include<stdio.h>
void fun();
void main()
{
struct st
{
int a, b;
};
}
void fun()
{
struct st var ;
}

  1. Blank output
  2. Function prototype is missing
  3. Local variable cannot be accessed from outside function
  4. None of the above

Answer :

Ans : 3 

What is the Output of following Code?
#include<stdio.h>
struct st
{
short a, b;
};
void main()
{
printf(“%d\n”, sizeof(struct st));
}

  1. 4 bytes
  2. 8 bytes
  3. compile time error
  4. Depends on compiler

Answer :

Ans : 1 

What is the Output of following Code?
#include<stdio.h>
union un
{
int a, b;
};
void main()
{
union un var ;
var.a=10;
var.b=20;
printf(“a val : %d\n”, var.a);
}

  1. 10
  2. 20
  3. 30
  4. None of the above

Answer :

Ans : 2 , we are modifying the value of a by using b 

What is the Output of following Code?
#include <stdio.h>
int main()
{
struct sample
{
int a ;
int b ;
struct sample *s;
}t;
printf(“%d,%d”,sizeof(struct sample),sizeof(t.s));
return 0;
}

  1. 6, 2
  2. 12, 4
  3. 8 , 4
  4. 10 , 2

Answer :

Ans : 1 , integer size and pointer size is 2 bytes only 

What is the Output of following Code?
#include<stdio.h>
union un
{
short s;
char c[2];
};
void main()
{
union un var ;
var.c[0]=2;
var.c[1]=3;
printf(“s val : %d\n”, var.s);
}

  1. 512
  2. GarbageValue
  3. 23
  4. 770

Answer :

Ans : 4 

What is the Output of following Code?
#include <stdio.h>
int main()
{
struct sample
{
sample *s;
}t;
printf(“%d”,sizeof(t.s));
return 0;
}

  1. 4
  2. Compile time error
  3. 2
  4. None of the above

Answer :

Ans : 2, No data type specified for structure element 

What is the Output of following Code?
#include <stdio.h>
struct sample
{
int a;
}sample;
int main()
{
sample.a = 100;
printf(“%d”,sample.a);
return 0;
}

  1. 100
  2. Error : Cannot assign value directly
  3. Garbage value
  4. Error : Improper structure definition

Answer :

Ans : 1 

What is the Output of following Code?
#include <stdio.h>
struct student
{
char *name = “Kowshik” ;
};
void main()
{
struct student m ;
printf(“%c”, m.name);
}

  1. Kowshik
  2. Error : cannot assign values to structure variables directly
  3. K
  4. Garbage character

Answer :

Ans : 2 , We cannot assign values to structure elements directly, because memory will not be allocated by just defining structure 

What is the Output of following Code?
#include<stdio.h>
union un
{
int a, b;
};
void main()
{
printf(“%d\n”, sizeof(union un));
}

  1. 2 bytes
  2. 4 bytes
  3. 8 bytes
  4. None of the above

Answer :

Ans : 1 

What is output of following program?
#include <stdio.h>
struct student
{
char *name;
};
struct student s;
void main()
{
printf(“%s”, s.name);
}

  1. Blank output
  2. Garbage value
  3. Set of Garbage characters
  4. null

Answer :

Ans : 4 , char* default value is null-pointer

What is output of following program?
#include <stdio.h>
struct student
{
char *name;
};
struct student fun(void)
{
struct student s;
s.name = “Karthik”;
return s;
}
void main()
{
struct student m = fun();
printf(“%c”, m.name);
}

  1. K
  2. Garbage character
  3. Karthik
  4. None of the above

Answer :

Ans : 2 , It prints the address corresponding character that is unknown 

Point out the error in the program?
struct Student
{
int data;
struct Student *s;
}

  1. Error : we cannot declare same structure variable inside structure
  2. Error : semi-colon missing
  3. Error : Pointer variables declarations not allowed inside structure
  4. No Error

Answer :

Ans : 2 , Every structure definition must ends with semi-colon 

What is output of following program?
#include <stdio.h>
int main()
{
union test
{
int i;
int j;
};
union test var = {10 , 20};
printf(“%d,%d\n”,var.i,var.j);
return 0;
}

  1. 10,20
  2. 10,10
  3. 20,20
  4. Error : We cannot initialize directly

Answer :

Ans : 2, In the memory only one value will be stored that is first element always 

What is output of following program?
#include <stdio.h>
void main()
{
struct emp
{
char name[20];
int age;
float sal;
};
struct emp e = {“Kowshik”} ;
printf(“%d,%f”,e.age,e.sal);
}

  1. Compile time error
  2. 0, 0.000000
  3. Garbage value, Garbage value
  4. None of the above

Answer :

Ans : 2 

Structures and Unions – Objective – Part 1

Share this