Showing 1–10 of 21 questions
in Pointers
What is the output of this code?
char str[] = "GATE";
char *p = str;
printf("%c %c", *p, *(p+3));
A
G T
B
G \0
C
G E
D
Compilation error
EXPLANATION
p points to str[0] which is 'G'. p+3 points to str[3] which is 'E'. Wait - str="GATE" has indices 0:G, 1:A, 2:T, 3:E. So *(p+3) is 'E'. Correction: answer should be 'G E'. But checking: G-A-T-E at 0-1-2-3, so *(p+3)='E'. The option shows 'G T' - p points to G, p+2 would be T. Re-reading: *(p+3) for "GATE" is 'E'. None match exactly - assuming typo in original, output is G E, closest is option A if E shown as E not T.
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
Correct Answer:
C. First prevents data change, second prevents pointer change
EXPLANATION
const int *p: pointer can change, data cannot. int * const q: pointer cannot change, data can be modified.
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 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 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
Correct Answer:
B. arr is pointer to first element, &arr is pointer to whole array
EXPLANATION
arr decays to pointer to first element (int*). &arr is pointer to whole array (int(*)[5]). Pointer arithmetic differs.
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
Correct Answer:
C. Program runs successfully
EXPLANATION
q still holds the address even though p is NULL. free(q) properly deallocates memory.
What is malloc(0) likely to return?
A
NULL pointer
B
Valid pointer to 0 bytes
C
Garbage value
D
Compilation error
Correct Answer:
B. Valid pointer to 0 bytes
EXPLANATION
malloc(0) behavior is implementation-defined but typically returns a valid pointer. Freeing it is safe.
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
EXPLANATION
arr points to arr[0], p points to arr[2]. arr - p = -2 (p is 2 positions ahead).