Showing 91–100 of 100 questions
in Pointers
What will be printed?
int arr[] = {1, 2, 3};
int *p = arr;
printf("%d", *(p+2));
A
1
B
2
C
3
D
Garbage value
EXPLANATION
p+2 points to the third element of the array. *(p+2) dereferences it to get 3.
What is the difference between *p++ and (*p)++?
A
No difference
B
*p++ increments pointer, (*p)++ increments value
C
(*p)++ increments pointer, *p++ increments value
D
Syntax error
Correct Answer:
B. *p++ increments pointer, (*p)++ increments value
EXPLANATION
Due to operator precedence, *p++ is equivalent to *(p++), incrementing the pointer. (*p)++ explicitly increments the value.
Identify the output:
char *str = "Hello";
printf("%c", *str);
A
H
B
Hello
C
e
D
Address
EXPLANATION
*str dereferences the pointer to get the first character of the string, which is 'H'.
What is the output?
int x = 10, y = 20;
int *p = &x;
int *q = &y;
p = q;
printf("%d", *p);
A
10
B
20
C
Address of y
D
Garbage value
EXPLANATION
After p = q, pointer p now points to y. So *p gives the value of y, which is 20.
Which of the following is NOT a valid pointer declaration?
A
int *p;
B
float *q;
C
void *r;
D
int &s;
Correct Answer:
D. int &s;
EXPLANATION
& is a reference operator used in C++, not in C. In C, pointers are declared using *.
What happens when you increment a pointer?
int arr[5] = {1,2,3,4,5};
int *p = arr;
p++;
A
p points to the next byte in memory
B
p points to the next element of the array
C
p's value increases by 1
D
Compilation error
Correct Answer:
B. p points to the next element of the array
EXPLANATION
When a pointer is incremented, it moves to the next element based on the data type size (pointer arithmetic).
What is the output?
int a = 5;
int *p = &a;
int **q = &p;
printf("%d", **q);
A
5
B
Address of a
C
Address of p
D
Compilation error
EXPLANATION
q is a pointer to pointer p. **q dereferences q to get p, then dereferences p to get the value 5.
What is a NULL pointer?
A
Pointer pointing to address 0
B
Uninitialized pointer
C
Pointer with no name
D
Invalid pointer
Correct Answer:
A. Pointer pointing to address 0
EXPLANATION
A NULL pointer is a pointer that points to memory address 0, indicating it doesn't point to any valid memory location.
What will be the size of a pointer variable on a 64-bit system?
A
2 bytes
B
4 bytes
C
8 bytes
D
16 bytes
Correct Answer:
C. 8 bytes
EXPLANATION
On a 64-bit system, a pointer is 8 bytes (64 bits) regardless of the data type it points to.
Which operator is used to get the address of a variable in C?
EXPLANATION
The & operator (address-of operator) returns the memory address of a variable.