1Which keyword is used to declare a structure in C?
A.structure
B.struct
C.type
D.struc
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.struct emp Employee;
B.struct Employee emp;
C.new struct Employee emp;
D.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.& (ampersand)
B.-> (arrow operator)
C.. (dot operator)
D.* (asterisk)
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 cannot contain arrays.
B.It must contain only integers.
C.It defines a new data type but does not allocate memory.
D.It allocates memory for variables.
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.Declaration of a structure type
B.Definition of a structure variable
C.A syntax error due to the semicolon
D.Initialization of a structure
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.Dynamic initialization
B.Compile-time initialization
C.Copy initialization
D.Run-time 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, but only as a pointer.
B.Yes, directly.
C.No, never.
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.5 bytes
B.8 bytes
C.4 bytes
D.1 byte
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.s->age
D.p.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?
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 function returning a structure.
B.An array of 5 structures.
C.A structure containing an array.
D.A pointer to 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; };
B.struct s { [10]int arr; };
C.struct s { array int arr; };
D.struct s { int arr[10]; };
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.Undefined/Garbage
B.2.5
C.10
D.0
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.Yes, if they are initialized.
C.No, C does not support operator overloading for structs.
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.Adding comments to code.
C.Removing unused variables.
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.Linked Lists
B.Unions
C.Arrays
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, if casted.
B.No, unless typedef is used.
C.Yes, because they have the same members.
D.No, they are distinct types.
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[2].arr
B.s->arr[2]
C.s.arr[2]
D.s.arr.2
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.4
B.1
C.Compilation Error (or undefined in pure C standard)
D.0
Correct Answer: 0
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;
B.union Data d(10);
C.union Data d = {10};
D.union Data d = {10, 20.5};
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.Only in Unions
B.No
C.Only if it is an integer
D.Yes
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.Syntax error.
B.Accessing member of a pointer variable.
C.Dereferencing a pointer to a structure and accessing the member.
D.Multiplying ptr by member.
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.'b'
B.'a'
C.1
D.2
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.Only if the union is named
C.Only if the structure is named
D.No
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.8 bytes
B.4 bytes
C.5 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 that is hidden.
B.A structure without a tag name.
C.A structure without members.
D.A structure without a variable.
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.Overlapping at the same start address
B.Randomly
C.Sequentially
D.In a linked list
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(b)
B.sizeof(Box)
C.sizeof(*b)
D.sizeof(struct Box)
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.Only if it contains no arrays
B.Yes
C.Only if it is small
D.No
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 allows the structure to be immutable.
B.It is syntactically required.
C.It avoids copying the entire structure data, saving memory and time.
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.Undefined
C.0
D.10
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.pointer struct S ptr();
D.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.Array size 4
B.Byte alignment of 4
C.Initialization to 4
D.Bit-field of width 4 bits
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.alignof
B.sizeof
C.typeof
D.offsetof
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.u->member
D.p.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.Only for strings
B.Yes, e.g., scanf("%d", s.age);
C.Yes, e.g., scanf("%d", &s.age);
D.No
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.b is initialized to 0
B.b is uninitialized (garbage)
C.Compile error
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.Compiler dependent entirely
B.In order of declaration (c then i)
C.Largest to smallest (i then c)
D.Smallest to largest (c then i)
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 that changes type.
B.A pointer.
C.An array inside a struct with size 0 or undefined [], declared as the last member.
D.An array declared in a union.
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.Only in C++
B.No
C.Only if they are defined in same scope
D.Yes
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 the same address.
B.Copying is invalid for pointers.
C.s2.p is null.
D.They point to different addresses.
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 sum of union members.
B.Scattered memory.
C.Contiguous blocks, each the size of the largest union member.
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.define struct Person { ... } p1;
B.struct Person p1 { ... };
C.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]; };?
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.