Showing 1–10 of 28 questions
in Pointers
What is the primary issue with this code?
int *p;
*p = 10;
A
Syntax error
B
Pointer p is uninitialized and points to garbage memory
C
The value 10 cannot be assigned through a pointer
D
Missing return statement
Correct Answer:
B. Pointer p is uninitialized and points to garbage memory
EXPLANATION
p is declared but not initialized to point to any valid memory location. Dereferencing an uninitialized pointer causes undefined behavior.
Consider the declaration: const int *p; and int * const q;
Which statement is TRUE?
A
Both p and q cannot be modified
B
p points to a constant integer; q is a constant pointer to integer
C
Both declarations are identical
D
Both p and q can be modified freely
Correct Answer:
B. p points to a constant integer; q is a constant pointer to integer
EXPLANATION
const int *p means p can change but the data it points to cannot. int * const q means q cannot change but the data it points to can be modified.
What is the output of the following code?
int x = 10;
int *p = &x;
int **q = &p;
printf("%d", **q);
A
10
B
Address of x
C
Address of p
D
Garbage value
EXPLANATION
q is a pointer to pointer p. **q dereferences q twice: first to get p, then to get the value of x which is 10.
What happens with: int arr[10]; int *p = arr; *p = 5; printf("%d", arr[0]);?
A
Prints garbage
B
Prints 5
C
Compiler error
D
Prints 10
Correct Answer:
B. Prints 5
EXPLANATION
p points to arr[0]. *p = 5 modifies arr[0]. Printing arr[0] displays 5.
What is the result of: int x = 5; int *p = &x; int *q = p; if p == q?
A
0 (false)
B
1 (true)
C
Undefined
D
Compiler error
Correct Answer:
B. 1 (true)
EXPLANATION
Both p and q point to same address of x, so p == q evaluates to true (1).
How does free() handle a NULL pointer?
A
Causes segmentation fault
B
Does nothing (safe operation)
C
Returns error code
D
Undefined behavior
Correct Answer:
B. Does nothing (safe operation)
EXPLANATION
free(NULL) is safe in C and does nothing. This is by design to prevent errors when freeing pointers.
What is the size of a pointer in a 64-bit system?
A
2 bytes
B
4 bytes
C
8 bytes
D
16 bytes
Correct Answer:
C. 8 bytes
EXPLANATION
In 64-bit systems, pointers are 8 bytes (64 bits) regardless of the data type they point to.
Which of the following correctly declares a pointer to a pointer?
A
int **ptr;
B
int *&ptr;
C
int &&ptr;
D
int ***ptr;
Correct Answer:
A. int **ptr;
EXPLANATION
Double pointer (int **ptr) is the correct syntax for pointer to pointer. Single & is used in C++ references, not C.
What is the output?
char str[] = "ABC";
char *p = str;
printf("%c", *(p+2));
A
A
B
B
C
C
D
Null character
EXPLANATION
p+2 points to str[2] which is 'C'. Note: str[3] is null terminator.
What is the output?
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int x = 5, y = 10;
swap(&x, &y);
printf("%d %d", x, y);
A
5 10
B
10 5
C
10 10
D
5 5
EXPLANATION
swap() exchanges values through pointers. x becomes 10, y becomes 5 after function call.