Structure and Union

Structure and Union


Ques 1. what is the output for the below code?
#include <stdio.h>
#include <stdlib.h>

typedef struct str{
char * name[2]={"welcome", "good"};
int x=333;
float y=98.878;
char * name;
}p;

int main()
{
    p s1;

    printf("\n %s", s1.name[0]);
    printf(" %d", s1.x);

    return 0;
}

options:
A. welcome 333
B. good 333
C. Compile time error
D. ruin time error



Ques 2. what is the output for the following c program ?
#include <stdio.h>
#include <stdlib.h>

typedef struct str{
static int x;
float y;
}pstr;

int main()
{
    pstr p;
    p.x=32;
    p.y=22;
    printf("\n %d", p.x);
    printf("\n %f", p.y);

    return 0;
}

options:
A. compile time error
B. 32 22
C. run time error
D. 22 32


Ques 3. what is the output for the below program?

#include <stdio.h>
#include <stdlib.h>

union un{
int x;
float y =3434;
}uvar;

int main()
{
    uvar.x=0x33ff;

    printf("\n %x", uvar.x);
    printf("\n %f", uvar.y);

    return 0;
}

options:
A. 0x33ff, 3434
B. compile time error
C. 0x33ff 0.000
D. run time error


Ques 4. what is the output for the below code?
#include <stdio.h>
#include <stdlib.h>

union un{
int x;
char y ;
char z;
}uvar;

int main()
{
    uvar.x=0x3334;
    uvar.y = 'A';
    printf("\n %x", uvar.x);
    printf("\n %c", uvar.y);
    printf("\n %c", uvar.z);

    return 0;
}
options:
A. 0x3334 A,A
B. 0X0A0A A,A
C. 0X3341 95 95
D. 0X3341 A A 


Ques 5. what is the output for the below code?
#include <stdio.h>
#include <stdlib.h>

union un{
int x;
char y ;
char z;
}uvar;

int main()
{
    printf("\n %d", sizeof(uvar));
    return 0;
}

options:
A. 6
B. 4
C. 8
D. 10


Ques 6. what is the output for the below code?
#include <stdio.h>
#include <stdlib.h>
struct st{
    int y;
union un{
int x;
char m ;
char z;
}unvar;
};
int main()
{
    struct st svar;

    svar.unvar.x=0x1244;
    svar.y=0x4567;

    printf("\n 0x%x", svar.y);
    printf("\n 0x%x", svar.unvar.x);
    printf("\n %c", svar.unvar.m);
    printf("\n 0x%x", svar.unvar.z);
    return 0;
}

options:
A. compiler error
B. runtime error
C. 0x4567 0x4567 g 67
D. 0x4567 0x1244 D 0x44


Ques 7. what is the output ?
#include <stdio.h>
#include <stdlib.h>

struct st{
    int y;
union un{
int x;
char m ;
char z;
}unvar;
};
int main()
{
    struct st svar;

    printf("\n %d %d", sizeof(struct st), sizeof(union un));
    return 0;
}


options:
A. compile time error
B. 8 4
C. 8 6
D. 4 4


Ques 8. what is the output for the below code?

#include <stdio.h>
#include <stdlib.h>

struct st{
    int y[4];
union un{
double x;
long m ;
char z[8];
}unvar;
};
int main()
{
    struct st svar;

 printf("\n 0x%x 0x%x", sizeof(struct st), sizeof(union un));

    return 0;
}
options:
A. compile time error
B. 0x18 0x18
C. 0x18 0x8
D. runtime error



Ques 9. what is the output for the below code?

#include <stdio.h>
#include <stdlib.h>
typedef struct st {

    float average;
    struct st ptr;
}svar;
int main()
{
    svar sv;
    sv.average=99.44;
    sv.ptr=sv;
    printf("\n %f", sv.ptr.average);

    return 0;
}

options:
A. copile time error
B. run time error
C. 99.44
D. 99.00


Ques 10. what is the output for the below code?

#include <stdio.h>
#include <stdlib.h>
typedef struct st {

    float average;
    char y;
}svar;
int main()
{
    svar s1, s2;
    s1.average=99.44;
    s2.average=99.44;
    if(s1==s2)
    printf("\n %d", s1.average);
    printf("\n struct var compare");
    return 0;
}

options:
A. 99
B. struct var compare
C. 99.44
D. compile time error


Ques 11. what is the output for the below code?

#include <stdio.h>
#include <stdlib.h>
union un {

    char arr[4];
    int avg;
}u1;
int main()
{

    u1.avg=0;
    u1.arr[2]='F';
    u1.arr[3]='E';
    printf("\n %s", u1.arr);


    return 0;
}

options:
A. 0FE
B. nothing is printed
C. Compile time error
D. Garbage characters


Ques 12. what is the output for the below code?

#include <stdio.h>
#include <stdlib.h>
union un {

    char arr1[7];
    char arr2[7];    
}u1;
int main()
{
    strcpy(u1.arr1, "good");
    strcpy(u1.arr1, u1.arr2);
    u1.arr2[4]='b';
    printf("\n %s", u1.arr1);
    printf(" %s", u1.arr2);

    return 0;
}

options:
A. good goodb
B. compile time error
C. good bgood
D. run time error


Ques 13. If the system is little endian what is the output ?

#include <stdio.h>
#include <stdlib.h>

union un {

    char arr1[2];
    short svar;
}u1;
int main()
{
    u1.svar=0x4567;
    printf("\n 0x%x", u1.svar);
    printf(" 0x%x 0x%x", u1.arr1[0], u1.arr1[1]);

    return 0;
}


options:
A. 0x4567 0x67 0x45
B. 0x4567 0x45 0x67
C. runtime error
D. 0x4567 g E


Ques 14. what is the output for the below code?

#include <stdio.h>
#include <stdlib.h>
union un {
    int x;
    short svar;
}u1;
int main()
{
    register int i=0;
    u1.x=0x4;
    u1.svar = 0x2;
    for(i=0; i<5; i++)
    u1.svar /= 0x2*i;
    printf("\n 0x%x", u1.svar);

    return 0;
}

options:
A. runtime error
B. compile time error
C. 0
D. 0.5


Ques 15. what is the output for the below code?

#include <stdio.h>
#include <stdlib.h>
struct st {

    int m,n,p;
}svar= {3, 5, 8};
int main()
{
    svar.m+=2;
    svar.n+=2;
    svar.p+=2;

    printf("\n %d %d %d ", svar.m, svar.n, svar.p);
    return 0;
}

options:
A. 3 5 8
B. runtime error
C. 5 7 10 
D. Compile error



Answers

1. C 2. A 3. B 4. D 5. B 6. D 7. B 8. C 9. C 10. D 11. B 12. A 13. A 14. A 15. C