Showing 11–20 of 100 questions
in Pointers
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
Correct Answer:
C. Array name decays to pointer in most contexts
EXPLANATION
Array name acts as pointer to first element in expressions but they're distinct. &arr and arr differ in type.
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
Correct Answer:
B. Allocating memory for each row
EXPLANATION
This allocates row pointers only. Each row needs allocation: arr[i] = (int*)malloc(m * sizeof(int));
What is the output of: printf("%p", NULL);?
A
0x0
B
NULL
C
Implementation-defined (typically 0x0 or empty)
D
Compilation error
Correct Answer:
C. Implementation-defined (typically 0x0 or empty)
EXPLANATION
%p prints pointer values. NULL representation depends on implementation, commonly shown as 0x0 or (nil).
In: int *p; *p = 10; What is the issue?
A
Syntax error
B
p is uninitialized (wild pointer)
C
Memory leak
D
Type mismatch
Correct Answer:
B. p is uninitialized (wild pointer)
EXPLANATION
p is not initialized to valid address. Dereferencing causes undefined behavior (writing to random memory location).
Which of the following is valid pointer comparison?
A
p > 5
B
p == q (different types)
C
p < q (valid pointers)
D
p + 10
Correct Answer:
C. p < q (valid pointers)
EXPLANATION
Pointers of same type can be compared using <, >, ==, !=. Comparing with integers or different types is invalid.
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.
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
Correct Answer:
B. Pointer to function returning int with 2 int parameters
EXPLANATION
Parentheses around *ptr give pointer priority. It's a pointer to a function taking 2 ints and returning int.
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).
For function: void modify(int *x) { *x = 20; } If called as modify(&a), what changes?
A
The pointer itself
B
The value of variable a
C
The function parameter
D
Nothing
Correct Answer:
B. The value of variable a
EXPLANATION
Passing address allows function to modify original variable. *x = 20 changes the value at that address.
Which scenario demonstrates a dangling pointer?
A
Pointer declared but not initialized
B
Pointer freed but still used afterward
C
Pointer set to NULL
D
Pointer pointing to static variable
Correct Answer:
B. Pointer freed but still used afterward
EXPLANATION
Dangling pointer occurs when memory is freed but pointer still references that location. This causes undefined behavior.