Pointer To Void(C++ Programming ) Questions and Answers

Question 1.

What we can't do on a void pointer?


  1.    pointer arithemetic
  2.    pointer functions
  3.    both of the mentioned
  4.    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
  1.    8, memory address
  2.    8.14
  3.    memory address
  4.    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
  1.    5
  2.    6
  3.    compile time error
  4.    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
  1.    2d
  2.    two memory addresses
  3.    both of the mentioned
  4.    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
  1.    equal
  2.    no output
  3.    compile error
  4.    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?


  1.    methods in c++
  2.    class member in c++
  3.    all of the mentioned
  4.    none of the mentioned
Explanation:-
Answer: Option D. -> none of the mentioned

None.



Question 7.

When does the void pointer can be dereferenced?


  1.    when it doesn't point to any value
  2.    when it cast to another type of object
  3.    using delete keyword
  4.    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?


  1.    const
  2.    volatile
  3.    both a & b
  4.    static
Explanation:-
Answer: Option C. -> both a & b

None.



Question 9.

Void pointer can point to which type of objects?


  1.    int
  2.    float
  3.    double
  4.    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.