What is the difference between *p++ and (*p)++?
A No difference B *p++ increments pointer, (*p)++ increments value C (*p)++ increments pointer, *p++ increments value D Syntax error
Due to operator precedence, *p++ is equivalent to *(p++), incrementing the pointer. (*p)++ explicitly increments the value.
What does the following code do? int *p = (int*)malloc(sizeof(int)); *p = 5; free(p); p = NULL;
A Allocates, assigns, deallocates and nullifies pointer B Creates a memory leak C Causes segmentation fault D Compilation error
This is proper memory management: allocate, use, free, and nullify to prevent dangling pointer.
Which statement is true about void pointers?
A Cannot be dereferenced without casting B Can only point to void type C Are always NULL D Cannot be incremented
void pointers are generic pointers that must be cast to appropriate type before dereferencing.
What is the output? int x = 50; int *p = &x; int *q = p; q = NULL; printf("%d", *p);
A 50 B NULL C Garbage value D Segmentation fault
Setting q to NULL doesn't affect p. p still points to x, so *p is 50.
In a function that returns a pointer, which of the following is UNSAFE?
A Returning a pointer to dynamically allocated memory (malloc) B Returning a pointer to a local variable C Returning a pointer to a global variable D Returning a pointer to a static variable
Returning a pointer to a local variable is unsafe because the variable ceases to exist after the function returns, creating a dangling pointer.
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?
A delete p; B free(p); C free(&p); D p = NULL;
malloc() allocated memory must be freed using free(p), not free(&p). After freeing, it's good practice to set p = NULL.
A programmer uses a pointer variable but forgets to initialize it before dereferencing. Which type of error will occur?
A Compile-time error B Syntax error C Runtime error or undefined behavior D Logical error
Uninitialized pointers contain garbage values. Dereferencing them accesses arbitrary memory locations, causing runtime errors or undefined behavior.
What is the relationship between arrays and pointers in C?
A Arrays are pointers B Pointers are arrays C Array names decay to pointers to their first element in most contexts D No relationship
In most contexts, array names automatically decay to pointers to their first element. This is why array[i] is equivalent to *(array+i).
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));
A 5 B 6 C 8 D Garbage value
Converting 2D array to int pointer treats it as 1D. p+5 points to the 6th element (0-indexed), which is 6.
What does realloc() do?
A Allocates new memory block B Resizes an existing allocated memory block and returns new address C Frees memory and allocates new D Initializes memory to zero
realloc() changes the size of previously allocated memory. It may return the same address or a new one depending on availability.
What is the primary use of const pointer (int * const p)?
A Prevent modification of the value pointed to B Prevent modification of the pointer itself C Prevent allocation of memory D Prevent deallocation of memory
int * const p is a constant pointer - the pointer address cannot be changed, but the value it points to can be modified.
Consider: int arr[] = {1,2,3,4,5}; int *p = arr + 2; What is arr - p?
A -2 B 2 C Address difference D Compilation error
arr points to arr[0], p points to arr[2]. arr - p = -2 (p is 2 positions ahead).
What is malloc(0) likely to return?
A NULL pointer B Valid pointer to 0 bytes C Garbage value D Compilation error
malloc(0) behavior is implementation-defined but typically returns a valid pointer. Freeing it is safe.
What will be printed? int *p = (int*)malloc(5 * sizeof(int)); int *q = p; p = NULL; free(q);
A Compilation error B Memory leak C Program runs successfully D Double free error
q still holds the address even though p is NULL. free(q) properly deallocates memory.
What is the difference between arr and &arr if arr is an array? int arr[5];
A Both are identical B arr is pointer to first element, &arr is pointer to whole array C &arr is pointer to first element, arr is pointer to whole array D They have different sizes
arr decays to pointer to first element (int*). &arr is pointer to whole array (int(*)[5]). Pointer arithmetic differs.
In pointer to function: int (*ptr)(int, int); What does this declare?
A Function returning pointer to int B Pointer to function returning int with 2 int parameters C Array of pointers D Pointer to array of ints
Parentheses around *ptr give pointer priority. It's a pointer to a function taking 2 ints and returning int.
What is the output of: printf("%p", NULL);?
A 0x0 B NULL C Implementation-defined (typically 0x0 or empty) D Compilation error
%p prints pointer values. NULL representation depends on implementation, commonly shown as 0x0 or (nil).
For dynamic 2D array: int arr = (int )malloc(n * sizeof(int*)); What's missing?
A Nothing, code is complete B Allocating memory for each row C Casting malloc result D Type checking
This allocates row pointers only. Each row needs allocation: arr[i] = (int*)malloc(m * sizeof(int));
What is the relationship between arrays and pointers?
A Arrays are pointers B Pointers are arrays C Array name decays to pointer in most contexts D They are unrelated
Array name acts as pointer to first element in expressions but they're distinct. &arr and arr differ in type.
Consider: const int *p; and int * const q; Which statement is true?
A Both prevent modification of data B First prevents pointer change, second prevents data change C First prevents data change, second prevents pointer change D Both are identical
const int *p: pointer can change, data cannot. int * const q: pointer cannot change, data can be modified.