The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

C Structures Questions

In most of the MNC interview questions such as in ZOHO interview question, IVTL Infoview interview questions, Amazon interview questions, GOOGLE interview questions, Infosys interview questions and even in Voonik interview questions, We come across several Tricky C Questions about which 2:5 of the questions are from Structure in c. Solving that kind of tricky C questions is not an easy task for all C programmers. We need more practices to solve it with ease. So we provide 25+ interesting C questions in structure to make your MNC interview very easy.

C Structures Questions and Answers

1. What will be the output of the C program?

#include<stdio.h>
int main()
{
	struct simp 
	{
		int i = 6;
		char city[] = "chennai";
	};
	struct simp s1;
	printf("%d",s1.city);
	printf("%d", s1.i);
	return 0;
}

A. chennai 6

B. Nothing will be displayed

C. Runtime Error

D. Compilation Error

x

 

Option: D

Explanation

Compilation Error: Cannot initialize a class member here
When we declared members in struture, it just tells the compiler about their presence. There is no memory allocated for that members. So we can't intialize structure members.

Answer


2.What will be the output of the C program?

#include<stdio.h>
struct 
{
	int i;
	float ft;
}decl;
int main() 
{
	decl.i = 4;
	decl.ft = 7.96623;
	printf("%d %.2f", decl.i, decl.ft);
	return 0;
}

A. 4 7.97

B. 4 7.96623

C. Compilation error

D. None of the above

x

 

Option: A

Explanation

In the above program the float variable ft is intialised to 7.96623, and it rounded of to 7.97 because of %.2f that we have mentioned in printf statement.

Answer


3.What will be the output of the C program?

#include<stdio.h>
int main() 
{ 
	struct bitfields
	{
		int bits_1: 2;
		int bits_2: 4;
		int bits_3: 4;
		int bits_4: 3;
	}bit = {2, 3, 8, 7};
	printf("%d %d %d %d", bit.bits_1, bits.bit_2, bit.bits_3, bits.bit_4);
}

A. Runtime error

B. Compilation error

C. 2 3 8 7

D. -2 3 -8 -1

x

 

Option: D

Explanation

The above structure declaration shows that we declare four variables as int(whenever you declare int you get a signed int).
Signed integers needs the left most bit for +/- sign.
int bits_1: 2; // assigns two bits to the variable bits_1
int bits_2: 4; //assigns four bits to the variable bits_2
int bits_3: 4; //assigns four bits to the variable bits_3
int bits_4: 1; //assigns one bit to the variable bits_4

2 bits - 00
4 bits - 0000
1 bit - 0

we assign values 2, 3, 8, 1 for the variables bits_1, bits_2, bits_3 and bits_4 respectively.

when we assign 2 in 2 bit field:
value bit
2 - 10

here the left most bit is 1. so, the compiler treat the value as negative.
To handle negative values the system will use the 2's complement method.

1's complement of 2(10) - 01
2's complement of 2(10) - 10

Finally the value is -2.

when we assign 3 in 4 bit field:

value bit
3 - 0011

here the left most bit is 0. so, the compiler treat the value as positive.

There is no need of 2's complement. The compiler will displays 3.

when we assign 8 in 4-bit field:
value bit
8 - 1000

here the left most bit is 1. so, the compiler treat the value as negative.
To handle negative values the system will use the 2's complement method.

1's complement of 8(1000) - 0111
2's complement of 8(1000) - 1000

Finally the value is -8.

when we assign 7 in 3-bit field:
value bit
7 - 111

here the left most bit is 1. so, the compiler treat the value as negative.
To handle negative values the system will use the 2's complement method.

1's complement of 7(111) - 000
2's complement of 7(111) - 001

Finally the value is -1.

Answer


4.What will be the output of the C program?

 void main()
  {
  struct bitfields {
  int bits_1: 2;
  int bits_2: 9;
  int bits_3: 6;
  int bits_4: 1;
  }bit;
  printf("%d", sizeof(bit));
  }

A. 2

B. 3

C. 4

D. 0

x

 

Option: B

Explanation

1 byte = 8 bits
In the above program we assign 2, 9, 6, 1 for the variables. Sum of the bits assigned is, 2 + 9 + 6 + 1 = 18, It is greater than 2 bytes, so it automatically takes 3 bytes.

Answer


5.What will be the output of the C program?

#include<stdio.h>
int main()
{
	struct leader
	{
	char *lead;
	int born;
	};
	struct leader l1 = {"AbdulKalam", 1931};
	struct leader l2 = l1;
	printf("%s %d", l2.lead, l1.born);
}

A. Compilation error

B. Garbage value 1931

C. AbdulKalam 1931

D. None of the above

x

 

Option: C

Explanation

We can assign one structure member to the another structure member as like normal variable assignmentation. We can access the same values using different structure variables.

Answer


Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report