This section contains carefully selected MCQs and Previous Year Questions with explanations to help students understand concepts and prepare effectively for examinations, interviews, and competitive tests.
Q: 1Choose 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);
}
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: 2Which of the following correctly declares a pointer to an integer in C?
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: 3What happens in ++*ptr?
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: 4What is the size of a pointer on a 64-bit system?
Option B
On a 64-bit system, pointers are typically 8 bytes, regardless of the data type they point to.
Q: 5Uninitialized pointer in C is called as
Option D
In C language, a pointer that is declared but not initialized with any valid address is called a Wild Pointer. Such pointers contain garbage values and may point to any random memory location, which can cause unpredictable behavior or crashes.
Dangling Pointer: It points to a memory location that has already been freed or deleted.
NULL Pointer: It is a pointer that is explicitly assigned NULL i.e., it points to nothing.
Void Pointer: It is a generic pointer that can hold the address of any data type.
For more about Pointer and Void Pointer.
Q: 6What does the following fragment of C-Program print?
char c[]=”LAKEVIEW”;
char *p=c;
printf(“%s”,p+p[3]-p[1]);
Option C
In this program, a character array "LAKEVIEW" is stored, and pointer p points to the beginning of the string. Each character has an index position such as L(0), A(1), K(2), E(3), V(4), I(5), E(6), W(7).
The expression p+p[3]-p[1] uses pointer arithmetic. Here, p[3] is 'E' whose ASCII value is 69, and p[1] is 'A' whose ASCII value is 65. So the expression becomes p+(69-65) = p+4.
This means the pointer now points to index 4, which is character 'V'. When printf("%s", p+4) is executed, it prints the string starting from that position, resulting in "VIEW".
Q: 7The *ptr++ is equivalent to:
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: 8Find output of the code given below
#include<stdio.h>
int main()
{
int a=5;
int*ptr ;
ptr=&a;
*ptr=*ptr*3;
printf("%d", a);
return 0;
}
Option A
A pointer stores the address of a variable, and using the dereference operator *, we can access or modify the value stored at that address.
Here, the variable a is initialized with value 5. Then a pointer ptr is declared and assigned the address of a using ptr=&a;. This means ptr now points to a.
In given statement *ptr=*ptr*3, the *ptr accesses the value of a. So, this statement multiplies the current value of a by 3, i.e., 5*3=15 and stores the result back into a itself.
Finally, the printf statement prints the value of a, which is now 15.
Q: 9What is the output of the following ‘C’ code snippet?
#include<stdio.h>
tchange(int* b, int* a)
{
*a+=*a;
*b*=*a;
*a++=++*b;
*--b=*--a;
}
void main()
{
int a=2, b=5;
tchange(&a,&b);
printf(“%d %d”,a,b);
}
Option A
This question involves pointers and operator precedence in C, so careful step-by-step evaluation is required. Initially, the values are a=2 and b=5. The function is called as tchange(&a, &b), so inside the function, pointer b refers to variable a, and pointer a refers to variable b.
First, the statement *a += *a is executed. Since a points to b, this updates b as 5+5 = 10. Next, *b *= *a is executed. Here, b points to a and *a is 10, so a becomes 2*10 = 20.
Now, the statement *a++ = ++*b is evaluated. The expression ++*b increments the value of a to 21. Then *a++ assigns this value to b, and afterward the pointer moves. So now both a and b become 21.
Finally, in the statement *--b = *--a, both pointers are decremented, but they still refer to the same updated variables. This does not change the values further.
| Statement | Value of a | Value of b |
|---|---|---|
| Initially | 2 | 5 |
| *a += *a | 2 | 10 |
| *b *= *a | 20 | 10 |
| *a++ = ++*b | 21 | 21 |
| *--b = *--a | 21 | 21 |
Q: 10If int is of two bytes, find the output of the code given below
#include<stdio.h>
int main()
{
int a[]={1,2,3,4,5};
int *ptr;
ptr=a;
printf(“%d”, *(ptr+1));
return 0;
}
Option C
In C, the name of an array represents the base address of the array. When we write ptr=a;, the pointer ptr points to the first element of the array, i.e., a[0].
The ptr+1 moves the pointer to the next integer location. So ptr+1 points to the second element of the array, which is a[1].
The expression *(ptr+1) first moves the pointer ptr to the next memory location, i.e., a[1]. After moving to this position, the * operator (Dereference Operator) is applied, which retrieves the value stored at that memory location, i.e., 2.
Q: 11Find output of the code given below
#include<stdio.h>
int main()
{
int arr[]={1,2,3,4,5};
int *p=arr;
++*p;
p += 3;
printf("%d", *p);
return0;
}
Option B
The array is initialized as arr = {1, 2, 3, 4, 5} and the pointer p points to the first element arr[0].
The expression ++*p means increment the value pointed by p, not the pointer itself [Click for Detailed Solution]. So, arr[0] changes from 1 to 2. Now the array becomes {2, 2, 3, 4, 5}.
Next, the statement p+=3 moves the pointer 3 positions ahead. Since p was pointing to arr[0], it now points to arr[3], which contains the value 4.
Finally, *p prints the value at this position, which is 4.
Q: 12Output of the given C code fragment is ___________.
int i=5;
int *p=&i;
printf(*p);
Option B
In C language, a Pointer is a variable that stores the address of another variable. Using pointers, we can access or modify the value stored at that address.
In the given code, the variable i is initialized with value 5, and the pointer p is assigned the address of i using &i.
The expression *p represents dereferencing, which means accessing the value stored at the address contained in the pointer.
Since p points to i, dereferencing p gives the value of i, which is 5. Therefore, printf(*p); prints 5.
Q: 13What 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;
}
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: 14Which expression increments pointer first?
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: 15If int is of two bytes, find the output of the code given below
#include<stdio.h>
int main()
{
int a[]={1,2,3,4,5};
int *ptr;
ptr=a;
printf(“%d”, *(ptr+1));
return 0;
}
Option C
In C, the name of an array represents the base address of the array. When we write ptr=a;, the pointer ptr points to the first element of the array, i.e., a[0].
The ptr+1 moves the pointer to the next integer location. So ptr+1 points to the second element of the array, which is a[1].
The expression *(ptr+1) first moves the pointer ptr to the next memory location, i.e., a[1]. After moving to this position, the * operator (Dereference Operator) is applied, which retrieves the value stored at that memory location, i.e., 2.
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.