What will be the output?
int a[] = {10, 20, 30};
int *p = a;
printf("%d", *(p+2));
Answer: C
p points to a[0], p+2 points to a[2] which contains 30. Dereferencing gives 30.
Q.42Medium
What does calloc() do that malloc() doesn't?
Answer: B
calloc() takes two parameters (number of elements, size) and initializes all bytes to zero, while malloc() leaves memory uninitialized.
Q.43Medium
What is the output of this code?
int *p, *q;
int x = 5;
p = &x;
q = p;
printf("%d %d", *p, *q);
Answer: A
Both p and q point to the same variable x. Dereferencing both gives the value 5.
Q.44Medium
Which statement correctly declares a pointer to a pointer?
Answer: B
int **p declares a pointer to a pointer (double pointer). Each * adds one level of indirection.
Q.45Medium
What will be printed?
int x = 100, y = 200;
int *p = &x;
int *q = &y;
p = q;
printf("%d", *p);
Answer: B
p is reassigned to point to y (p = q). Dereferencing p now gives the value of y, which is 200.
Advertisement
Q.46Medium
What is the primary disadvantage of using free() without proper checks?
Answer: B
Calling free() on already freed memory or using a pointer after freeing it causes double-free errors and use-after-free bugs, leading to undefined behavior.
Q.47Medium
What will be the output?
char *str = "Hello";
printf("%c", *(str+1));
Answer: B
str points to 'H', str+1 points to 'e'. Dereferencing gives 'e'.
Q.48Hard
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).
Q.49Hard
What will be the output?
int arr[3][3] = {{1,2,3}, {4,5,6}, {7,8,9}};
int *p = (int *)arr;
printf("%d", *(p+5));
Answer: B
Converting 2D array to int pointer treats it as 1D. p+5 points to the 6th element (0-indexed), which is 6.
Q.50Hard
What does realloc() do?
Answer: B
realloc() changes the size of previously allocated memory. It may return the same address or a new one depending on availability.
Q.51Medium
What is the output?
void func(int *arr) { arr[0] = 100; }
int main() {
int a[5] = {1,2,3,4,5};
func(a);
printf("%d", a[0]);
}
Answer: B
Arrays are passed by reference (as pointers). Changes made in func() affect the original array. a[0] becomes 100.
Q.52Medium
Which of the following is valid pointer arithmetic in C?
Answer: A
Valid pointer arithmetic includes addition/subtraction with integers and subtraction between pointers. Multiplication, division, and pointer addition are invalid.
Q.53Hard
What is the primary use of const pointer (int * const p)?
Answer: B
int * const p is a constant pointer - the pointer address cannot be changed, but the value it points to can be modified.
Q.54Easy
What is the output of the following code?
int x = 5;
int *p = &x;
int q = &p;
printf("%d", q);
Answer: A
q is a pointer to pointer p. **q dereferences p twice, giving the value of x which is 5.
Q.55Medium
Which of the following correctly declares a pointer to a function that takes two integers and returns an integer?
Answer: A
int (*ptr)(int, int) declares a pointer to a function. int *ptr(int, int) declares a function returning pointer to int.
Q.56Easy
What will be the size of the pointer variable on a 64-bit system?
int *ptr;
Answer: B
On a 64-bit system, all pointers are 8 bytes regardless of the data type they point to.
Q.57Easy
What is the output?
int arr[] = {10, 20, 30};
int *p = arr;
printf("%d %d", *(p+1), arr[1]);
Answer: A
p+1 points to second element (20). arr[1] is also 20. Both print the same value.
Q.58Medium
What happens when you try to modify a string literal through a pointer?
char *str = "Hello";
str[0] = 'J';
Answer: B
String literals are stored in read-only memory. Attempting to modify causes undefined behavior or segmentation fault.
Q.59Medium
Consider the code:
int *p = NULL;
int *q = malloc(sizeof(int));
free(q);
free(q);
What is the issue here?
Answer: A
Freeing the same pointer twice causes double free error and undefined behavior. Should set q=NULL after first free.
Q.60Medium
What is the output?
int a = 10;
int *p = &a;
int *q = p;
q = NULL;
printf("%d", *p);
Answer: A
q = NULL only changes q, not p. p still points to a, so *p prints 10.