Here is a listing of C++ programming questions on “Pointer to Void” along with answers, explanations and/or solutions:
1. The void pointer can point to which type of objects?
a) int
b) float
c) double
d) all of the mentioned
View Answer
Explanation: Because it doesn’t know the type of object it is pointing to, So it can point to all objects.
2. When does the void pointer can be dereferenced?
a) when it doesn’t point to any value
b) when it cast to another type of object
c) using delete keyword
d) using shift keyword
View Answer
Explanation: By casting the pointer to another data type, it can be dereferenced from the void pointer.
3. The pointer can point to any variable that is not declared with which of these?
a) const
b) volatile
c) both const & volatile
d) static
View Answer
Explanation: Pointer can point to any variable that is not declared with const & volatile.
4. A void pointer cannot point to which of these?
a) methods in c++
b) class member in c++
c) methods & class member in c++
d) none of the mentioned
View Answer
Explanation: A void pointer can point to methods & class member in c++.
5. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int func(void *Ptr);
int main()
{
char *Str = "abcdefghij";
func(Str);
return 0;
}
int func(void *Ptr)
{
cout << Ptr;
return 0;
}
a) abcdefghij
b) address of string “abcdefghij”
c) compile time error
d) runtime error
View Answer
Explanation: Even though it is a void pointer, we gets the address.
Output:
$ g++ b.cpp $ a.out 0x8048714
6. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int *p;
void *vp;
if (vp == p)
cout << "equal";
return 0;
}
a) equal
b) no output
c) compile error
d) runtime error
View Answer
Explanation: The void pointer is easily converted to any other type of pointer, so these are equal.
Output:
$ g++ poi4.cpp $ a.out equal
7. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int i;
char c;
void *data;
i = 2;
c = 'd';
data = &i;
cout << "the data points to the integer value" << data;
data = &c;
cout << "the data now points to the character" << data;
return 0;
}
a) 2d
b) two memory addresses
c) 3d
d) 4d
View Answer
Explanation: 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
8. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int n = 5;
void *p = &n;
int *pi = static_cast<int*>(p);
cout << *pi << endl;
return 0;
}
a) 5
b) 6
c) compile time error
d) runtime error
View Answer
Explanation: We just casted this from void to int, so it prints 5
Output:
$ g++ poi1.cpp $ a.out 5
9. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int a = 5, c;
void *p = &a;
double b = 3.14;
p = &b;
c = a + b;
cout << c << '\n' << p;
return 0;
}
a) 8, memory address
b) 8.14
c) memory address
d) 12
View Answer
Explanation: In this program, we are just adding the two values and printing it.
Output:
$ g++ poi.cpp $ a.out 8 0xbfef0378
10. What we can’t do on a void pointer?
a) pointer arithmetic
b) pointer functions
c) pointer objects
d) pointer functions & objects
View Answer
Explanation: Because the void pointer is used to cast the variables only, So pointer arithmetic can’t be done in a void pointer.
Sanfoundry Global Education & Learning Series – C++ Programming Language.
To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.
If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]
- Apply for C++ Internship
- Practice Programming MCQs
- Practice Computer Science MCQs
- Apply for Computer Science Internship
- Check C++ Books