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.82Hard
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.83Hard
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.84Hard
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.85Hard
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.
Advertisement
Q.86Hard
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.87Hard
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.88Hard
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.89Hard
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.90Hard
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.91Hard
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.92Hard
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.
Q.93Hard
What is the difference between *p++ and (*p)++?
Answer: B
Due to operator precedence, *p++ is equivalent to *(p++), incrementing the pointer. (*p)++ explicitly increments the value.
Q.94Hard
What does the following code do?
int *p = (int*)malloc(sizeof(int));
*p = 5;
free(p);
p = NULL;
Answer: A
This is proper memory management: allocate, use, free, and nullify to prevent dangling pointer.
Q.95Hard
Which statement is true about void pointers?
Answer: A
void pointers are generic pointers that must be cast to appropriate type before dereferencing.
Q.96Hard
What is the output?
int x = 50;
int *p = &x;
int *q = p;
q = NULL;
printf("%d", *p);
Answer: A
Setting q to NULL doesn't affect p. p still points to x, so *p is 50.
Q.97Hard
In a function that returns a pointer, which of the following is UNSAFE?
Answer: B
Returning a pointer to a local variable is unsafe because the variable ceases to exist after the function returns, creating a dangling pointer.
Q.98Hard
Consider: int *p = (int *)malloc(5 * sizeof(int)); for(int i=0; i<5; i++) *(p+i) = i*10; Which statement correctly deallocates this memory?
Answer: B
malloc() allocated memory must be freed using free(p), not free(&p). After freeing, it's good practice to set p = NULL.
Q.99Hard
A programmer uses a pointer variable but forgets to initialize it before dereferencing. Which type of error will occur?
Answer: C
Uninitialized pointers contain garbage values. Dereferencing them accesses arbitrary memory locations, causing runtime errors or undefined behavior.
Q.100Hard
What is the relationship between arrays and pointers in C?
Answer: C
In most contexts, array names automatically decay to pointers to their first element. This is why array[i] is equivalent to *(array+i).