C++ Programming Questions and Answers – Pointer to Void

Q 1. The void pointer can point to which type of objects?
A. int
B. float
C. double
D. all of the mentioned

Show Answer Answer:-B. float
Explanation Because it doesn’t know the type of object it is pointing to, So it can point to all objects.

Q 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

Show Answer Answer:-B. when it cast to another type of object
Explanation By casting the pointer to another data type, it can be dereferenced from the void pointer.

Q 3. The pointer can point to any variable that is not declared with which of these?
A. const
B. volatile
C. static
D. both const & volatile

Show Answer Answer:-D. both const & volatile
Explanation Pointer can point to any variable that is not declared with const & volatile.

Q 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

Show Answer Answer:-D. none of the mentioned
Explanation A void pointer can point to methods & class member in c++.

Q 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

Show Answer Answer:-B. address of string “abcdefghij”
Explanation Even though it is a void pointer, we gets the address. Output: $ g++ b.cpp $ a.out 0x8048714

Q 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. no output
B. equal
C. compile error
D. runtime error

Show Answer Answer:-B. equal
Explanation The void pointer is easily converted to any other type of pointer, so these are equal. Output: $ g++ poi4.cpp $ a.out equal

Q 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. 3d
C. 4d
D. two memory addresses

Show Answer Answer:-D. two memory addresses
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

Q 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

Show Answer Answer:-A. 5
Explanation We just casted this from void to int, so it prints 5 Output: $ g++ poi1.cpp $ a.out 5

Q 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

Show Answer Answer:-A. 8, memory address
Explanation In this program, we are just adding the two values and printing it. Output: $ g++ poi.cpp $ a.out 8 0xbfef0378

Q 10. What we can’t do on a void pointer?
A. pointer functions
B. pointer arithmetic
C. pointer objects
D. pointer functions & objects

Show Answer Answer:-B. pointer arithmetic
Explanation Because the void pointer is used to cast the variables only, So pointer arithmetic can’t be done in a void pointer.

Comments

Leave a Reply

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

You cannot copy content of this page