Q: 1 Choose the correct output of the following code when executed in GCC compiler:
#include<stdio.h>
int main()
{
int arr[] = {1,2,3,4,5};
printf(“%c”,*(arr+3)+65);
}
69
68
D
E
[ Option D ]
The expression *(arr+3) accesses the 4th element of the array (4). Adding 65 gives 69. Since %c is used, ASCII 69 is printed, which is E.
Q: 2 Which of the following correctly declares a pointer to an integer in C?
int *ptr;
int ptr;
int &ptr;
pointer int ptr;
[ Option A ]
int *ptr; declares a pointer to an integer.
char *ptr; declares a pointer to a character.
float *ptr; declare a pointer to a float.
int **ptr; declares pointer to a int * and so on.
Q: 3 What happens in ++*ptr?
Pointer Increments
Value Increments
Both Increment
Nothing Happens
[ Option B ]
The expression is interpreted as ++(*ptr). Here, dereference happens first and the increment is applied to the value stored at that memory location. The pointer itself does not change, only the data it points to is incremented.
Q: 4 What is the size of a pointer on a 64-bit system?
2 Bytes
8 Bytes
4 Bytes
Depends on data type it points to
[ Option B ]
On a 64-bit system, pointers are typically 8 bytes, regardless of the data type they point to.
Q: 5 The *ptr++ is equivalent to:
(*ptr)++
*(ptr++)
++(*ptr)
*(++ptr)
[ Option B ]
Postfix ++ has higher precedence than *, so increment is applied to the pointer first in parsing.
However, because it is postfix, the current pointer value is used for dereferencing and then the pointer is incremented. Hence, value is accessed first and pointer moves afterward.
Q: 6 What will be printed for the following C program?
#include<stdio.h>
int main()
{
char myChar[7]=”Hanger”,*ptr;
ptr=myChar+4;
*ptr=’a’;
printf(“%s”,myChar);
return 0;
}
Hangear
Hangr
Hangar
Hanger
[ Option C ]
The character array myChar is initialized with the string "Hanger". The pointer ptr is assigned myChar+4, which means it points to the character at index 4. In the string "Hanger", index 4 contains the character 'e'.
The statement *ptr='a'; replaces 'e' with 'a', modifying the string to "Hangar". Therefore, when printf("%s", myChar); is executed, the output printed is Hangar.
Q: 7 Which expression increments pointer first?
*ptr++
++*ptr
*++ptr
(*ptr)++
[ Option C ]
The expression is interpreted as *(++ptr). Since prefix increment is used, the pointer is incremented before dereferencing.
So, pointer moves to the next location first, and then the value at the new location is accessed.
Q: 8 The expression (*ptr)++ means:
Increment pointer
Increment value before use
Increment value after use
None of these
[ Option C ]
This is a postfix increment applied to the dereferenced value. First, the current value is used for assignment or output, and then the value stored at that location is incremented. The pointer remains unchanged throughout.
Q: 9 Which are equivalent?
(i) *(ptr++)
(ii) *(ptr)++
(iii) (*ptr)++
(i) and (ii)
(i) and (iii)
(ii) and (iii)
(i), (ii) and (iii)
[ Option A ]
Due to operator precedence, postfix ++ binds with ptr and not with *. Therefore, *(ptr)++ is parsed as *(ptr++), making both expressions equivalent. However, (*ptr)++ is different because it increments the value, not the pointer.
Thank you so much for taking the time to read my Computer Science MCQs section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.