Pointer 01
Pointer 01
int main() {
int a = 5;
int *p = &a;
printf("%d", *p);
return 0;
}
Options:
a) 5
b) Address of a
c) 0
d) Compilation error
—------------------------------------------------------------------------------
Correct Answer: a) 5
Explanation: The pointer p points to a, and dereferencing p with *p retrieves the value
of a, which is 5.
Question 2
What will be the output of the following C code?
#include <stdio.h>
int main() {
int a = 10;
int *p = &a;
*p = 20;
printf("%d", a);
return 0;
}
Options:
a) 10
b) 20
c) Compilation error
d) Undefined behavior
—------------------------------------------------------------------------------------------
Correct Answer: b) 20
Explanation: The pointer p is used to modify the value of a through dereferencing.
After executing *p = 20;, the value of a becomes 20.
Question 3
What will be the output of the following C code?
#include <stdio.h>
int main() {
int a = 15;
int *p = &a;
printf("%d %d", a, *p);
return 0;
}
Options:
a) 15 15
b) 15 Address of a
c) Compilation error
d) Undefined behavior
—-------------------------------------------------------------------------------------------
Correct Answer: a) 15 15
Explanation: Both a and *p point to the same value, which is 15, so the output will be
15 15.
Question 4
What will be the output of the following C code?
#include <stdio.h>
int main() {
int a = 30;
int *p = &a;
printf("%d", *(&(*p)));
return 0;
}
Options:
a) Address of a
b) Compilation error
c) 30
d) Undefined behavior
—------------------------------------------------------------------------------------------------
Correct Answer: c) 30
Explanation: The expression *(&(*p)) dereferences p to get a, and then takes its
address and dereferences it again, resulting in 30.
Question 5
What will be the output of the following C code?
#include <stdio.h>
int main() {
int a = 5;
updateValue(&a);
printf("%d", a);
return 0;
}
Options:
a) 5
b) 10
c) 15
d) Compilation error
—----------------------------------------------------------------------------------------
Correct Answer: c) 15
Explanation: The function updateValue modifies the value of a by adding 10, so after
calling it, a becomes 15.
Question 6
What will be the output of the following C code?
#include <stdio.h>
int main() {
int a = 1, b = 2;
int *p1 = &a, *p2 = &b;
Options:
a) 3 2
b) 3 4
c) Compilation error
d) Undefined behavior
—-------------------------------------------------------------------------------------
Correct Answer: b) 3 2
Explanation: The first part calculates the sum (1 + 2) which is 3, and the second part
calculates the product (1 * 2) which is 2, resulting in 3 2.
Question 7
What will be the output of the following C code?
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3};
int *ptr = arr;
Options:
a) Compilation error
b) Undefined behavior
c) 2,3
d) Access violation
—------------------------------------------------------------------------------------------------
Correct Answer: c) 2,3
Explanation: The expression *(ptr + 1) accesses the second element (value 2) and
ptr accesses the third element (value 3) of the array.
Question 8
What will be the output of this C code?
#include <stdio.h>
int main() {
int a = 10, b = 20;
swap(&a, &b);
printf("%d %d", a, b);
return 0;
}
Options:
a) Compilation error
b) Runtime error
c) "10" "20"
d) "20" "10"
—-------------------------------------------------------------------------------
Correct Answer: d) "20" "10"
Explanation: The function swap exchanges the values of a and b, so after calling it, a
becomes 20, and b becomes 10, resulting in output "20" "10".