Pointers Question Bank for C-CAT
Topic-wise Pointers MCQs for CDAC C-CAT preparation with answers and explanations.
Show Answer & Explanation
Correct Answer: B - Variable storing address
A pointer is a variable that stores the memory address of another variable.
Show Answer & Explanation
Correct Answer: A - &
The address-of operator (&) returns the memory address of a variable.
Show Answer & Explanation
Correct Answer: B - *
The dereference operator (*) accesses the value stored at the address.
What will be the output?
int x = 10;
int *p = &x;
printf("%d", *p);Show Answer & Explanation
Correct Answer: C - 10
Pointer p stores address of x and *p gives the value of x.
What will be the output?
int x = 5;
int *p = &x;
*p = 8;
printf("%d", x);Show Answer & Explanation
Correct Answer: C - 8
Changing *p modifies the original variable x.
Show Answer & Explanation
Correct Answer: C - Pointer pointing to 0 address
NULL pointer does not point to any valid memory location.
Show Answer & Explanation
Correct Answer: B - Undefined behavior
Dereferencing NULL pointer leads to runtime error or crash.
Show Answer & Explanation
Correct Answer: C - Pointer not initialized
Wild pointer points to an unknown memory location.
What will be the output?
int a = 10;
int *p = &a;
printf("%d", *p + 1);Show Answer & Explanation
Correct Answer: C - 11
Dereferenced value 10 is incremented by 1.
What will be the output?
int arr[3] = {1,2,3};
int *p = arr;
printf("%d", *p);Show Answer & Explanation
Correct Answer: A - 1
Array name stores base address, so *p accesses first element.
What will be the output?
int arr[3] = {1,2,3};
int *p = arr;
printf("%d", *(p+1));Show Answer & Explanation
Correct Answer: B - 2
Pointer arithmetic moves pointer to next element.
Show Answer & Explanation
Correct Answer: C - Data type size
Incrementing pointer moves by size of data type.
What will be the output?
int x = 5;
int *p = &x;
p++;
printf("%d", *p);Show Answer & Explanation
Correct Answer: B - Garbage
Pointer moves to next memory location which is not valid for x.
Show Answer & Explanation
Correct Answer: D - void pointer
void pointer is a generic pointer type.
Show Answer & Explanation
Correct Answer: D - Type casting
Void pointer must be typecasted before dereferencing.
Show Answer & Explanation
Correct Answer: D - Pointer to freed memory
Dangling pointer points to memory that has been deallocated.
What will be the output?
int a = 10;
int *p = &a;
int **q = &p;
printf("%d", **q);Show Answer & Explanation
Correct Answer: C - 10
Pointer to pointer dereferences twice to get value.
Show Answer & Explanation
Correct Answer: C - int **p;
Double asterisk declares pointer to pointer.
What will be the output?
int arr[5];
printf("%d", sizeof(arr));Show Answer & Explanation
Correct Answer: D - 20
Array of 5 integers occupies 20 bytes (assuming 4 bytes each).
What will be the output?
int arr[5];
int *p = arr;
printf("%d", sizeof(p));Show Answer & Explanation
Correct Answer: B - Depends on compiler
Pointer size depends on system architecture (32-bit/64-bit).
Show Answer & Explanation
Correct Answer: B - Initializing pointer to NULL
Initializing pointers avoids accidental access.
Show Answer & Explanation
Correct Answer: B - Pointer stores address
Pointers store memory addresses.
What will be the output?
int x = 10;
int *p = &x;
printf("%d", sizeof(*p));Show Answer & Explanation
Correct Answer: A - 4
sizeof(*p) is size of int.
Show Answer & Explanation
Correct Answer: D - int &p;
Reference operator (&) is not valid in C.
What will be the output?
int a = 5;
int *p = &a;
printf("%d", (*p)++);Show Answer & Explanation
Correct Answer: B - 5
Post-increment prints value first, then increments.
What is the output?
int x = 10;
int *p = &x;
*p = 25;
printf("%d", x);Show Answer & Explanation
Correct Answer: C - 25
p is a pointer to x. Dereferencing p with *p = 25 modifies the value stored at x's address. So x becomes 25. This demonstrates how pointers provide indirect access to variables.
What is the output?
int arr[] = {10, 20, 30, 40, 50};
int *p = arr;
printf("%d", *(p + 3));Show Answer & Explanation
Correct Answer: D - 40
p points to arr[0]. Adding 3 to p moves it forward by 3 integer positions (pointer arithmetic accounts for the size of int). So *(p + 3) is equivalent to arr[3], which is 40.
What is the output?
int a = 5, b = 10;
int *p = &a, *q = &b;
printf("%d", *p + *q);Show Answer & Explanation
Correct Answer: A - 15
*p dereferences p to get the value of a (5), and *q dereferences q to get the value of b (10). The expression *p + *q adds these values: 5 + 10 = 15.
Show Answer & Explanation
Correct Answer: A - A pointer that doesn't point to any valid memory location
A NULL pointer is a special pointer value that indicates the pointer doesn't point to any valid object or function. It is typically defined as ((void*)0). Dereferencing a NULL pointer causes undefined behavior, usually a segmentation fault.
What is the output?
int arr[] = {2, 4, 6, 8, 10};
int *p = arr + 4;
printf("%d", p[-2]);Show Answer & Explanation
Correct Answer: A - 6
p points to arr[4] (value 10). The expression p[-2] is equivalent to *(p - 2), which points to arr[4-2] = arr[2] = 6.
What is the output?
int x = 100;
int *p = &x;
int **pp = &p;
printf("%d", **pp);Show Answer & Explanation
Correct Answer: D - 100
pp is a pointer to pointer. *pp gives p (pointer to x), and **pp dereferences again to get the value of x, which is 100. This is a two-level indirection: pp → p → x.
int *p and int **p?Show Answer & Explanation
Correct Answer: B - *p is a pointer to int, **p is a pointer to a pointer to int
'int *p' declares p as a pointer that stores the address of an int variable. 'int **p' declares p as a pointer that stores the address of another pointer (which in turn points to an int). This allows two levels of indirection.
What is the output?
void modify(int **pp) {
static int val = 50;
*pp = &val;
}
int main() {
int x = 10;
int *p = &x;
modify(&p);
printf("%d", *p);
}Show Answer & Explanation
Correct Answer: D - 50
The function modify() receives a pointer to pointer. *pp = &val changes what p points to — now p points to the static variable val (50) instead of x. Since val is static, it persists after the function returns. *p now gives 50.
What is the output?
char *str = "Hello";
printf("%c", *(str + 1));Show Answer & Explanation
Correct Answer: D - e
str points to the first character 'H' of the string literal "Hello". str + 1 advances the pointer by one character position, pointing to 'e'. Dereferencing with * gives 'e'.
Show Answer & Explanation
Correct Answer: B - The address increases by sizeof(int) bytes
Pointer arithmetic in C is scaled by the size of the pointed-to type. Incrementing an int pointer by 1 advances the address by sizeof(int) bytes (typically 4 bytes), not by 1 byte. This ensures the pointer moves to the next integer in memory.
What is the output?
int arr[] = {1, 2, 3, 4, 5};
int *p = arr;
int *q = arr + 4;
printf("%ld", q - p);Show Answer & Explanation
Correct Answer: A - 4
When two pointers to the same array are subtracted, the result is the number of elements between them (not bytes). q points to arr[4] and p points to arr[0], so q - p = 4 elements.
Show Answer & Explanation
Correct Answer: C - int (*fptr)(int, int);
'int (*fptr)(int, int);' correctly declares fptr as a pointer to a function that takes two int parameters and returns int. The parentheses around (*fptr) are essential. Without them, 'int *fptr(int, int)' declares a function returning int*.
What is the output?
int a = 10, b = 20, c = 30;
int *arr[3] = {&a, &b, &c};
printf("%d", *arr[1]);Show Answer & Explanation
Correct Answer: C - 20
arr is an array of 3 int pointers. arr[0] = &a, arr[1] = &b, arr[2] = &c. *arr[1] dereferences arr[1] (which holds &b) to give the value of b, which is 20.
What is the output?
void increment(int *p) {
(*p)++;
}
int main() {
int x = 7;
increment(&x);
increment(&x);
printf("%d", x);
}Show Answer & Explanation
Correct Answer: A - 9
Each call to increment() dereferences p and increments the value by 1. After first call: x = 8. After second call: x = 9. The pointer provides pass-by-reference semantics.
What is the output?
char str[] = "CDAC Exam";
char *p = str;
while(*p != '\0') {
if(*p == ' ') {
printf("_");
} else {
printf("%c", *p);
}
p++;
}Show Answer & Explanation
Correct Answer: A - CDAC_Exam
The pointer p traverses each character of the string. When it encounters a space character ' ', it prints underscore '_' instead. All other characters are printed as-is. So 'CDAC Exam' becomes 'CDAC_Exam'.
What is the output?
int x = 5;
int *p = &x;
int **q = &p;
**q = 20;
printf("%d %d %d", x, *p, **q);Show Answer & Explanation
Correct Answer: C - 20 20 20
**q = 20 modifies the value at the location pointed to by the pointer that q points to. Since q → p → x, **q = 20 sets x = 20. Now x, *p, and **q all refer to the same memory location containing 20.
What is wrong with this code?
int *p;
*p = 10;
printf("%d", *p);Show Answer & Explanation
Correct Answer: A - Dereferencing an uninitialized (wild) pointer — undefined behavior
The pointer p is declared but not initialized — it contains a garbage address. Dereferencing it with *p = 10 writes to an unknown memory location, causing undefined behavior (likely a segmentation fault). Always initialize pointers before use.
What is the output?
int arr[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int *p = &arr[0][0];
printf("%d", *(p + 5));Show Answer & Explanation
Correct Answer: D - 6
A 2D array is stored in row-major order in memory: 1,2,3,4,5,6,7,8,9. p points to arr[0][0] (value 1). p+5 points to the 6th element, which is 6. So *(p+5) = 6.
What is the output?
int x = 10;
int *p1 = &x;
int *p2 = &x;
printf("%d", (p1 == p2));Show Answer & Explanation
Correct Answer: B - 1
Both p1 and p2 point to the same variable x, so they hold the same address. Comparing them with == returns 1 (true). Pointer comparison checks if two pointers point to the same memory location.
What is the output?
void allocate(int **pp) {
*pp = (int*)malloc(sizeof(int));
**pp = 42;
}
int main() {
int *p = NULL;
allocate(&p);
printf("%d", *p);
free(p);
}Show Answer & Explanation
Correct Answer: A - 42
The function allocate() receives a pointer to pointer. *pp = malloc() makes the original pointer p in main() point to newly allocated memory. **pp = 42 stores 42 in that memory. Back in main(), *p gives 42. The memory is properly freed afterward.
arr[i] translate to internally in C?Show Answer & Explanation
Correct Answer: A - *(arr + i)
In C, arr[i] is syntactic sugar for *(arr + i). The array name arr is a pointer to the first element. Adding i performs pointer arithmetic to reach the i-th element, and * dereferences it. This also means i[arr] is valid and equivalent!
What is the output?
void swap(int **a, int **b) {
int *temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 5, y = 10;
int *px = &x, *py = &y;
swap(&px, &py);
printf("%d %d", *px, *py);
}Show Answer & Explanation
Correct Answer: D - 10 5
The function swaps the pointers px and py (not the values they point to). After swap, px points to y and py points to x. So *px = 10 and *py = 5. The original variables x and y are unchanged, but the pointers now reference each other's target.
What is the output?
const int *p;
int x = 10, y = 20;
p = &x;
printf("%d ", *p);
p = &y;
printf("%d", *p);Show Answer & Explanation
Correct Answer: A - 10 20
'const int *p' means the value pointed to cannot be changed via p, but p itself can be reassigned. p first points to x (10), then is reassigned to point to y (20). Both dereferences are reads (allowed with const). Output: 10 20.
What is the output?
char *ptr;
char str[] = "Programming";
ptr = str;
ptr += 3;
printf("%s", ptr);Show Answer & Explanation
Correct Answer: D - gramming
ptr initially points to str[0] ('P'). After ptr += 3, it points to str[3] ('g'). Printing with %s from that position gives the substring starting from 'g': "gramming".
What is the output?
int compute(int a, int b) { return a + b; }
int process(int a, int b) { return a * b; }
int main() {
int (*fp)(int, int);
fp = compute;
printf("%d ", fp(3, 4));
fp = process;
printf("%d", fp(3, 4));
}Show Answer & Explanation
Correct Answer: B - 7 12
fp is a function pointer. First it points to compute: fp(3,4) = 3+4 = 7. Then it's reassigned to process: fp(3,4) = 3*4 = 12. Function pointers enable runtime polymorphism in C. Output: 7 12.