C++ Programming Questions and Answers – Pointer to Void

This section on C++ programming questions and answers focuses 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
View Answer

Answer: d
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

Answer: b
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

Answer: c
Explanation: Pointer can point to any variable that is not declared with const & volatile.
advertisement
advertisement

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

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

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

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
  1.     #include <iostream>
  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
View Answer

Answer: b
Explanation: Even though it is a void pointer, we gets the address.
Output:

advertisement
$ g++ b.cpp
$ a.out
0x8048714

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

advertisement
  1.     #include <iostream>
  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
View Answer

Answer: a
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?

  1.     #include <iostream>
  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
View Answer

Answer: b
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?

  1.     #include <iostream>
  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
View Answer

Answer: a
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?

  1.     #include <iostream>
  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
View Answer

Answer: a
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

Answer: a
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]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.