Pointer To Void(C++ Programming ) Questions and Answers
Question 1.
What we can't do on a void pointer?
pointer arithemetic
pointer functions
both of the mentioned
none of the mentioned
Explanation:-
Answer: Option A. -> pointer arithemetic
Because void pointer is used to cast the variables only, So pointer arithemetic can't be
done in a void pointer.
Question 2.
What is the output of this program?
1. #include 2. using namespace std; 3. int main() 4. { 5. int a = 5, c; 6. void *p = &a; 7. double b = 3.14; 8. p = &b; 9. c = a + b; 10. cout
8, memory address
8.14
memory address
none of the mentioned
Explanation:-
Answer: Option A. -> 8, memory address
In this program, we are just adding the two values and printing it. Output: $ g++ poi.cpp $ a.out 8 0xbfef0378
Question 3.
What is the output of this program?
1. #include 2. using namespace std; 3. int main() 4. { 5. int n = 5; 6. void *p = &n; 7. int *pi = static_cast(p); 8. cout
5
6
compile time error
runtime error
Explanation:-
Answer: Option A. -> 5
We just casted this from void to int, so it prints 5 Output: $ g++ poi1.cpp $ a.out 5
Question 4.
What is the output of this program?
1. #include 2. using namespace std; 3. int main() 4. { 5. int i; 6. char c; 7. void *data; 8. i = 2; 9. c = 'd'; 10. data = &i; 11. cout
2d
two memory addresses
both of the mentioned
none of the mentioned
Explanation:-
Answer: Option B. -> two memory addresses
Because the data points to the address value of the variables only, So it is printing the memory
address of these two variable. Output: $ g++ poi2.cpp $ a.out the data points to the integer value0xbfc81824 the data now points to the character0xbfc8182f
Question 5.
What is the output of this program?
1. #include 2. using namespace std; 3. int main() 4. { 5. int *p; 6. void *vp; 7. if (vp == p); 8. cout
equal
no output
compile error
runtime error
Explanation:-
Answer: Option A. -> equal
The void pointer is easily converted to any other type of pointer, so these are equal. Output: $ g++ poi4.cpp $ a.out equal
Question 6.
A void pointer cannot point to which of these?
methods in c++
class member in c++
all of the mentioned
none of the mentioned
Explanation:-
Answer: Option D. -> none of the mentioned
None.
Question 7.
When does the void pointer can be dereferenced?
when it doesn't point to any value
when it cast to another type of object
using delete keyword
none of the mentioned
Explanation:-
Answer: Option B. -> when it cast to another type of object
By casting the pointer to another data type, it can dereferenced from void pointer.
Question 8.
The pointer can point to any variable that is not declared with which of these?
const
volatile
both a & b
static
Explanation:-
Answer: Option C. -> both a & b
None.
Question 9.
Void pointer can point to which type of objects?
int
float
double
all of the mentioned
Explanation:-
Answer: Option D. -> all of the mentioned
Because it doesn't know the type of object it is pointing to, So it can point to all objects.