*************************************************************************************************
* NOTE : All the programs are tested under Turbo C/C++ compilers. *
* It is assumed that, *
* *=> Programs run under DOS environment, *
* *=> The underlying machine is an x86 system, *
* *=> Program is compiled using Turbo C/C++ compiler. *
* *=> Necessary header files are included. *
* The program output may depend on the information based on this assumptions. *
* (For example sizeof(int) = 2 bytes may be assumed). *
*************************************************************************************************
[Q001]. What will be the output of the following program :
struct {
int i;
float f;
}var;
void main()
{
var.i=5;
var.f=9.76723;
printf(“%d %.2f”,var.i,var.f);
}
(a)Compile-Time Error (b)5 9.76723 (c)5 9.76 (d)5 9.77
Ans. (d) Though both and are optional, one of the two
must appear. In the above program, i.e. var is used. (2 decimal places or)
2-digit precision of 9.76723 is 9.77
_________________________________________________________________________________________________
[Q002]. What will be the output of the following program :
struct {
int i;
float f;
};
void main()
{
int i=5;
float f=9.76723;
printf(“%d %.2f”,i,f);
}
(a)Compile-Time Error (b)5 9.76723 (c)5 9.76 (d)5 9.77
Ans. (d) Both and are optional. Thus the structure
defined in the above program has no use and program executes in the normal way.
_________________________________________________________________________________________________
[Q003]. What will be the output of the following program :
struct values{
int i;
float f;
};
void main()
{
struct values var={555,67.05501};
printf(“%2d %.2f”,var.i,var.f);
}
(a)Compile-Time Error (b)55 67.05 (c)555 67.06 (d)555 67.05
Ans. (c) The members of a structure variable can be assigned initial values in much the same
manner as the elements of an array. The initial values must appear in order in which they will be
assigned to their corresponding strucutre members, enclosed in braces and separated by commas.
_________________________________________________________________________________________________
[Q004]. What will be the output of the following program :
typedef struct {
int i;
float f;
}values;
void main()
{
static values var={555,67.05501};
printf(“%2d %.2f”,var.i,var.f);
}
(a)Compile-Time Error (b)55 67.05 (c)555 67.06 (d)555 67.05
Ans. (c) In the above program, values is the user-defined structure type or the new user-defined
data type. Structure variables can then be defined in terms of the new data type.
_________________________________________________________________________________________________
[Q005]. What will be the output of the following program :
struct my_struct{
int i=7;
float f=999.99;
}var;
void main()
{
var.i=5;
printf(“%d %.2f”,var.i,var.f);
}
(a)Compile-Time Error (b)7 999.99 (c)5 999.99 (d)None of these
Ans. (a) C language does not permit the initialization of individual structure members within the
template. The initialization must be done only in the declaration of the actual variables. The
correct way to initialize the values is shown in [Q003] or [Q004].
_________________________________________________________________________________________________
[Q006]. What will be the output of the following program :
struct first{
int a;
float b;
}s1={32760,12345.12345};
typedef struct{
char a;
int b;
}second;
struct my_struct{
float a;
unsigned int b;
};
typedef struct my_struct third;
void main()
{
static second s2={‘A’,- -4};
third s3;
s3.a=~(s1.a-32760);
s3.b=-++s2.b;
printf(“%d %.2f\n%c %d\n%.2f %u”,(s1.a)–,s1.b+0.005,s2.a+32,s2.b,++(s3.a),–s3.b);
}
(a)Compile-Time Error (b)32760 12345.12 (c)32760 12345.13 (d)32760 12345.13
A 4 a -5 a 5
1 -5 0.00 65531 0.00 65530
Ans. (d) Illustrating 3 different ways of declaring the structres : first, second and third are
the user-defined structure type. s1, s2 and s3 are structure variables. Also an expression of the
form ++variable.member is equivalent to ++(variable.member), i.e. ++ operator will apply to the
structure member, not the entire structure variable.
_________________________________________________________________________________________________
[Q007]. What will be the output of the following program :
struct {
int i,val[25];
}var={1,2,3,4,5,6,7,8,9},*vptr=&var;
void main()
{
printf(“%d %d %d\n”,var.i,vptr->i,(*vptr).i);
printf(“%d %d %d %d %d %d”,var.val[4],*(var.val+4),vptr->val[4],*(vptr->val+4),(*vptr).val[4],*((*vptr).val+4));
}
(a)Compile-Time Error (b)1 1 1 (c)1 1 1 (d)None of these
6 6 6 6 6 6 5 5 5 5 5 5
Ans. (b) Since value of the member ‘i’ can be accessed using var.i, vptr->i and (*vptr).i
Similarly 5th value of the member ‘val’ can be accessed using var.val[4], *(var.val+4),
vptr->val[4], *(vptr->val+4), (*vptr).val[4] and *((*vptr).val+4)
________________________________________________________________________________________________
[Q008]. What will be the output of the following program :
typedef struct {
int i;
float f;
}temp;
void alter(temp *ptr,int x,float y)
{
ptr->i=x;
ptr->f=y;
}
void main()
{
temp a={111,777.007};
printf(“%d %.2f\n”,a.i,a.f);
alter(&a,222,666.006);
printf(“%d %.2f”,a.i,a.f);
}
(a)Compile-Time error (b)111 777.007 (c)111 777.01 (d)None of these
222 666.006 222 666.01
Ans. (c) This program illustrates the transfer of a structure to a function by passing the
structure’s address (a pointer) to the function.
_________________________________________________________________________________________________
[Q009]. What will be the output of the following program :
typedef struct {
int i;
float f;
}temp;
temp alter(temp tmp,int x,float y)
{
tmp.i=x;
tmp.f=y;
return tmp;
}
void main()
{
temp a={111,777.007};
printf(“%d %.3f\n”,a.i,a.f);
a=alter(a,222,666.006);
printf(“%d %.3f”,a.i,a.f);
}
(a)Compile-Time error (b)111 777.007 (c)111 777.01 (d)None of these
222 666.006 222 666.01
Ans. (b) This program illustrates the transfer of a structure to a function by value. Also the
altered structure is now returned directly to the calling portion of the program.
_________________________________________________________________________________________________
[Q010]. What will be the output of the following program :
typedef struct {
int i;
float f;
}temp;
temp alter(temp *ptr,int x,float y)
{
temp tmp=*ptr;
printf(“%d %.2f\n”,tmp.i,tmp.f);
tmp.i=x;
tmp.f=y;
return tmp;
}
void main()
{
temp a={65535,777.777};
a=alter(&a,-1,666.666);
printf(“%d %.2f”,a.i,a.f);
}
(a)Compile-Time error (b)65535 777.777 (c)65535 777.78 (d)-1 777.78
-1 666.666 -1 666.67 -1 666.67
Ans. (d) This program illustrates the transfer of a structure to a function by passing the
structure’s address (a pointer) to the function. Also the altered structure is now returned
directly to the calling portion of the program.
_________________________________________________________________________________________________
[Q011]. What will be the output of the following program :
struct my_struct1{
int arr[2][2];
};
typedef struct my_struct1 record;
struct my_struct2{
record temp;
}list[2]={1,2,3,4,5,6,7,8};
void main()
{
int i,j,k;
for (i=1; i>=0; i–)
for (j=0; j<2; j++)
for (k=1; k>=0; k–)
printf(“%d”,list[i].temp.arr[j][k]);
}
(a)Compile-Time Error (b)Run-Time Error (c)65872143 (d)56781243
Ans. (c) This program illustrates the implementation of a nested structure i.e. structure inside
another structure.
_________________________________________________________________________________________________
[Q012]. What will be the output of the following program :
struct my_struct{
int i;
unsigned int j;
};
void main()
{
struct my_struct temp1={-32769,-1},temp2;
temp2=temp1;
printf(“%d %u”,temp2.i,temp2.j);
}
(a)32767 -1 (b)-32769 -1 (c)-32769 65535 (d)32767 65535
Ans. (d) An entire structure variable can be assigned to another structure variable, provided
both variables have the same composition.
_________________________________________________________________________________________________
[Q013]. What will be the output of the following program :
struct names {
char str[25];
struct names *next;
};
typedef struct names slist;
void main()
{
slist *list,*temp;
list=(slist *)malloc(sizeof(slist)); // Dynamic Memory Allocation
strcpy(list->str,”Hai”);
list->next=NULL;
temp=(slist *)malloc(sizeof(slist)); // Dynamic Memory Allocation
strcpy(temp->str,”Friends”);
temp->next=list;
list=temp;
while (temp != NULL)
{
printf(“%s”,temp->str);
temp=temp->next;
}
}
(a)Compile-Time Error (b)HaiFriends (c)FriendsHai (d)None of these
Ans. (c) It is sometimes desirable to include within a structure one member i.e. a pointer to the
parent structure type. Such structures are known as Self-Referencial structures. These structures
are very useful in applications that involve linked data structures, such as lists and trees.
[A linked data structure is not confined to some maximum number of components. Rather, the data
structure can expand or contract in size as required.]
_________________________________________________________________________________________________
[Q014]. Which of the following declarations is NOT Valid :
(i) struct A{
int a;
struct B {
int b;
struct B *next;
}tempB;
struct A *next;
}tempA;
(ii) struct B{
int b;
struct B *next;
};
struct A{
int a;
struct B tempB;
struct A *next;
};
(iii)struct B{
int b;
}tempB;
struct {
int a;
struct B *nextB;
};
(iv) struct B {
int b;
struct B {
int b;
struct B *nextB;
}tempB;
struct B *nextB;
}tempB;
(a) (iv) Only (b) (iii) Only (c)All of the these (d)None of these
Ans. (d) Since all the above structure declarations are valid in C.
_________________________________________________________________________________________________
[Q015]. What will be the output of the following program :
union A{
char ch;
int i;
float f;
}tempA;
void main()
{
tempA.ch=’A';
tempA.i=777;
tempA.f=12345.12345;
printf(“%d”,tempA.i);
}
(a)Compile-Time Error (b)12345 (c)Erroneous output (d)777
Ans. (c) The above program produces erroneous output (which is machine dependent). In effect,
a union creates a storage location that can be used by any one of its members at a time. When a
different member is assigned a new value, the new value supercedes the previous member’s value.
[NOTE : The compiler allocates a piece of storage that is large enough to hold the largest
variable type in the union i.e. all members share the same address.]
_________________________________________________________________________________________________
[Q016]. What will be the output of the following program :
struct A{
int i;
float f;
union B{
char ch;
int j;
}temp;
}temp1;
void main()
{
struct A temp2[5];
printf(“%d %d”,sizeof temp1,sizeof(temp2));
}
(a)6 30 (b)8 40 (c)9 45 (d)None of these
Ans. (b) Since int (2 bytes) + float (4 bytes) = (6 bytes) + Largest among union is int (2 bytes)
is equal to (8 bytes). Also the total number of bytes the array ‘temp2′ requires :
(8 bytes) * (5 bytes) = (40 bytes).
_________________________________________________________________________________________________
[Q017]. What will be the output of the following program :
void main()
{
static struct my_struct{
unsigned a:1;
unsigned b:2;
unsigned c:3;
unsigned d:4;
unsigned :6; // Fill out first word
}v={1,2,7,12};
printf(“%d %d %d %d”,v.a,v.b,v.c,v.d);
printf(“\nSize=%d bytes”,sizeof v);
}
(a)Compile-Time Error (b)1 2 7 12 (c)1 2 7 12 (d)None of these
Size=2 bytes Size=4 bytes
Ans. (b) The four fields within ‘v’ require a total of 10 bits and these bits can be accomodated
within the first word(16 bits). Unnamed fields can be used to control the alignment of bit fields
within a word of memory. Such fields provide padding within the word.
[NOTE : Some compilers order bit-fields from righ-to-left (i.e. from lower-order bits to high-
order bits) within a word, whereas other compilers order the fields from left-to-right (high-
order to low-order bits).
_________________________________________________________________________________________________
[Q018]. What are the largest values that can be assigned to each of the bit fields defined in
[Q017] above.
(a)a=0 b=2 c=3 d=4 (b)a=1 b=2 c=7 d=15 (c)a=1 b=3 c=7 d=15 (d)None of thes
Ans. (c)a=1 (1 bit: 0 or 1)
b=3 (2 bits: 00 or 01 or 10 or 11),
c=7 (3 bits: 000 or 001 or 010 or 011 or 100 or 101 or 110 or 111)
d=15 (4 bits: 0000 or 0001 or 0010 or 0011 or 0100 or 0101 or 0110 or 0111 or 1000 or
1001 or 1010 or 1011 or 1100 or 1101 or 1110 or 1111)
_________________________________________________________________________________________________
[Q019]. What will be the output of the following program :
void main()
{
struct sample{
unsigned a:1;
unsigned b:4;
}v={0,15};
unsigned *vptr=&v.b;
printf(“%d %d”,v.b,*vptr);
}
(a)Compile-Time Error (b)0 0 (c)15 15 (d)None of these
Ans. (a) Since we cannot take the address of a bit field variable i.e. Use of pointer to access
the bit fields is prohibited. Also we cannot use ’scanf’ function to read values into a bit field
as it requires the address of a bit field variable. Also array of bit-fields are not permitted
and a function cannot return a bit field.
_________________________________________________________________________________________________
[Q020]. What will be the output of the following program :
void main()
{
static struct my_struct{
unsigned a:1;
int i;
unsigned b:4;
unsigned c:10;
}v={1,10000,15,555};
printf(“%d %d %d %d”,v.i,v.a,v.b,v.c);
printf(“\nSize=%d bytes”,sizeof v);
}
(a)Compile-Time Error (b)1 10000 15 555 (c)10000 1 15 555 (d)10000 1 15 555
Size=4 bytes Size=4 bytes Size=5 bytes
Ans. (d) Here the bit field variable ‘a’ will be in first byte of one word, the variable ‘i’ will
be in the second word and the bit fields ‘b’ and ‘c’ will be in the third word. The variables
‘a’, ‘b’ and ‘c’ would not get packed into the same word. [NOTE: one word=2 bytes]
_________________________________________________________________________________________________