Consider a 2D array int matrix[3][3]. If we access matrix as a 1D array, how many total elements can we access?
Answer: C
2D array is stored contiguously in memory. 3x3 array has 9 elements total, accessible as a 1D array of 9 elements.
Q.2Hard
Consider: int arr[3][4][5]. What is the size of arr[0][0]?
Answer: A
arr[0][0] is a 1D array of 5 integers. Each int is 4 bytes, so total is 5×4=20 bytes.
Q.3Hard
Which of the following is true about strcpy(dest, src)?
Answer: B
strcpy() doesn't check dest buffer size, making it unsafe. It can cause buffer overflow if src is larger than dest. Modern compilers recommend strncpy() or strlcpy().
Q.4Hard
What is the main difference between char arr[] = "Test" and char *ptr = "Test"?
Answer: A
arr creates a modifiable array copy of the string. ptr points to a string literal (read-only in memory). Modifying *ptr leads to undefined behavior.
Q.5Hard
If ptr is a pointer to int array and ptr = arr, what does ptr[3] represent?
Answer: D
ptr[3] is equivalent to arr[3] and accesses the element at address *(ptr+3). For int*, ptr+3 moves 12 bytes (3×4 bytes) in memory.
Advertisement
Q.6Hard
What is the correct way to safely copy strings in modern C?
Answer: B
strncpy with buffer size check and explicit null termination is the standard safe approach. Option A lacks bounds checking; C and D have issues with null termination.
Q.7Hard
In the declaration int *arr[10], what is arr?
Answer: A
int *arr[10] declares an array of 10 pointers to int (subscript binds tighter than dereference). Use int (*arr)[10] for pointer to array.
Q.8Hard
In a dynamic 2D array created as: int arr = (int)malloc(rows * sizeof(int*)); What is the next step?
Answer: B
After allocating the array of pointers, each row must be allocated separately.
Q.9Hard
What will happen?
char arr[5] = "Hello";
printf("%d", strlen(arr));
Answer: C
Array is too small. "Hello" needs 6 bytes including null terminator, but only 5 are allocated. This causes buffer overflow.
Q.10Hard
What happens with this code?
char *ptr;
char str[] = "Programming";
ptr = str;
ptr[2] = 'X';
Answer: B
ptr points to the character array str. Modifying ptr[2] modifies str[2], changing 'o' to 'X', resulting in 'PrXgramming'.
Q.11Hard
What will the following code print?
int arr[3] = {10, 20, 30};
int *p = arr;
printf("%d %d", *p++, *++p);
Answer: D
This code exhibits undefined behavior due to lack of sequence points between modifications and access of p. Different compilers may produce different results.
Q.12Hard
How should dynamic 1D array of strings be declared for 5 strings of length 20?
Answer: A
For dynamic allocation, use double pointer char** and allocate memory for each string separately. Options B and C are static allocations.
Q.13Hard
What is the most efficient way to copy one array to another in C?
Answer: A
memcpy() is the most efficient as it performs block memory copy. strcpy() is only for strings, and array assignment doesn't work in C.
Q.14Hard
What is the relationship between pointer arithmetic and array indexing in C?
Answer: B
C standard defines arr[i] as *(arr + i). Both notations access the same memory location and perform identically.
Q.15Hard
What potential issue exists with this code: char *ptr = "Hello"; ptr[0] = 'h';?
Answer: B
String literals are stored in read-only memory. Attempting to modify them causes undefined behavior or segmentation fault at runtime.
Q.16Hard
For a 3D array int arr[2][3][4], what is arr[1][2][3] equivalent to?
Answer: B
In row-major order: address = base + (i*m*n + j*n + k) where m=3, n=4. So arr[1][2][3] = base + 1*12 + 2*4 + 3 = base + 23.
Q.17Hard
In string handling, what is the difference between fgets() and gets()?
Answer: C
gets() has no buffer overflow protection and was removed in C11. fgets(str, size, stdin) is the safer alternative with size limiting.
Q.18Hard
Which of the following correctly declares a pointer to an array (not array of pointers)?
Answer: B
Parentheses change precedence. int (*ptr)[5] is pointer to array of 5 ints. int *arr[5] is array of 5 pointers.
Q.19Hard
In what scenario would you use memcpy() instead of strcpy()?
Answer: B
strcpy() assumes null-terminated strings. memcpy() copies exact number of bytes, suitable for binary data or embedded nulls.
Q.20Hard
What does the strspn() function do?
Answer: B
strspn(str, charset) returns length of initial segment of str containing only characters from charset. Useful for token parsing.
C Programming MCQs & Practice Tests – Free | iGET | iGET