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 31–40 of 46
Topics in C Programming
Which initialization method for structures is used in designated initializers (C99 feature)?
A struct s obj = {1, 2, 3};
B struct s obj = {.x = 1, .y = 2};
C struct s obj[]; obj[0].x = 1;
D struct s *obj = malloc(...);
Correct Answer:  B. struct s obj = {.x = 1, .y = 2};
EXPLANATION

Designated initializers (C99) allow specifying members by name using dot notation, making code more readable and maintainable.

Test
What will be the behavior of this code? struct s { int a; }; struct s obj = {5}; struct s *ptr = &obj; ptr->a = 10; printf("%d", obj.a);
A 5
B 10
C Garbage value
D Compilation error
Correct Answer:  B. 10
EXPLANATION

ptr points to obj, so ptr->a = 10 modifies obj.a to 10. The printf outputs the modified value 10.

Test
What is the difference between struct tag and struct type in C?
A Tag is the variable name, type is the structure name
B Tag is the structure name, type is the variable declaration
C They are the same thing
D Type is used in unions only
Correct Answer:  B. Tag is the structure name, type is the variable declaration
EXPLANATION

In 'struct person { int age; } p;', 'person' is the tag (structure name) and 'p' is the type/variable. Tag names the structure template; type creates actual instances.

Test
Which statement is correct about bit fields in structures?
A They can be applied to any data type
B They are used to pack multiple members into fewer bytes
C They increase the structure size
D They make structures faster to access
Correct Answer:  B. They are used to pack multiple members into fewer bytes
EXPLANATION

Bit fields allow you to specify the number of bits for integral members, enabling memory optimization by packing multiple small values into a single byte.

Test
What happens when you modify a union member that overlaps with another?
A Both members update independently
B The previous member value is preserved
C The previous member's value gets overwritten
D A compilation error occurs
Correct Answer:  C. The previous member's value gets overwritten
EXPLANATION

Since union members share memory, modifying one member overwrites the value of other members because they occupy the same memory location.

Test
Which of the following correctly initializes a structure array? struct point { int x, y; } arr[3] = ?
A { {1,2}, {3,4}, {5,6} }
B { 1,2,3,4,5,6 }
C [ {1,2}, {3,4}, {5,6} ]
D None of the above
Correct Answer:  A. { {1,2}, {3,4}, {5,6} }
EXPLANATION

Structure arrays are initialized with nested braces, where each set of braces contains initialization values for one structure element.

Test
In a nested structure, how do you access the innermost member? struct outer { struct inner { int value; } in; } obj;
A obj.value
B obj.in.value
C obj->in->value
D obj.in->value
Correct Answer:  B. obj.in.value
EXPLANATION

For nested structures accessed through an object, use the dot operator multiple times: obj.in.value accesses the value member of the inner structure.

Test
What will be printed by this code? struct s { int a; struct s *next; }; struct s *ptr; sizeof(struct s)
A 4 bytes
B 8 bytes
C 12 bytes
D 16 bytes
Correct Answer:  C. 12 bytes
EXPLANATION

sizeof(int) = 4 bytes, sizeof(pointer) = 8 bytes (on 64-bit systems). Total = 12 bytes. Pointers are fixed size regardless of what they point to.

Test
Consider struct test { int x; double y; char z; }; What is the minimum size of this structure (assuming 4-byte int, 8-byte double)?
A 13 bytes
B 16 bytes
C 20 bytes
D 24 bytes
Correct Answer:  D. 24 bytes
EXPLANATION

Due to padding: int (4) + padding (4) + double (8) + char (1) + padding (3) = 24 bytes. The structure aligns to the largest member size.

Test
Consider: union u { int x; double y; }; What is sizeof(u)?
A 4 bytes
B 8 bytes
C 12 bytes
D 16 bytes
Correct Answer:  B. 8 bytes
EXPLANATION

The size of a union is equal to its largest member. double is typically 8 bytes, which is larger than int (4 bytes).

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