wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

c union

Total questions: 65

Worksheet time: 49mins

Name
Class
Date
1.

Which library is needed for Dynamic memory allocation?

a)

malloc.h

b)

memory.h

c)

stdlib.h

d)

calloc.h

2.

The default address stored in a pointer is :

a)

Hexadecimal

b)

integer

c)

long long integer

d)

double

3.

If ptr is a pointer variable pointing to an array, then

"printf("%d",*(ptr++)) ;"

statement will print :

a)

Next location address

b)

Next location index value

c)

Next location value

d)

Compile time error

4.

malloc() stands for :

a)

malicious location

b)

memory allocation

c)

multiple allocation

d)

multi-alternate locations

5.

calloc() function takes :

a)

One parameter

b)

Two parameters

c)

Two parameters but one is optional

d)

Zero parameters

6.

Dynamically allocated memory is released by :

a)

release()

b)

dealloc()

c)

free()

d)

Compiler automatically

7.

Passing address to a function as parameters is called

a)

Default function call

b)

Call by value

c)

Call by reference

d)

Call by pointers

8.

The code to swap the values of two variables using pointers is given : TRUE / FALSE ?


temp = &ptr1 ;

&ptr1 = &ptr2 ;

&ptr2 = temp ;

a)

TRUE

b)

FALSE

9.

From the given code, find the output :

char a[3] = {'a', '5' , '$'};

ptr=a;

for(i=1;i<=3;i++)

printf("%ld", a++);

a)

a 5 $

b)

124563 124564 124565

c)

124563 124567 124571

d)

Junk : 125845658 -25648569 -2564875

10.

Given the code, what will be the output:

a=5; b=2;c=0;

ptr1 = &a; ptr2 = &b; ptr3 = &c;

ptr3 = *ptr1 * *ptr2;

printf(%d", *ptr3);

a)

Compiler Error : Usage of double pointer without declaring

b)

Compiler Error : Addresses cannot be multiplied

c)

10

d)

Compiler Error : Operator * followed by value

11.

What is the syntax for declaring a union in C programming?

a)

union union_name { data_type member1; data_type member2; ...; }

b)

union union_name { data_type member1, member2, ...; }

c)

union union_name { data_type member1; data_type member2; ... };

d)

union union_name { data_type member1, member2; ...; }

12.

How do you access members of a union in C programming?

a)

Using the dot (.) operator

b)

Using the exclamation (!) operator

c)

Using the arrow (->) operator

d)

Using the semicolon (;) operator

13.

What is the size of a union in C programming?

a)

Size of the largest member of the union

b)

Size of the sum of all members of the union

c)

Size of the smallest member of the union

d)

Size of the average of all members of the union

14.

What is the difference between a union and a structure in C programming?

a)

A union and a structure have the same memory allocation in C programming

b)

A union and a structure both allow for the same type of data storage in C programming

c)

A union and a structure are interchangeable in C programming

d)

The main difference between a union and a structure is that a structure allocates enough memory to store each of its members, while a union allocates enough memory to store the largest member.

15.

Can a union be used as a function argument in C programming? If yes, how?

a)

Yes, a union can be used as a function argument in C programming.

b)

Yes, but only if the union is passed by reference.

c)

Yes, but only if the union is declared as a global variable.

d)

No, a union cannot be used as a function argument in C programming.

16.

How can a union be used within a structure in C programming?

a)

By declaring the union inside the structure definition

b)

By declaring the union outside the structure definition

c)

By using the keyword 'struct' before the union name

d)

By using the keyword 'union' before the structure name

17.

Explain the concept of padding in unions in C programming.

a)

Padding in unions is used to ensure that each member of the union is properly aligned in memory, according to the largest member's size.

b)

Padding in unions is used to minimize memory usage by removing unnecessary space between members

c)

Padding in unions is used to prevent data corruption during memory allocation

d)

Padding in unions is used to enforce a specific order of member access

18.

What are the advantages of using unions in C programming?

a)

Unions in C programming are only used for mathematical operations

b)

Unions in C programming are not compatible with other programming languages

c)

The advantages of using unions in C programming include saving memory and representing different types of data in a single variable.

d)

Unions in C programming can only store one type of data

19.

What are the limitations of unions in C programming?

a)

The limitations of unions in C programming include the ability to store only one value at a time and the potential for undefined behavior when accessing the wrong member.

