Showing 81–90 of 100 questions
in Pointers
What is the size of a pointer variable on a 32-bit system?
A
2 bytes
B
4 bytes
C
8 bytes
D
Depends on the data type
Correct Answer:
B. 4 bytes
EXPLANATION
On a 32-bit system, all pointers are 4 bytes (32 bits) regardless of the data type they point to.
A pointer variable stores the _____ of another variable.
A
value
B
memory address
C
data type
D
size
Correct Answer:
B. memory address
EXPLANATION
Pointers store memory addresses of variables. The address-of operator (&) retrieves this address.
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
EXPLANATION
Setting q to NULL doesn't affect p. p still points to x, so *p is 50.
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
Correct Answer:
A. Cannot be dereferenced without casting
EXPLANATION
void pointers are generic pointers that must be cast to appropriate type before dereferencing.
What will be the output?
int arr[] = {10, 20, 30};
int *p = arr;
printf("%d %d", arr[1], *(p+1));
A
20 20
B
20 10
C
10 20
D
30 20
EXPLANATION
arr[1] and *(p+1) both access the second element of the array, which is 20.
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
Correct Answer:
A. Allocates, assigns, deallocates and nullifies pointer
EXPLANATION
This is proper memory management: allocate, use, free, and nullify to prevent dangling pointer.
What is a dangling pointer?
A
Pointer pointing to freed memory
B
NULL pointer
C
Uninitialized pointer
D
Pointer declared outside main()
Correct Answer:
A. Pointer pointing to freed memory
EXPLANATION
A dangling pointer is a pointer that points to memory that has been freed or deallocated.
Which of the following correctly allocates memory for 10 integers?
A
int *p = malloc(10);
B
int *p = malloc(10 * sizeof(int));
C
int *p = malloc(sizeof(int));
D
int *p = alloc(10);
Correct Answer:
B. int *p = malloc(10 * sizeof(int));
EXPLANATION
malloc requires the size in bytes. For 10 integers, we need 10 * sizeof(int) bytes.
What is the output?
int *p = NULL;
if(p) printf("Not NULL");
else printf("NULL");
A
Not NULL
B
NULL
C
Segmentation fault
D
Compilation error
EXPLANATION
NULL pointer evaluates to false in a conditional, so the else block executes.
What is a wild pointer?
A
Pointer pointing to NULL
B
Uninitialized pointer with garbage address
C
Pointer in a wildlife program
D
Pointer declared inside a loop
Correct Answer:
B. Uninitialized pointer with garbage address
EXPLANATION
A wild pointer is an uninitialized pointer that contains an arbitrary/garbage memory address.