Font size
Worksheetsc union
Total questions: 65
Worksheet time: 49mins
Which library is needed for Dynamic memory allocation?
malloc.h
memory.h
stdlib.h
calloc.h
The default address stored in a pointer is :
Hexadecimal
integer
long long integer
double
If ptr is a pointer variable pointing to an array, then
"printf("%d",*(ptr++)) ;"
statement will print :
Next location address
Next location index value
Next location value
Compile time error
malloc() stands for :
malicious location
memory allocation
multiple allocation
multi-alternate locations
calloc() function takes :
One parameter
Two parameters
Two parameters but one is optional
Zero parameters
Dynamically allocated memory is released by :
release()
dealloc()
free()
Compiler automatically
Passing address to a function as parameters is called
Default function call
Call by value
Call by reference
Call by pointers
The code to swap the values of two variables using pointers is given : TRUE / FALSE ?
temp = &ptr1 ;
&ptr1 = &ptr2 ;
&ptr2 = temp ;
TRUE
FALSE
From the given code, find the output :
char a[3] = {'a', '5' , '$'};
ptr=a;
for(i=1;i<=3;i++)
printf("%ld", a++);
a 5 $
124563 124564 124565
124563 124567 124571
Junk : 125845658 -25648569 -2564875
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);
Compiler Error : Usage of double pointer without declaring
Compiler Error : Addresses cannot be multiplied
10
Compiler Error : Operator * followed by value
What is the syntax for declaring a union in C programming?
union union_name { data_type member1; data_type member2; ...; }
union union_name { data_type member1, member2, ...; }
union union_name { data_type member1; data_type member2; ... };
union union_name { data_type member1, member2; ...; }
How do you access members of a union in C programming?
Using the dot (.) operator
Using the exclamation (!) operator
Using the arrow (->) operator
Using the semicolon (;) operator
What is the size of a union in C programming?
Size of the largest member of the union
Size of the sum of all members of the union
Size of the smallest member of the union
Size of the average of all members of the union
What is the difference between a union and a structure in C programming?
A union and a structure have the same memory allocation in C programming
A union and a structure both allow for the same type of data storage in C programming
A union and a structure are interchangeable in C programming
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.
Can a union be used as a function argument in C programming? If yes, how?
Yes, a union can be used as a function argument in C programming.
Yes, but only if the union is passed by reference.
Yes, but only if the union is declared as a global variable.
No, a union cannot be used as a function argument in C programming.
How can a union be used within a structure in C programming?
By declaring the union inside the structure definition
By declaring the union outside the structure definition
By using the keyword 'struct' before the union name
By using the keyword 'union' before the structure name
Explain the concept of padding in unions in C programming.
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.
Padding in unions is used to minimize memory usage by removing unnecessary space between members
Padding in unions is used to prevent data corruption during memory allocation
Padding in unions is used to enforce a specific order of member access
What are the advantages of using unions in C programming?
Unions in C programming are only used for mathematical operations
Unions in C programming are not compatible with other programming languages
The advantages of using unions in C programming include saving memory and representing different types of data in a single variable.
Unions in C programming can only store one type of data
What are the limitations of unions in C programming?
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.
Accessing the wrong member of a union in C programming always results in a compilation error
Unions in C programming can store multiple values at the same time
Unions in C programming have no limitations
Write a sample code to demonstrate the usage of unions in C programming.
union SampleUnion { int intValue; char charValue; };
int main() { union SampleUnion u; u.charValue = 'a'; printf("Value of charValue: %c\n", u.charValue); return 0; }
union SampleUnion { int intValue; double doubleValue; };
Here is a sample code to demonstrate the usage of unions in C programming:
#include
What are the potential risks of using unions in C programming?
Unions in C programming can lead to memory corruption if not used carefully
Unions in C programming are completely risk-free
Unions in C programming always result in a compilation error if used incorrectly
Unions in C programming have no potential risks
Explain the process of accessing a specific member of a union in C programming.
Using the dot (.) operator followed by the member name
Using the exclamation (!) operator followed by the member name
Using the arrow (->) operator followed by the member name
Using the semicolon (;) operator followed by the member name
What is the purpose of using unions in C programming?
Unions in C programming are only used for storing single data types
Unions in C programming are used to represent different types of data in a single variable
Unions in C programming are used for mathematical operations only
Unions in C programming have no specific purpose
Explain the concept of memory allocation for unions in C programming.
Memory allocation for unions in C programming is based on the size of the largest member.
Memory allocation for unions in C programming is based on the sum of all members' sizes.
Memory allocation for unions in C programming is based on the size of the smallest member.
Memory allocation for unions in C programming is based on the average size of all members.
What is the syntax for accessing a union member within a structure in C programming?
Using the dot (.) operator followed by the member name
Using the exclamation (!) operator followed by the member name
Using the arrow (->) operator followed by the member name
Using the semicolon (;) operator followed by the member name
How can unions be used to optimize memory usage in C programming?
Unions can be used to minimize memory usage by removing unnecessary space between members
Unions can be used to enforce a specific order of member access
Unions can be used to store multiple values at the same time
Unions can be used to represent different types of data in a single variable
Explain the concept of memory allocation for unions in C programming.
Memory allocation for unions in C programming is based on the size of the largest member.
Memory allocation for unions in C programming is based on the sum of all members' sizes.
Memory allocation for unions in C programming is based on the size of the smallest member.
Memory allocation for unions in C programming is based on the average size of all members.
What are the potential risks of using unions in C programming?
Unions in C programming can lead to memory corruption if not used carefully
Unions in C programming are completely risk-free
Unions in C programming always result in a compilation error if used incorrectly
Unions in C programming have no potential risks
What is the syntax for accessing a union member within a structure in C programming?
Using the dot (.) operator followed by the member name
Using the exclamation (!) operator followed by the member name
Using the arrow (->) operator followed by the member name
Using the semicolon (;) operator followed by the member name
_____________ is a special type that represents a group of constants (unchangeable values).
enum
structure
array
variable
union
________________ are a way to group several related variables into one place. Each variable is known as a member
enum
structure
array
variable
union
_____________ is a collection User-Defined Data type stored at same memory location.
enum
structure
array
variable
union
_____________ is a variable that stores the memory address of another variable as its value.
structures
pointer
array
variable
union
_____________ is used to store unknown values
structures
pointer
array
variable
union
_____________ is a variable that stores values of the same datatype
structures
pointer
array
variable
union
A is a convenient tool for handling a group of logically related data items
array
structure
function
datatype
In, each member has its own storage location.
structure
union
file
destructure
Which of the following are themselves a collection of different data types?
string
structures
char
variable
Which operator connects the structure name to its member name?
-
->
.
:
Union differs from structure in the following way
all members are used at a time
only one member can be used at a time
union cannot have more members
union initialized all members as structure
What will be the size of the following structure? #include
5
11
41
44
What is the size of a C structure?
C structure is always 128 bytes
Size of C structure is the total bytes of all elements of structure
Size of C structure is the size of largest elements
Size of C structure is the size of smallest elements
Choose a correct statement about C structure elements
Structure elements are stored on random free memory locations
structure elements are stored in register memory locations
structure elements are stored in contiguous memory locations
structure elements are stored in uncontiguous memory locations
A C structure or User defined datatype is also called.
Derived data type
Secondary data type
Aggregate data type
All of the above
What is the similarity between a structure, union and enumeration?
All of them let you define new values
All of them let you define new data types
All of them let you define new pointers
All of them let you define new structures
Size of a union is determined by size of the.
first member in the union
last member in the union
biggest member in the union
sum of the sizes of all members
What are the types of data allowed inside a structure?
int, float, double, long double
char, enum, union
pointers and same structure type
all the above members
What is the size of the bit field?
1 to 18 bits
1 to 16 bits
1 to 8 bits
1 to 24 bits
Which array is used in structure members?
one dimensional
multidimensional
one or multidimensional
two dimensional
Which of the following cannot be a structure member?
function
array
structure
none of the above
Number of bytes in memory taken by the below structure is Struct test { int k; char c; };
Multiple of integer size
integer size+ character size
Depends on the platform
Multiple of word size
Size of a union is determined by size of the
First member in the union
Last member in the union
Biggest member in the union
Sum of the sizes of all members
Members of a union are accessed as
union-name.member
union-pointer->member
Both a &b
None of the above
Which of the following share a similarity in syntax? 1. Union, 2. Structure, 3. Arrays and 4. Pointers
3 and 4
1 and 2
1 and 3
1, 3 and 4
The declaration of structure is also called as
structure creator
structure signifier
structure specifier
structure member
What is the size of union u? (runs in GDB compiler)
union u{
int x;
float y;
char z[10];
};
10
8
12
18
#include <stdio.h>
union u {
int x;
float y;
char z[10];
};
int main()
{
u.x = 2;
printf("%d\n",u.x);
return 0;
}
2
compile time error
undefined output
run time error
#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)
#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;
}
65
A
AA
Undefined
#include <stdio.h>
struct s {
int x:5;
float y:6
};
int main()
{
struct s ss;
return 0;
}
Compile time error
Run time error
No output
struct s{
int x:5;
float y;
};
When sizeof(struct s) is used, (a) bytes is allocated;
struct s{
int x:5;
};
When sizeof(struct s) is used, (a) bytes is allocated;
#include <stdio.h>
struct s{
int x:5;
char y;
};
int main()
{
struct s ss;
printf("%ld\n", sizeof(struct s));
return 0;
}
4 bytes
13 bits
2 bytes
8 bytes
#include <stdio.h>
enum vehicle {Auto, Bus=3, Car, Van};
int main()
{
enum vehicle v;
v = Van;
printf("%d",v);
return 0;
}
(a)
Identify from the list which is NOT a C macro?
#define
#time
#else
#ifdef