b)

Accessing the wrong member of a union in C programming always results in a compilation error

c)

Unions in C programming can store multiple values at the same time

d)

Unions in C programming have no limitations

20.

Write a sample code to demonstrate the usage of unions in C programming.

a)

union SampleUnion { int intValue; char charValue; };

b)

int main() { union SampleUnion u; u.charValue = 'a'; printf("Value of charValue: %c\n", u.charValue); return 0; }

c)

union SampleUnion { int intValue; double doubleValue; };

d)

Here is a sample code to demonstrate the usage of unions in C programming: #include union SampleUnion { int intValue; float floatValue; }; int main() { union SampleUnion u; u.intValue = 10; printf("Value of intValue: %d\n", u.intValue); u.floatValue = 3.14; printf("Value of floatValue: %f\n", u.floatValue); return 0; }

21.

What are the potential risks of using unions in C programming?

a)

Unions in C programming can lead to memory corruption if not used carefully

b)

Unions in C programming are completely risk-free

c)

Unions in C programming always result in a compilation error if used incorrectly

d)

Unions in C programming have no potential risks

22.

Explain the process of accessing a specific member of a union in C programming.

a)

Using the dot (.) operator followed by the member name

b)

Using the exclamation (!) operator followed by the member name

c)

Using the arrow (->) operator followed by the member name

d)

Using the semicolon (;) operator followed by the member name

23.

What is the purpose of using unions in C programming?

a)

Unions in C programming are only used for storing single data types

b)

Unions in C programming are used to represent different types of data in a single variable

c)

Unions in C programming are used for mathematical operations only

d)

Unions in C programming have no specific purpose

24.

Explain the concept of memory allocation for unions in C programming.

a)

Memory allocation for unions in C programming is based on the size of the largest member.

b)

Memory allocation for unions in C programming is based on the sum of all members' sizes.

c)

Memory allocation for unions in C programming is based on the size of the smallest member.

d)

Memory allocation for unions in C programming is based on the average size of all members.

25.

What is the syntax for accessing a union member within a structure in C programming?

a)

Using the dot (.) operator followed by the member name

b)

Using the exclamation (!) operator followed by the member name

c)

Using the arrow (->) operator followed by the member name

d)

Using the semicolon (;) operator followed by the member name

26.

How can unions be used to optimize memory usage in C programming?

a)

Unions can be used to minimize memory usage by removing unnecessary space between members

b)

Unions can be used to enforce a specific order of member access

c)

Unions can be used to store multiple values at the same time

d)

Unions can be used to represent different types of data in a single variable

27.

Explain the concept of memory allocation for unions in C programming.

a)

Memory allocation for unions in C programming is based on the size of the largest member.

b)

Memory allocation for unions in C programming is based on the sum of all members' sizes.

c)

Memory allocation for unions in C programming is based on the size of the smallest member.

d)

Memory allocation for unions in C programming is based on the average size of all members.

28.

What are the potential risks of using unions in C programming?

a)

Unions in C programming can lead to memory corruption if not used carefully

b)

Unions in C programming are completely risk-free

c)

Unions in C programming always result in a compilation error if used incorrectly

d)

Unions in C programming have no potential risks

29.

What is the syntax for accessing a union member within a structure in C programming?

a)

Using the dot (.) operator followed by the member name

b)

Using the exclamation (!) operator followed by the member name

c)

Using the arrow (->) operator followed by the member name

d)

Using the semicolon (;) operator followed by the member name

30.

_____________ is a special type that represents a group of constants (unchangeable values).

a)

enum

b)

structure

c)

array

d)

variable

e)

union

31.

________________ are a way to group several related variables into one place. Each variable is known as a member

a)

enum

b)

structure

c)

array

d)

variable

e)

union

32.

_____________ is a collection User-Defined Data type stored at same memory location.

a)

enum

b)

structure

c)

array

d)

variable

e)

union

33.

_____________ is a variable that stores the memory address of another variable as its value.

a)

structures

b)

pointer

c)

array

d)

variable

e)

union

34.

_____________ is used to store unknown values

a)

structures

b)

pointer

c)

array

d)

variable

e)

union

35.

_____________ is a variable that stores values of the same datatype

a)

structures

b)

pointer

c)

array

d)

variable

e)

union

36.

A is a convenient tool for handling a group of logically related data items

a)

array

b)

