Showing 91–100 of 100 questions
in Structures & Unions
How do you access a member of a structure using a pointer?
A
ptr.member
B
ptr->member
C
*ptr.member
D
ptr*member
Correct Answer:
B. ptr->member
EXPLANATION
The arrow operator (->) is used to access structure members through a pointer. Dot (.) is for direct variables.
What will be printed?
struct s { int a; char b; int c; }; printf("%lu", sizeof(struct s));
A
9 bytes
B
12 bytes
C
13 bytes
D
8 bytes
Correct Answer:
B. 12 bytes
EXPLANATION
Due to memory alignment/padding: int a(4) + char b(1) + 3 padding bytes + int c(4) = 12 bytes.
What is the difference between struct and typedef struct?
A
No difference, both are identical
B
typedef struct allows direct variable declaration without 'struct' keyword
C
struct is faster than typedef struct
D
typedef struct is used only in C++
Correct Answer:
B. typedef struct allows direct variable declaration without 'struct' keyword
EXPLANATION
typedef struct creates an alias, so you can declare variables directly using the alias name without repeating 'struct'.
Which of the following correctly declares a pointer to a structure?
A
struct emp *ptr;
B
*struct emp ptr;
C
struct *emp ptr;
D
emp struct *ptr;
Correct Answer:
A. struct emp *ptr;
EXPLANATION
Correct syntax is 'struct typename *pointerName;'. Option A follows proper declaration syntax.
What happens when you initialize a structure variable without assigning values?
A
Members are automatically set to 0
B
Members contain garbage values
C
Compilation error occurs
D
Members are set to NULL
Correct Answer:
B. Members contain garbage values
EXPLANATION
Uninitialized local structure variables contain garbage values. Global structures are automatically initialized to zero.
Can we pass a structure variable to a function in C?
A
No, structures cannot be passed
B
Yes, by value only
C
Yes, by value or by reference using pointers
D
Only through global variables
Correct Answer:
C. Yes, by value or by reference using pointers
EXPLANATION
Structures can be passed by value (copy of structure) or by reference (using pointers to structure).
What is the size of the following structure?
struct point { int x; int y; float z; };
A
9 bytes
B
12 bytes
C
8 bytes
D
16 bytes
Correct Answer:
B. 12 bytes
EXPLANATION
int x = 4 bytes, int y = 4 bytes, float z = 4 bytes. Total = 12 bytes (no padding in this case on most systems).
Which keyword is used to define a structure in C?
A
class
B
struct
C
record
D
object
Correct Answer:
B. struct
EXPLANATION
The 'struct' keyword is used to define structures in C. 'class' is used in C++ and Java.
What will be the output of sizeof(union test) if union has members: int a, char b, float c?
A
4 bytes
B
7 bytes
C
9 bytes
D
11 bytes
Correct Answer:
A. 4 bytes
EXPLANATION
Union size equals the size of its largest member. Here, both int and float are 4 bytes (largest), so sizeof(union test) = 4 bytes.
What is the primary difference between a structure and a union in C?
A
Structure allocates memory for all members, union shares memory among members
B
Union allocates memory for all members, structure shares memory
C
Both allocate same memory but structure is faster
D
There is no difference, they are synonyms
Correct Answer:
A. Structure allocates memory for all members, union shares memory among members
EXPLANATION
In structures, each member gets its own memory space. In unions, all members share the same memory location, so only one member can hold a value at a time.