250+ TOP MCQs on Pointer to Void and Answers

This section on C++ programming questions and answers on “Pointer to Void”. One shall practice these questions to improve their C++ programming skills needed for various interviews (campus interviews, walk-in interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ programming questions come with the detailed explanation of the answers which helps in better understanding of C++ concepts.

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
Answer: d
Clarification: 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
Answer: b
Clarification: 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
Answer: c
Clarification: 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

Answer: d
Clarification: A void pointer can point to methods & class member in c++.

5. What will be the output of the following C++ code?

  1.     #include 
  2.     using namespace std;
  3.     int func(void *Ptr);
  4.     int main()
  5.     {
  6.         char *Str = "abcdefghij";
  7.         func(Str);
  8.         return 0;
  9.     }
  10.     int func(void *Ptr)
  11.     {
  12.         cout << Ptr;
  13.         return 0;
  14.     }

a) abcdefghij
b) address of string “abcdefghij”
c) compile time error
d) runtime error
Answer: b
Clarification: 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?

  1.     #include 
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int *p;
  6.         void *vp;
  7.         if (vp == p);
  8.             cout << "equal";
  9.         return 0;
  10.     }

a) equal
b) no output
c) compile error
d) runtime error
Answer: a
Clarification: 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?

  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 << "the data points to the integer value" << data;
  12.         data = &c;
  13.         cout << "the data now points to the character" << data;
  14.         return 0;
  15.     }

a) 2d
b) two memory addresses
c) 3d
d) 4d
Answer: b
Clarification: 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?

  1.     #include 
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int n = 5;
  6.         void *p = &n;
  7.         int *pi = static_cast<int*>(p);
  8.         cout << *pi << endl;
  9.         return 0;
  10.     }

a) 5
b) 6
c) compile time error
d) runtime error
Answer: a
Clarification: We just casted this from void to int, so it prints 5
Output:

9. What will be the output of the following C++ code?

  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 << c << 'n' << p;
  11.         return 0;
  12.     }

a) 8, memory address
b) 8.14
c) memory address
d) 12
Answer: a
Clarification: 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
Answer: a
Clarification: Because the void pointer is used to cast the variables only, So pointer arithmetic can’t be done in a void pointer.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top