Pointers - C Questions

Pointers Questions - 1 Pointers Questions - 2 Pointers Questions - 3 Pointers Questions - 4 Pointers Questions - 5 Pointers Questions - 6 Pointers Questions - 7 Pointers Questions - 8

Data Types - C Questions

Data Types Questions - 1 Data Types Questions - 2 Data Types Questions - 3 Data Types Questions - 4 Data Types Questions - 5

Operators - C Questions

Operators Questions - 1 Operators Questions - 2 Operators Questions - 3 Operators Questions - 4 Operators Questions - 5

Structures - C Questions

Structures Questions - 1 Structures Questions - 2 Structures Questions - 3 Structures Questions - 4 Structures Questions - 5

Files - C Questions

Files Questions - 1 Files Questions - 2 Files Questions - 3 Files Questions - 4 Files Questions - 5

printf - C Questions

printf Questions - 1 printf Questions - 2 printf Questions - 3 printf Questions - 4 printf Questions - 5

Variables - C Questions

Variables Questions - 1 Variables Questions - 2 Variables Questions - 3 Variables Questions - 4 Variables Questions - 5

Preprocessor Macros

Preprocessor Macros - 1 Preprocessor Macros - 2 Preprocessor Macros - 3 Preprocessor Macros - 4 Preprocessor Macros - 5

While Loop - C Questions

While Loop Questions - 1 While Loop Questions - 2 While Loop Questions - 3 While Loop Questions - 4 While Loop Questions - 5

for Loop - C Questions

for Loop Questions - 1 for Loop Questions - 2 for Loop Questions - 3 for Loop Questions - 4 for Loop Questions - 5

if else - C Questions

if else Questions - 1 if else Questions - 2 if else Questions - 3 if else Questions - 4 if else Questions - 5

Switch case - C Questions

Switch case Questions - 1 Switch case Questions - 2 Switch case Questions - 3 Switch case Questions - 4 Switch case Questions - 5

Array - C Questions

Array Questions - 1 Array Questions - 2 Array Questions - 3 Array Questions - 4 Array Questions - 5

Functions - C Questions

Functions Questions - 1 Functions Questions - 2 Functions Questions - 3 Functions Questions - 4 Functions Questions - 5

Memory Allocation

Memory Allocation - 1 Memory Allocation - 2
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

We to update you