Home Subjects C Programming Structures & Unions

C Programming
Structures & Unions

C language from basics to advanced placement prep

46 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 11–20 of 46
Topics in C Programming
What is the size of the following union?
union Test {
int a[5];
char b[10];
double c;
}
A 10 bytes
B 20 bytes
C 30 bytes
D 24 bytes
Correct Answer:  B. 20 bytes
EXPLANATION

Union size equals the largest member. int[5] = 20 bytes, char[10] = 10 bytes, double = 8 bytes. Maximum is 20 bytes.

Test
What will be the output of this code?
union Data {
int a;
char b;
};
union Data d;
d.a = 257;
printf("%d %c", d.a, d.b);
A 257 A
B 257 \0
C 1 A
D 257 1
Correct Answer:  C. 1 A
EXPLANATION

Union shares memory. 257 in int format overwrites the char. On a little-endian system, only the lower byte (1) is stored in the char portion.

Test
What is the size of the following structure on a 32-bit system?
struct Point {
char c;
int x;
double y;
}
A 13 bytes
B 16 bytes
C 20 bytes
D 24 bytes
Correct Answer:  D. 24 bytes
EXPLANATION

char(1) + padding(3) + int(4) + double(8) = 16 bytes minimum, but alignment to double boundary makes it 24 bytes.

Test
In a nested structure scenario, how do you access a member of the inner structure?
A outer.inner.member
B outer->inner->member
C Both A and B (depending on whether it's direct or pointer)
D outer[inner][member]
Correct Answer:  C. Both A and B (depending on whether it's direct or pointer)
EXPLANATION

Use dot operator (.) for direct structure variables and arrow operator (->) for pointers, or combinations for nested access.

Test
What will be the output if you read a union member that was not last written to?
A Garbage value or previous data
B Zero by default
C Compiler warning only
D Runtime error
Correct Answer:  A. Garbage value or previous data
EXPLANATION

Reading a union member that wasn't last written gives unpredictable results (garbage value or leftover bits from previous member).

Test
What is the difference between 'struct' and 'typedef struct' in C?
A No difference, both are identical
B typedef struct creates an alias, avoiding the need to write 'struct' keyword repeatedly
C struct is faster than typedef struct
D typedef struct only works with unions
Correct Answer:  B. typedef struct creates an alias, avoiding the need to write 'struct' keyword repeatedly
EXPLANATION

typedef struct creates a type alias, allowing you to declare variables without using the 'struct' keyword each time.

Test
How can you create a structure that can hold either an integer or a floating-point number, but not both simultaneously?
A Using a structure with conditional compilation
B Using a union instead of a structure
C Using an array of mixed types
D Using a pointer to void
Correct Answer:  B. Using a union instead of a structure
EXPLANATION

A union is the appropriate choice when you need to store different data types but only one value at a time.

Test
What is the primary advantage of using unions in embedded systems?
A Reduced memory consumption
B Faster execution
C Better readability
D Easier debugging
Correct Answer:  A. Reduced memory consumption
EXPLANATION

Unions are primarily used in embedded systems to conserve memory by allowing multiple members to share the same memory location.

Test
Which of the following is a valid declaration of nested structure?
A struct A { int x; struct B { int y; } b; };
B struct A { int x; struct B b; };
C Both A and B are valid
D Neither A nor B is valid
Correct Answer:  C. Both A and B are valid
EXPLANATION

Both nested structure definitions (defining B inside A and declaring B as member) are valid in C.

Test
What happens if you attempt to assign one structure variable to another of the same type?
A Compilation error occurs
B All members are copied
C Only pointers are copied
D Runtime error occurs
Correct Answer:  B. All members are copied
EXPLANATION

In C, you can assign one structure variable to another of the same type, and all members are copied (shallow copy).

Test
IGET
IGET AI
Online · Exam prep assistant
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips