1Which keyword is used to declare a structure in C?
A.struc
B.struct
C.structure
D.type
Correct Answer: struct
Explanation:The keyword struct is used to define a user-defined data type that allows combining data items of different kinds.
Incorrect! Try again.
2What is the correct syntax to declare a structure variable emp of type struct Employee?
A.Employee emp;
B.struct Employee emp;
C.struct emp Employee;
D.new struct Employee emp;
Correct Answer: struct Employee emp;
Explanation:In C, unless using typedef, you must use the keyword struct followed by the structure tag (Employee) and then the variable name.
Incorrect! Try again.
3Which operator is used to access a member of a structure variable?
A.. (dot operator)
B.-> (arrow operator)
C.* (asterisk)
D.& (ampersand)
Correct Answer: . (dot operator)
Explanation:The dot operator (.) is used to access members of a structure when working with the structure variable directly.
Incorrect! Try again.
4If ptr is a pointer to a structure, which operator is used to access its members?
A..
B.->
C.:
D.::
Correct Answer: ->
Explanation:The arrow operator (->) is used to access structure members through a pointer to that structure. ptr->member is equivalent to (*ptr).member.
Incorrect! Try again.
5Which of the following statements about structure declaration is TRUE?
A.It allocates memory for variables.
B.It defines a new data type but does not allocate memory.
C.It must contain only integers.
D.It cannot contain arrays.
Correct Answer: It defines a new data type but does not allocate memory.
Explanation:A structure declaration (blueprint) describes the format of the data. Memory is allocated only when variables of that structure type are defined.
Incorrect! Try again.
6What does the following code snippet signify?
c
struct Point {
int x;
int y;
};
A.Definition of a structure variable
B.Declaration of a structure type
C.Initialization of a structure
D.A syntax error due to the semicolon
Correct Answer: Declaration of a structure type
Explanation:This code defines the layout of struct Point. The semicolon at the end is required syntax for closing the structure declaration.
Incorrect! Try again.
7Given the following structure:
c
struct Data {
int id;
char name[20];
} d1 = {1, "Test"};
How is d1 initialized?
A.Compile-time initialization
B.Run-time initialization
C.Dynamic initialization
D.Copy initialization
Correct Answer: Compile-time initialization
Explanation:The structure variable d1 is defined and initialized simultaneously with constant values.
Incorrect! Try again.
8Can a structure contain another structure of the same type as a member variable?
A.Yes, directly.
B.No, never.
C.Yes, but only as a pointer.
D.Yes, but only as a static member.
Correct Answer: Yes, but only as a pointer.
Explanation:A structure cannot contain an instance of itself (incomplete type), but it can contain a pointer to the same structure type. This is used in self-referential structures like linked lists.
Incorrect! Try again.
9What is the size of the following union assuming int is 4 bytes and char is 1 byte?
c
union Packet {
int a;
char b;
};
A.1 byte
B.4 bytes
C.5 bytes
D.8 bytes
Correct Answer: 4 bytes
Explanation:The size of a union is determined by the size of its largest member. Here, int (4 bytes) is larger than char (1 byte), so the size is 4 bytes.
Incorrect! Try again.
10How do you access the member age of the structure variable s using a pointer p pointing to s?
A.p.age
B.p->age
C.*p.age
D.s->age
Correct Answer: p->age
Explanation:When accessing members via a pointer, use the arrow operator ->. Alternatively, (*p).age is valid, but *p.age is incorrect due to operator precedence.
Incorrect! Try again.
11In a nested structure, how are inner members accessed?
A.outer.inner_member
B.inner.outer_member
C.outer.inner.member
D.outer->inner->member
Correct Answer: outer.inner.member
Explanation:If structure A contains structure B, and structure B contains member C, you access C via varA.varB.C.
Incorrect! Try again.
12Which of the following creates an alias Student for struct student_record?
B.Structures allocate memory for all members; unions share memory for all members.
C.Structures are always smaller than unions.
D.Unions can only contain integers.
Correct Answer: Structures allocate memory for all members; unions share memory for all members.
Explanation:In a structure, each member has its own storage. In a union, all members share the same memory location, so only one member can be active at a time.
Incorrect! Try again.
15Consider the declaration: struct { int x; } s[5];. What is s?
A.A structure containing an array.
B.A pointer to a structure.
C.An array of 5 structures.
D.A function returning a structure.
Correct Answer: An array of 5 structures.
Explanation:The syntax tag variable[N] declares an array of N elements of that structure type.
Incorrect! Try again.
16Which of the following is correct to define an array inside a structure?
A.struct s { int arr[10]; };
B.struct s { int arr; };
C.struct s { array int arr; };
D.struct s { [10]int arr; };
Correct Answer: struct s { int arr[10]; };
Explanation:Standard array declaration syntax is valid inside a structure definition.
Incorrect! Try again.
17Given: union Data { int i; float f; }; union Data d; d.i = 10; d.f = 2.5;. What is the value of d.i?
A.10
B.2.5
C.Undefined/Garbage
D.
Correct Answer: Undefined/Garbage
Explanation:In a union, writing to one member overwrites the others because they share memory. Setting d.f corrupts the integer representation of d.i.
Incorrect! Try again.
18Can you compare two structure variables directly using ==?
A.Yes, it compares all members.
B.No, C does not support operator overloading for structs.
C.Yes, if they are initialized.
D.No, unless they are pointers.
Correct Answer: No, C does not support operator overloading for structs.
Explanation:C does not allow direct comparison of structures using == or !=. You must compare each member individually.
Incorrect! Try again.
19What is the memory alignment/padding in structures?
A.Adding extra bytes to ensure members sit on specific address boundaries.
B.Removing unused variables.
C.Adding comments to code.
D.Compressing the structure size.
Correct Answer: Adding extra bytes to ensure members sit on specific address boundaries.
Explanation:Compilers insert 'padding' bytes between members to ensure that specific types (like int or double) are aligned in memory for performance reasons.
Incorrect! Try again.
20Given struct Node { int data; struct Node *next; };, what is this structure typically used for?
A.Arrays
B.Linked Lists
C.Unions
D.Files
Correct Answer: Linked Lists
Explanation:This is a self-referential structure containing a pointer to the same structure type, the fundamental building block of a linked list.
Incorrect! Try again.
21If struct A { int x; }; and struct B { int x; };, are struct A and struct B compatible types?
A.Yes, because they have the same members.
B.No, they are distinct types.
C.Yes, if casted.
D.No, unless typedef is used.
Correct Answer: No, they are distinct types.
Explanation:In C, two structures declared with different tags are distinct types, even if their members are identical.
Incorrect! Try again.
22How do you access the element at index 2 of an integer array arr inside a structure variable s?
A.s.arr.2
B.s->arr[2]
C.s.arr[2]
D.s[2].arr
Correct Answer: s.arr[2]
Explanation:First access the member array arr using the dot operator (s.arr), then use standard array indexing [2].
Incorrect! Try again.
23What is the output size of sizeof(struct empty) in C (GCC/Standard behavior for empty structs)?
A.
B.1
C.Compilation Error (or undefined in pure C standard)
D.4
Correct Answer:
Explanation:Standard C (C99/C11) does not explicitly define empty structures (undefined behavior), though GCC allows it and returns 0. In C++, empty structs are size 1.
Incorrect! Try again.
24Which syntax initializes a union?
A.union Data d = {10, 20.5};
B.union Data d = {10};
C.union Data d = 10;
D.union Data d(10);
Correct Answer: union Data d = {10};
Explanation:A union can only be initialized with a value appropriate for its first member. Syntax {10} initializes the first member.
Incorrect! Try again.
25Is it possible to have a pointer to a bit-field in a structure?
A.Yes
B.No
C.Only if it is an integer
D.Only in Unions
Correct Answer: No
Explanation:Bit-fields may occupy parts of a byte, and pointers address whole bytes. Therefore, you cannot take the address of a bit-field.
Incorrect! Try again.
26What implies (*ptr).member?
A.Accessing member of a pointer variable.
B.Dereferencing a pointer to a structure and accessing the member.
C.Multiplying ptr by member.
D.Syntax error.
Correct Answer: Dereferencing a pointer to a structure and accessing the member.
Explanation:*ptr dereferences the pointer to get the structure, and .member accesses the field. The parentheses are needed because . has higher precedence than *.
Incorrect! Try again.
27Given: struct { int a; char b; } arr[2] = {{1, 'a'}, {2, 'b'}};. What is arr[1].a?
A.1
B.2
C.'a'
D.'b'
Correct Answer: 2
Explanation:arr[1] refers to the second structure in the array. Its member a was initialized to 2.
Incorrect! Try again.
28Which header file is required to use structures?
A.<stdio.h>
B.<struct.h>
C.<stdlib.h>
D.None
Correct Answer: None
Explanation:Structures are a core language feature of C and do not require any specific header file, unlike I/O functions.
Incorrect! Try again.
29Can a union be nested inside a structure?
A.Yes
B.No
C.Only if the union is named
D.Only if the structure is named
Correct Answer: Yes
Explanation:Structures can contain unions, and unions can contain structures. This is common in complex data formatting.
Incorrect! Try again.
30What is the size of struct { char a; int b; } on a machine with 4-byte integers and 4-byte alignment?
A.5 bytes
B.8 bytes
C.4 bytes
D.12 bytes
Correct Answer: 8 bytes
Explanation:char takes 1 byte. To align the following 4-byte int, 3 bytes of padding are added. Total: 1 (char) + 3 (padding) + 4 (int) = 8 bytes.
Incorrect! Try again.
31What is an 'anonymous structure'?
A.A structure without members.
B.A structure without a variable.
C.A structure without a tag name.
D.A structure that is hidden.
Correct Answer: A structure without a tag name.
Explanation:An anonymous structure is defined without a tag, usually nested inside another structure or union.
Incorrect! Try again.
32How are members of a union stored in memory?
A.Sequentially
B.In a linked list
C.Overlapping at the same start address
D.Randomly
Correct Answer: Overlapping at the same start address
Explanation:All members of a union start at the same memory address.
Incorrect! Try again.
33Consider struct Box { int l, w, h; }; struct Box *b;. Which expression calculates the memory size required for the pointer?
A.sizeof(Box)
B.sizeof(struct Box)
C.sizeof(b)
D.sizeof(*b)
Correct Answer: sizeof(b)
Explanation:sizeof(b) returns the size of the pointer itself (usually 4 or 8 bytes), while sizeof(*b) or sizeof(struct Box) returns the size of the structure data.
Incorrect! Try again.
34Can a structure be passed to a function by value?
A.Yes
B.No
C.Only if it is small
D.Only if it contains no arrays
Correct Answer: Yes
Explanation:Structures can be passed by value. A copy of the entire structure is made and passed to the function.
Incorrect! Try again.
35Why is passing a large structure by pointer preferred over passing by value?
A.It is syntactically required.
B.It avoids copying the entire structure data, saving memory and time.
C.It allows the structure to be immutable.
D.Pointers are safer.
Correct Answer: It avoids copying the entire structure data, saving memory and time.
Explanation:Passing by value copies every byte of the structure. Passing by pointer only copies the address (typically 4 or 8 bytes).
Incorrect! Try again.
36What is the result of struct { int a; } s1, s2; s1.a = 5; s2 = s1; s2.a = 10; on s1.a?
A.5
B.10
C.
D.Undefined
Correct Answer: 5
Explanation:When s2 = s1 happens, s1 is copied to s2. Modifying s2 afterwards does not affect s1 because they are separate memory blocks.
Incorrect! Try again.
37How do you declare a pointer ptr to a function that returns a structure S?
A.struct S *ptr();
B.struct S (*ptr)();
C.struct S ptr*();
D.pointer struct S ptr();
Correct Answer: struct S (*ptr)();
Explanation:This declares ptr as a pointer to a function returning struct S. Without the parentheses, struct S *ptr() would be a function returning a pointer.
Incorrect! Try again.
38In the declaration struct { int a : 4; } x;, what does : 4 indicate?
A.Initialization to 4
B.Array size 4
C.Bit-field of width 4 bits
D.Byte alignment of 4
Correct Answer: Bit-field of width 4 bits
Explanation:This is a bit-field declaration. The member a will occupy only 4 bits of memory.
Incorrect! Try again.
39Which macro is used to find the offset of a member within a structure?
A.sizeof
B.typeof
C.offsetof
D.alignof
Correct Answer: offsetof
Explanation:offsetof(type, member) (defined in <stddef.h>) returns the byte offset of a member from the beginning of the structure.
Incorrect! Try again.
40If u is a union variable and p is a pointer to u, which is valid?
A.p.member
B.u->member
C.p->member
D.*u.member
Correct Answer: p->member
Explanation:Pointers to unions use the arrow operator (->) just like pointers to structures.
Incorrect! Try again.
41Can you use the scanf function to read input directly into a structure member?
A.No
B.Yes, e.g., scanf("%d", &s.age);
C.Yes, e.g., scanf("%d", s.age);
D.Only for strings
Correct Answer: Yes, e.g., scanf("%d", &s.age);
Explanation:You pass the address of the member to scanf. The & operator is applied to s.age.
Incorrect! Try again.
42What happens if a structure is partially initialized? E.g., struct { int a, b; } s = {1};
A.Compile error
B.b is uninitialized (garbage)
C.b is initialized to 0
D.Runtime error
Correct Answer: b is initialized to 0
Explanation:If an explicit initialization list is provided but is shorter than the number of members, remaining members are initialized to zero.
Incorrect! Try again.
43Given struct Test { char c; int i; };, which order are members stored in memory?
A.In order of declaration (c then i)
B.Largest to smallest (i then c)
C.Smallest to largest (c then i)
D.Compiler dependent entirely
Correct Answer: In order of declaration (c then i)
Explanation:C standard guarantees that members are stored in increasing memory addresses in the order they are declared, though padding may exist between them.
Incorrect! Try again.
44What is a 'flexible array member' in C?
A.An array inside a struct with size 0 or undefined [], declared as the last member.
B.An array that changes type.
C.An array declared in a union.
D.A pointer.
Correct Answer: An array inside a struct with size 0 or undefined [], declared as the last member.
Explanation:Introduced in C99, type name[] as the last member allows allocating a structure with a variable amount of data following it.
Incorrect! Try again.
45Can a structure contain a pointer to a union?
A.Yes
B.No
C.Only if they are defined in same scope
D.Only in C++
Correct Answer: Yes
Explanation:Structures can contain pointers to any data type, including unions.
Incorrect! Try again.
46Consider struct S { int *p; }; struct S s1, s2; int x = 10; s1.p = &x; s2 = s1;. What is the relationship between s1.p and s2.p?
A.They point to different addresses.
B.They point to the same address.
C.s2.p is null.
D.Copying is invalid for pointers.
Correct Answer: They point to the same address.
Explanation:This is a shallow copy. The pointer address value is copied, so both structures point to the same variable x.
Incorrect! Try again.
47If you have an array of unions, what is the memory layout?
A.Contiguous blocks, each the size of the largest union member.
B.Contiguous blocks, each the size of the sum of union members.
C.Scattered memory.
D.Linked list.
Correct Answer: Contiguous blocks, each the size of the largest union member.
Explanation:An array allocates contiguous memory. Each element has the size of the union (which is the size of its largest member).
Incorrect! Try again.
48How do you define a structure type Person and a variable p1 in a single statement?
A.struct Person { ... } p1;
B.struct Person p1 { ... };
C.define struct Person { ... } p1;
D.Person p1 struct { ... };
Correct Answer: struct Person { ... } p1;
Explanation:You can declare the variable p1 immediately after the closing brace of the structure definition.
Incorrect! Try again.
49Which statement best describes sizeof(union U) where union U { double d; char c[10]; };?
Explanation:The size is at least the size of the largest member. Here, c is 10 bytes and d is usually 8. The size will be at least 10, but likely padded to 16 bytes for alignment (multiple of 8).
Incorrect! Try again.
50Is struct T var = {0}; a valid way to initialize all members to zero?
A.Yes, it is the universal zero initializer.
B.No, must list all members.
C.Only for integers.
D.No, it initializes only the first member to 0.
Correct Answer: Yes, it is the universal zero initializer.
Explanation:Initializing the first member to 0 causes all remaining members (implicit) to also be initialized to 0.
Incorrect! Try again.
Give Feedback
Help us improve by sharing your thoughts or reporting issues.