C Structure & Union - Aptitude Questions & Answers

C programming Structure and Union Aptitude Questions and Answers: In this section you will find C Aptitude Questions and Answers on Structure and Union Questions.

List of C programming Structure and Union Aptitude Questions and Answers

1) What will be the output of following program ?
#include <stdio.h>
struct sample
{
	int a=0;
	char b='A';
	float c=10.5;
};
int main()
{
	struct sample s;
	printf("%d,%c,%f",s.a,s.b,s.c);
	return 0;
}
 
  1. Error
  2. 0,A,10.5
  3. 0,A,10.500000
  4. No Error , No Output

2) What will be the output of following program ?
#include <stdio.h>
int main()
{
	struct sample{
		int a;
		int b;
		sample *s;
	}t;

	printf("%d,%d",sizeof(sample),sizeof(t.s));
	return 0;
} 
  1. 12,12
  2. 12,0
  3. Error
  4. 12,4

3) What will be the output of following program ?
#include <stdio.h>
#include < string.h >

struct student
{
	char name[20];
}std;
char * fun(struct student *tempStd)
{
	strcpy(tempStd->name,"Thomas");
	return tempStd->name;
}

int main()
{
	strcpy(std.name,"Mike ");
	printf("%s%s",std.name,fun(&std));
	return 0;
}
  1. Mike Thomas
  2. Mike Mike
  3. ThomasThomas
  4. ThomasMike

4) What will be the output of following program ?
#include <stdio.h>

struct sample
{
	int a;
}sample;

int main()
{
	sample.a=100;
	printf("%d",sample.a);
	return 0;
}
  1. 0
  2. 100
  3. ERROR
  4. Warning

5) What will be the output of following program ?
#include <stdio.h>
struct employee{
	int empId;
	char *name;
	int age;
};
int main()
{
	struct employee emp []={ {1,"Mike",24}, {2,"AAA",24}, {3,"BBB",25}, {4,"CCC",30} };

	printf("Id : %d, Age : %d, Name : %s", emp[2].empId,3[emp].age,(*(emp+1)).name);
	return 0;
}
  1. Id : 3, Age : 24, Name : Mike
  2. Id : 3, Age : 23, Name : Mike
  3. Id : 3, Age : 30, Name : AAA
  4. ERROR

6) What will be the output of following program ? (On 16 bit compiler)
#include <stdio.h>
int main()
{
	union values
	{
		int  intVal;
		char chrVal[2];i
	};

	union values val;
	val.chrVal[0]='A'; val.chrVal[1]='B';

	printf("\n%c,%c,%d",val.chrVal[0],val.chrVal[1],val.intVal);
	return 0;
}
  1. A,B,0
  2. A,B,16961
  3. B,B,66
  4. A,A,65

7) What will be the output of following program ? (On 16 bit compiler)
#include <stdio.h>
int main()
{
	union values
	{
		unsigned char a;
		unsigned char b;
		unsigned int  c;
	};

	union values val;
	val.a=1;
	val.b=2;
	val.c=300;

	printf("%d,%d,%d",val.a,val.b,val.c);
	return 0;
}
  1. 44,44,300
  2. 1,2,300
  3. 2,2,300
  4. 256,256,300

8) What will be the output of following program ? (On 16 bit compiler)
#include <stdio.h>
int main()
{
	typedef struct tag{
		char str[10];
		int a;
	}har;

	har h1,h2={"IHelp",10};
	h1=h2;
	h1.str[1]='h';
	printf("%s,%d",h1.str,h1.a);
	return 0;
}
  1. ERROR
  2. IHelp,10
  3. IHelp,0
  4. Ihelp,10

9) What will be the output of following program ?
#include <stdio.h>
int main()
{
	struct std
	{
		char name[30];
		int age;
	};
	struct std s1={"Mike",26};
	struct std s2=s1;
	
	printf("Name: %s, Age: %d\n",s2.name,s2.age);
}
  1. Name: Mike, Age: 26
  2. Name: Garbage, Age: Garbage
  3. Error

10) What will be the output of following program ?
#include <stdio.h>
int main()
{
	union test
	{
		int i;
		int j;
	};
	
	union test var=10;
	printf("%d,%d\n",var.i,var.j);
}
  1. 10,10
  2. 10,0
  3. 0,10
  4. Error