1. Which is referred by pointers to member?
A. Static members of class objects
B. Non-static members of class objects
C. Referring to whole class
D. Dynamic members of class objects
Answer: B
Explanation:
: We cannot use a pointer to member to point to a static class member because the address of a static member is not associated with any particular object.
2. What should be used to point to a static class member?
A. Smart pointer
B. Dynamic pointer
C. Normal pointer
D. Static pointer
Answer: C
Explanation:
: Normal pointer is sed to point to a static class member.
3. Which operator is used in pointer to member function?
A. .*
B. ->*
C. Both .* & ->*
D. $*
Answer: C
Explanation:
: The pointer to member operators .* and ->* are used to bind a pointer to a member of a specific class object.
4. What will be the output of the following C++ code?
#include<iostream>
using namespace std;
class X
{
public:
int a;
void f(int B.
{
cout<< b << endl;
}
};
int main()
{
int X :: *ptiptr = &X :: a;
void (X :: * ptfptr) (int) = &X :: f;
X xobject;
xobject.*ptiptr = 10;
cout << xobject.*ptiptr << endl;
(xobject.*ptfptr) (20);
}
A. 10
20
B. 20
10
C. 20
D. 10
Answer: A
Explanation:
: In this program, We are assigning 10 and printing it in the main function and then for value 20, We are passing the value to class and printing it.
5. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class Testpm
{
public:
void m_func1()
{
cout << "func1\n";
}
int m_num;
};
void (Testpm :: *pmfn)() = &Testpm :: m_func1;
int Testpm :: *pmd = &Testpm :: m_num;
int main()
{
Testpm ATestpm;
Testpm *pTestpm = new Testpm;
(ATestpm.*pmfn)();
(pTestpm ->* pmfn)();
ATestpm.*pmd = 1;
pTestpm ->* pmd = 2;
cout << ATestpm.*pmd << endl
<< pTestpm ->* pmd << endl;
}
A. func1
B. func1
func1
C. 1
2
D. func1
func1
1
2
Answer: D
Explanation:
: In this program, As we are passing the value twice to the method in the class, It is printing the func1 twice and then it is printing the given value.
6. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class Car
{
public:
int speed;
};
int main()
{
int Car :: *pSpeed = &Car :: speed;
Car c1;
c1.speed = 1;
cout << c1.speed << endl;
c1.*pSpeed = 2;
cout << c1.speed << endl;
return 0;
}
A. 1
B. 2
C. Both 1 & 2
D. 4
Answer: C
Explanation:
: In this program, We are printing the value by direct access and another one by using pointer to member.
7. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class bowl
{
public:
int apples;
int oranges;
};
int count_fruit(bowl * begin, bowl * end, int bowl :: *fruit)
{
int count = 0;
for (bowl * iterator = begin; iterator != end; ++ iterator)
count += iterator ->* fruit;
return count;
}
int main()
{
bowl bowls[2] = {{ 1, 2 },{ 3, 5 }};
cout << "I have " << count_fruit(bowls, bowls + 2, & bowl :: apples) << " apples\n";
cout << "I have " << count_fruit(bowls, bowls + 2, & bowl :: oranges) << " oranges\n";
return 0;
}
A.
I have 4 apples
I have 7 oranges
B.
I have 3 apples
I have 5 oranges
C.
I have 1 apples
I have 5 oranges
D.
I have 1 apples
I have 7 oranges
Answer: A
Explanation:
: In this program, We are passing the value to the class and adding the values and printing it in the main.
8. What will be the output of the following C++ code?
#include
using namespace std;
class Foo
{
public:
Foo(int i = 0){ _i = i;}
void f()
{
cout << "Executed"<
private:
int _i;
};
int main()
{
Foo *p = 0;
p -> f();
}
private: int _i; }; int main() { Foo *p = 0; p -> f(); }
A. Executed
B. Error
C. Runtime error
D. 10
Answer: A
Explanation:
: In this program, We passes the value to the class and printing it.
9. Which is the best design choice for using pointer to member function?
A. Interface
B. Class
C. Structure
D. Block
Answer: A
Explanation:
: Interface is the best design choice for using pointer to member function.
10. What is the operation for .*?
A. It combines the first operand and the second operand
B. It separates the first operand and the second operand
C. It reduces the data size
D. It combines the first operand and the second operand and terminates third operand
Answer: A
Explanation:
: 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.
- 10+ TOP MCQs on C++ Concepts
- 10+ TOP MCQs on C++ Concepts -2
- 10+ TOP C++ MCQs on Declaration
- 10+ TOP C++ MCQs on Enumerations
- 10+ TOP C++ MCQs on Character Classification
- 10+ TOP C++ MCQs on Complex Number Type
- 10+ TOP C++ MCQs on Conversion Operators
- 10+ TOP C++ MCQs on String – 1