Computer StudyMCQ Computer ScienceQuiz Computer ScienceUGC NETUncategorized

C Programming Questions and Answers – Unions

C Programming Questions and Answers – Unions

1.Size of a union is determined by size of the.

a) First member in the union

b) Last member in the union

c) Biggest member in the union

d) Sum of the sizes of all members

View Answer

Answer:c

2.Comment on the following union declaration?

#include <stdio.h>

union temp

{

int a;

float b;

char c;

};

union temp s = {1,2.5,’A’}; //REF LINE

Which member of the union will be active after REF LINE?

a) a

b) b

c) c

d) Such declaration are illegal

View Answer

Answer:a

3.What would be the size of the following union declaration?

#include <stdio.h>

union uTemp

{

double a;

int b[10];

char c;

}u;

(Assuming size of double = 8, size of int = 4, size of char = 1)

a) 4

b) 8

c) 40

d) 80

View Answer

Answer:c

4.What type of data is holded by variable u int this C code?

#include <stdio.h>

union u_tag

{

int ival;

float fval;

char *sval;

} u;

The variable u here

a) Will be large enough to hold the largest of the three types;

b) Will be large enough to hold the smallest of the three types;

c) Will be large enough to hold the all of the three types;

d) None of the mentioned

View Answer

Answer:a

5.Members of a union are accessed as________________.

a) union-name.member

b) union-pointer->member

c) Both a & b

d) None of the mentioned

View Answer

Answer:c

6.What is the output of this C code?

#include <stdio.h>

struct

{

char *name;

union

{

char *sval;

} u;

} symtab[10];

the first character of the string sval by either of

a) *symtab[i].u.sval

b) symtab[i].u.sval[0] c) You cannot have union inside structure

d) Both a & b

View Answer

Answer:d

7.What is the output of this C code(size of int and float is 4)?

#include <stdio.h>

union

{

int ival;

float fval;

} u;

void main()

{

printf(“%d”, sizeof(u));

}

a) 16

b) 8

c) 4

d) 32

View Answer

Answer:c

8.What is the output of this C code?

#include <stdio.h>

union stu

{

int ival;

float fval;

};

void main()

{

union stu r;

r.ival = 5;

printf(“%d”, r.ival);

}

a) 9

b) Compile time error

c) 16

d) 5

View Answer

Answer:d

9.What is the output of this C code?

#include <stdio.h>

union

{

int x;

char y;

}p;

int main()

{

p.x = 10;

printf(“%d\n”, sizeof(p));

}

a) Compile time error

b) sizeof(int) + sizeof(char)

c) Depends on the compiler

d) sizeof(int)

View Answer

Answer:d

10.What is the output of this C code?

#include <stdio.h>

union

{

int x;

char y;

}p;

int main()

{

p.y = 60;

printf(“%d\n”, sizeof(p));

}

a) Compile time error

b) sizeof(int) + sizeof(char)

c) Depends on the compiler

d) sizeof(char)

View Answer

Answer:c

11.What is the output of this C code?

#include <stdio.h>

union p

{

int x;

char y;

};

int main()

{

union p p, b;

p.y = 60;

b.x = 12;

printf(“%d\n”, p.y);

}

a) Compile time error

b) Depends on the compiler

c) 60

d) Undefined behaviour

View Answer

Answer:c

12.What is the output of this C code?

#include <stdio.h>

union p

{

int x;

char y;

}k = {1, 97};

int main()

{

printf(“%d\n”, k.y);

}

a) Compile time error

b) 97

c) a

d) 1

View Answer

Answer:d

13.What is the output of this C code?

#include <stdio.h>

union p

{

int x;

char y;

}k = {.y = 97};

int main()

{

printf(“%d\n”, k.y);

}

a) Compile time error

b) 97

c) a

d) Depends on the standard

View Answer

Answer:b

14.What is the output of this C code?

#include <stdio.h>

union p

{

int x;

float y;

};

int main()

{

union p p, b;

p.x = 10;

printf(“%f\n”, p.y);

}

a) Compile time error

b) Implementation dependent

C) 10.000000

d) 0.000000

View Answer

Answer:b

15.Which of the following share a similarity in syntax?

Union, 2. Structure, 3. Arrays and 4. Pointers

a) 3 and 4

b) 1 and 2

c) 1 and 3

d) 1, 3 and 4

View Answer

Answer:b

16.What is the output of this C code?

#include <stdio.h>

union utemp

{

int a;

double b;

char c;

}u;

int main()

{

u.c = ‘A’;

u.a = 1;

printf(“%d”, sizeof(u));

}

The output will be: (Assuming size of char = 1, int = 4, double = 8)

a) 1

b) 4

c) 8

d) 13

View Answer

Answer:c

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button