C& CPP Tutorials
About C
Program Structure
Running C Program
Variables, constants
Operators
Control Structure
Array
Function
Pointers
Preprocessor
Structure
File
Bitwise Operators
Recursion
 Data Structures
Introduction
Stacks
Queues
Linked List
Sorting 
Searching
 Test Your Skill
Fundamentals
Input and Output
Branching and Looping
Function
Pointers I
Pointers II
Structure and Union
Sample Problems I
Sample Problems II
 Help and Support
C Forum
Source code
C  Yahoo Group
C Compilers
 

Structures & Unions

 [Q001] What will be the output of the following program :
  struct {
              int i;
              float f;
           }var;
 int main()
 {
    var.i=5;
    var.f=9.76723;
    printf("%d %.2f",var.i,var.f);
    return(0);
 }

  (a)Compile-Time Error
  (b)5 9.76723
  (c)5 9.76
  (d)5 9.77

  [Q002] What will be the output of the following program :
 int main()
   {
     int val=1234;
     int* ptr=&val;
     printf("%d %d",val,*ptr++);
     return(0);
   }

   (a)Compile-Time Error
   (b)5 9.76723
   (c)5 9.76
   (d)5 9.77

 [Q003] What will be the output of the following program :
   struct values {
                         int i;
                         float f;
                      };
 int main()
 {
    struct values var={555,67.05501};
    printf("%2d %.2f",var.i,var.f);
    return(0);
 }

  (a)1234 1234
  (b)1235 1235
  (c)1234 1235
  (d)1235 1234

   [Q004] What will be the output of the following program :
  typedef struct {
                           int i;
                           float f;
                       }values;
   int main()
         {
           static values var={555,67.05501};
           printf("%2d %.2f",var.i,var.f);
           return(0);
        }

  (a)Compile-Time Error
  (b)55 67.05
  (c)555 67.06
  (d)555 67.05

 [Q005] What will be the output of the following program :

 struct my_struct {
                              int i=7;
                              float f=999.99;
                           }var;
 int main()
 {
    var.i=5;
    printf("%d %.2f",var.i,var.f);
    return(0);
 }

  (a)Compile-Time Error
  (b)7 999.99
  (c)5 999.99
  (d)None of these

   [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;
   int 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);
    return(0);
 }

   (a)Compile-Time Error
   (b)32760 12345.12
     A 4
     1 -5
 (c)32760 12345.13
     a -5
     0.00 65531
 (d)32760 12345.13
     a 5

    
0.00 65530

 [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;
 int 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));
    return(0);
 }

  (a)Compile-Time Error
 (b)1 1 1
     6 6 6 6 6 6
 (c)1 1 1
     5 5 5 5 5 5
 (d)None of these

 [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;
 }
 int 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);
    return(0);
 }

 (a)Compile-Time error
 (b)111 777.007
     222 666.006
 (c)111 777.01
     222 666.01
 (d)None of these

 [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;
 }
 int 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);
    return(0);
 }

  (a)Compile-Time error
 (b)111 777.007
     222 666.006
 (c)111 777.01
     222 666.01
 (d)None of these

  [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;
 }
 int main()
 {
     temp a={65535,777.777};
     a=alter(&a,-1,666.666);
     printf("%d %.2f",a.i,a.f);
    return(0);
 }

   (a)Compile-Time error
 (b)65535 777.777
     -1 666.666
 (c)65535 777.78
     -1 666.67
 (d)-1 777.78
     -1 666.67

[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};
 int 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]);
    return(0);
 }

 (a)Compile-Time Error
 (b)Run-Time Error
 (c)65872143
 (d)56781243

 [Q012] What will be the output of the following program :
   
struct my_struct {
                               int i;
                               unsigned int j;
                          };
 int main()
 {
     struct my_struct temp1={-32769,-1},temp2;
     temp2=temp1;
     printf("%d %u",temp2.i,temp2.j);
    return(0);
 }

 (a)32767 -1
 (b)-32769 -1
 (c)-32769 65535
 (d)32767 65535

 [Q013] What will be the output of the following program :
  
struct names {
                          char str[25];
                          struct names *next;
                      };
 typedef struct names slist;
 int 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;
     }
    return(0);
 }

  (a)Compile-Time Error
 (b)HaiFriends
 (c)FriendsHai
 (d)None of these

 [Q014] What will be the output of the following program :
(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

[Q015] What will be the output of the following program :
union A {
                  char ch;
                  int i;
                  float f;
              }tempA;
 int main()
 {
     tempA.ch='A';
     tempA.i=777;
     tempA.f=12345.12345;
     printf("%d",tempA.i);
    return(0);
 }

  (a)Compile-Time Error
 (b)12345
 (c)Erroneous output
 (d)777

 [Q016] What will be the output of the following program :
struct A {
                  int i;
                  float f;
                  union B {
                                   char ch;
                                   int j;
                               }temp;
             }temp1;
 int main()
 {
     struct A temp2[5];
     printf("%d %d",sizeof temp1,sizeof(temp2));
    return(0);
 }

  (a)6 30
 (b)8 40
 (c)9 45
 (d)None of these

 [Q017] What will be the output of the following program :
 int 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);
    return(0);
 }

  (a)Compile-Time Error
 (b)1 2 7 12
     Size=2 bytes
 (c)1 2 7 12
     Size=4 bytes
 (d)None of these

 [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 these
   [Q019] What will be the output of the following program :
 int main()
 {
    struct sample {
                              unsigned a:1;
                              unsigned b:4;
                          }v={0,15};
    unsigned *vptr=&v.b;
    printf("%d %d",v.b,*vptr);
    return(0);
 }

   (a)Compile-Time Error
 (b)0 0
 (c)15 15
 (d)None of these

 [Q020] What will be the output of the following program :
int 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);
    return(0);
 }

 (a)Compile-Time Error
 (b)1 10000 15 555
     Size=4 bytes
 (c)10000 1 15 555
     Size=4 bytes
 (d)10000 1 15 555
     Size=5 bytes

Solutions for the above queries.

Back Next
 

Google