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 Uninitialized pointer in C is called as
Dangling Pointer
NULL Pointer
Void Pointer
Wild Pointer
[ 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: 6 What does the following fragment of C-Program print?
char c[]=”LAKEVIEW”;
char *p=c;
printf(“%s”,p+p[3]-p[1]);
LAKEVIEW
EVIEW
VIEW
IEW
[ 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: 7 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: 8 Find 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;
}
15
13
14
12
[ 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: 9 What 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);
}
21 21
20 20
11 11
22 22
[ 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: 10 If 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;
}
5
3
2
4
[ 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: 11 Find 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;
}
1
4
5
3
[ 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: 12 Output of the given C code fragment is ___________.
int i=5;
int *p=&i;
printf(*p);
Address of i
5
Address of p
Junk Value
[ 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: 13 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: 14 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: 15 If 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;
}
5
3
2
4
[ 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: 16 Assume size of integer data type is 4 bytes. Find output of the code given below
#include<stdio.h>
void func(char**);
int main()
{
char*argv[] = {"sa","ri","ga","ma","pa","da"};
func(argv);
return 0;
}
void func(char **p)
{
char *t;
t=(p+=sizeof(int))[-1];
printf("%s", t);
}
sa
ri
ga
ma
[ Option D ]
The array argv is an array of char*, so each element stores an address of a string ("sa", "ri", "ga", "ma", "pa", "da"). Here, as given, integer occupied 4 bytes memory. So, sizeof(int) is 4.
When func(argv) is called, the pointer p points to the first element, i.e., p=&argv[0].
In the given statement t=(p+=sizeof(int))[-1];, sizeof(int) is 4. Since p is of type char**, pointer arithmetic moves in units of sizeof(char*). Adding sizeof(int), i.e., 4 moves the pointer 4 positions ahead.
So:
Further, (p)[-1] means one position before current p. So, (p)[-1] points to "ma". Thus, t="ma".
Finally, printf("%s", t); prints "ma".
Q: 17 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: 18 Assume that float has 4 bytes, Find output of the code given below:
#include <stdio.h>
int main()
{
float x[5] = {212.5, 210.0, 50.5, 1.5, 0.5};
float *ptr1 = &x[0];
float *ptr2 = ptr1+3;
printf("%f ", *ptr2);
printf("%d", ptr2-ptr1);
return 0;
}
50.5 2
210.0 2
1.5 3
0.5 3
[ Option C ]
Here, ptr1 points to the first element of the array, i.e., x[0], and ptr2 is assigned as ptr1+3, which means it points to the element at index 3, i.e., x[3]. The value at x[3] is 1.5, so *ptr2 prints 1.5.
The expression ptr2-ptr1 gives the number of elements between the two pointers, not the number of bytes. Since ptr2 is three positions ahead of ptr1, the result is 3. Therefore, the output of the program is 1.5 3.
Q: 19 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.