Pointers To Members(C++ Programming ) Questions and Answers

Question 1.

What is the operation for .*?


  1.    It combines the first operand and the second operand
  2.    It seperates the first operand and the second operand
  3.    It reduces the data size
  4.    None of the mentioned
Explanation:-
Answer: Option A. -> It combines the first operand and the second operand

The binary operator .* combines its first operand, which must be an object of class type, with 

its second operand, which must be a pointer-to-member type.



Question 2.

Which is the best design choice for using pointer to member function?


  1.    Interface
  2.    Class
  3.    Structure
  4.    None of the mentioned
Explanation:-
Answer: Option A. -> Interface

None.



Question 3.


What is the output of this program?


1.
#include
2.
using namespace std;
3.
class Foo
4.
{
5.
public:
6.
Foo(int i = 0){ _i = i;}
7.
void f()
8.
{
9.
cout
  1.    Executed
  2.    Error
  3.    Runtime error
  4.    None of the mentioned
Explanation:-
Answer: Option A. -> Executed

In this program, We passes the value to the class and printing it.
Output:
$ g++ ptm4.cpp
$ a.out
Executed



Question 4.


What is the output of this program?


1.
#include
2.
using namespace std;
3.
class bowl
4.
{
5.
public:
6.
int apples;
7.
int oranges;
8.
};
9.
int count_fruit(bowl * begin, bowl * end, int bowl :: *fruit)
10.
{
11.
int count = 0;
12.
for (bowl * iterator = begin; iterator != end; ++ iterator)
13.
count += iterator ->* fruit;
14.
return count;
15.
}
16.
int main()
17.
{
18.
bowl bowls[2] = {{ 1, 2 },{ 3, 5 }};
19.
cout
  1.    Executed
  2.    Error
  3.    Runtime error
  4.    None of the mentioned
Explanation:-
Answer: Option A. -> Executed

Answer:a
Explanation:
In this program, We are passing the value to the class and adding the values and printing it in the main.
Output:
$ g++ ptm3.cpp
$ a.out
I have 4 apples
I have 7 oranges



Question 5.


What is the output of this program?


1.
#include
2.
using namespace std;
3.
class Car
4.
{
5.
public:
6.
int speed;
7.
};
8.
int main()
9.
{
10.
int Car :: *pSpeed = &Car :: speed;
11.
Car c1;
12.
c1.speed = 1;
13.
cout
  1.    1
  2.    2
  3.    Both a & b
  4.    None of the mentioned
Explanation:-
Answer: Option C. -> Both a & b

In this program, We are printing the value by direct access and another one by using pointer 

to member.
Output:
$ g++ ptm2.cpp
$ a.out
1
2