Which of the following is the correct way to pass a string to a function in C?
Avoid func(string s);
Bvoid func(char s[]);
Cvoid func(char *s);
DBoth B and C
Correct Answer:
D. Both B and C
EXPLANATION
Both char s[] (array notation) and char *s (pointer notation) are valid ways to pass strings to functions in C. C does not have a built-in string type; strings are represented as character arrays or pointers to char.
What happens when you try to access an array element beyond its size in C?
ACompilation error
BRuntime error with exception
CUndefined behavior
DReturns 0 automatically
Correct Answer:
C. Undefined behavior
EXPLANATION
C does not perform bounds checking on arrays. Accessing beyond array bounds results in undefined behavior - it may access garbage values, crash, or seem to work without error. This is a common source of bugs.
What is the difference between struct and union in C?
Astruct members share memory, union members have separate memory
Bunion members share memory, struct members have separate memory
CNo difference in memory allocation
Dstruct is faster than union
Correct Answer:
B. union members share memory, struct members have separate memory
EXPLANATION
In a struct, each member has its own memory allocation, so the total size is the sum of all members. In a union, all members share the same memory location, so the size equals the largest member. Only one member can hold a value at a time in a union.
Consider the following C code. What will be printed?
int arr[3][3] = {{1,2,3}, {4,5,6}, {7,8,9}};
int *ptr = (int *)arr;
printf("%d", *(ptr + 5));
A5
B6
C8
D9
Correct Answer:
B. 6
EXPLANATION
A 2D array is stored in row-major order in memory: 1,2,3,4,5,6,7,8,9. When ptr is cast to int*, ptr+5 points to the 6th element (0-indexed), which is 6.