structure

c)

function

d)

datatype

37.

In, each member has its own storage location.

a)

structure

b)

union

c)

file

d)

destructure

38.

Which of the following are themselves a collection of different data types?

a)

string

b)

structures

c)

char

d)

variable

39.

Which operator connects the structure name to its member name?

a)

-

b)

->

c)

.

d)

:

40.

Union differs from structure in the following way

a)

all members are used at a time

b)

only one member can be used at a time

c)

union cannot have more members

d)

union initialized all members as structure

41.

What will be the size of the following structure? #include struct temp { int a[10]; char p; };

a)

5

b)

11

c)

41

d)

44

42.

What is the size of a C structure?

a)

C structure is always 128 bytes

b)

Size of C structure is the total bytes of all elements of structure

c)

Size of C structure is the size of largest elements

d)

Size of C structure is the size of smallest elements

43.

Choose a correct statement about C structure elements

a)

Structure elements are stored on random free memory locations

b)

structure elements are stored in register memory locations

c)

structure elements are stored in contiguous memory locations

d)

structure elements are stored in uncontiguous memory locations

44.

A C structure or User defined datatype is also called.

a)

Derived data type

b)

Secondary data type

c)

Aggregate data type

d)

All of the above

45.

What is the similarity between a structure, union and enumeration?

a)

All of them let you define new values

b)

All of them let you define new data types

c)

All of them let you define new pointers

d)

All of them let you define new structures

46.

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

47.

What are the types of data allowed inside a structure?

a)

int, float, double, long double

b)

char, enum, union

c)

pointers and same structure type

d)

all the above members

48.

What is the size of the bit field?

a)

1 to 18 bits

b)

1 to 16 bits

c)

1 to 8 bits

d)

1 to 24 bits

49.

Which array is used in structure members?

a)

one dimensional

b)

multidimensional

c)

one or multidimensional

d)

two dimensional

50.

Which of the following cannot be a structure member?

a)

function

b)

array

c)

structure

d)

none of the above

51.

Number of bytes in memory taken by the below structure is Struct test { int k; char c; };

a)

Multiple of integer size

b)

integer size+ character size

c)

Depends on the platform

d)

Multiple of word size

52.

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

53.

Members of a union are accessed as

a)

union-name.member

b)

union-pointer->member

c)

Both a &b

d)

None of the above

54.

Which of the following share a similarity in syntax? 1. 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

55.

The declaration of structure is also called as

a)

structure creator

b)

structure signifier

c)

structure specifier

d)

structure member

56.

What is the size of union u? (runs in GDB compiler)

union u{

int x;

float y;

char z[10];

};

a)

10

b)

8

c)

12

d)

18

57.

#include <stdio.h>

union u {

int x;

float y;

char z[10];

};

int main()

{

u.x = 2;

printf("%d\n",u.x);

return 0;

}

a)

2

b)

compile time error

c)

undefined output

d)

run time error

58.

#include <stdio.h>

union u{

int x;

float y;

char z[10];

};

int main()

{

union u uu;

uu.x = 2;

printf("%d\n",uu.x);

return 0;

}

(a)  

59.

#include <stdio.h>

union u{

int x;

float y;

char z[10];

};

int main()

{

union u uu;

uu.x = 65;

printf("%c\n",uu.z[0]);

return 0;

}

a)

65

b)

A

c)

AA

d)

Undefined

60.

#include <stdio.h>

struct s {

int x:5;

float y:6

};

int main()

{

struct s ss;

return 0;

}

a)

Compile time error

b)

Run time error

c)

No output

61.

struct s{

int x:5;

float y;

};

When sizeof(struct s) is used, (a)   bytes is allocated;

62.

struct s{

int x:5;

};

When sizeof(struct s) is used, (a)   bytes is allocated;

63.

#include <stdio.h>

struct s{

int x:5;

char y;

};

int main()

{

struct s ss;

printf("%ld\n", sizeof(struct s));

return 0;

}

a)

4 bytes

b)

13 bits

c)

2 bytes

d)

8 bytes

64.

#include <stdio.h>

enum vehicle {Auto, Bus=3, Car, Van};

int main()

{

enum vehicle v;

v = Van;

printf("%d",v);

return 0;

}

(a)  

65.

Identify from the list which is NOT a C macro?

a)

#define

b)

#time

c)

#else

d)

#ifdef