250+ TOP MCQs on Derived Classes and Answers

This section on C++ programming questions and answers on “Derived Classes”. One shall practice these questions to improve their C++ programming skills needed for various interviews (campus interviews, walkin 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 detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ programming questions on “Derived Classes” along with answers, explanations and/or solutions:

1. Where is the derived class is derived from?
a) derived
b) base
c) both derived & base
d) class
Answer: b
Clarification: Because derived inherits functions and variables from base.

2. Pick out the correct statement.
a) A derived class’s constructor cannot explicitly invokes its base class’s constructor
b) A derived class’s destructor cannot invoke its base class’s destructor
c) A derived class’s destructor can invoke its base class’s destructor
d) A derived class’s destructor can invoke its base & derived class’s destructor
Answer: b
Clarification: Destructors are automatically invoked when an object goes out of scope or when a dynamically allocated object is deleted. Inheritance does not change this behavior. This is the reason a derived destructor cannot invoke its base class destructor.

3. Which of the following can derived class inherit?
a) members
b) functions
c) both members & functions
d) classes
Answer: c
Clarification: Both data members and member functions are inherited by derived class in C++.

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

  1.     #include 
  2.     using namespace std;
  3.     class A
  4.     {
  5.         public:
  6.         A(int n )
  7.         {
  8.             cout << n;
  9.         }
  10.     };
  11.     class B: public A
  12.     {
  13.         public:
  14.         B(int n, double d)
  15.         : A(n)
  16.         {
  17.             cout << d;
  18.         }    
  19.     };
  20.     class C: public B
  21.     {
  22.         public:
  23.         C(int n, double d, char ch)
  24.         : B(n, d)
  25.         {
  26.             cout <<ch;
  27.         }
  28.     };
  29.     int main()
  30.     {
  31.         C c(5, 4.3, 'R');
  32.         return 0;
  33.     }

a) 54.3R
b) R4.35
c) 4.3R5
d) R2.6
Answer: a
Clarification: In this program, We are passing the value and manipulating by using the derived class.
Output:

$ g++ der.cpp
$ a.out
54.3R

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

  1.     #include 
  2.     using namespace std;
  3.     class BaseClass 
  4.     {
  5.         protected:
  6.         int i;
  7.         public:
  8.         BaseClass(int x) 
  9.         {
  10.             i = x;
  11.         }
  12.         ~BaseClass() 
  13.         {
  14.         }
  15.     };
  16.     class DerivedClass: public BaseClass 
  17.     {
  18.         int j;
  19.         public:
  20.         DerivedClass(int x, int y): BaseClass(y)
  21.         {
  22.             j = x;
  23.         }
  24.         ~DerivedClass() 
  25.         {
  26.         }
  27.         void show() 
  28.         {
  29.             cout << i << " " << j << endl;
  30.         }
  31.     };
  32.     int main()
  33.     {
  34.         DerivedClass ob(3, 4);
  35.         ob.show();
  36.         return 0;
  37.     }

a) 3 4
b) 4 3
c) 4
d) 3
Answer: b
Clarification: In this program, We are passing the values and assigning it to i and j and we are printing it.
Output:

$ g++ der1.cpp
$ a.out
4 3

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

  1.     #include 
  2.     using namespace std;
  3.     class Base
  4.     {
  5.         public:
  6.         int m;
  7.         Base(int n=0)
  8.         : m(n)
  9.         {
  10.             cout << "Base" << endl;
  11.         }
  12.     };
  13.     class Derived: public Base
  14.     {
  15.         public:
  16.         double d;
  17.         Derived(double de = 0.0)
  18.         : d(de)
  19.         {
  20.             cout << "Derived" << endl;
  21.         }
  22.     };
  23.     int main()
  24.     {
  25.         cout << "Instantiating Base" << endl;
  26.         Base cBase;
  27.         cout << "Instantiating Derived" << endl;
  28.         Derived cDerived;
  29.         return 0;
  30.     }

a)

   Instantiating Base
   Base
   Instantiating Derived
   Base
   Derived

b)

   Instantiating Base
   Instantiating Derived
   Base 
   Derived

c)

   Instantiating Base
   Base
   Instantiating Derived
   Base

d) Instantiating Base
Answer: a
Clarification: In this program, We are printing the execution order of the program.
Output:

$ g++ der2.cpp
$ a.out
Instantiating Base
Base
Instantiating Derived
Base
Derived

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

  1.     #include 
  2.     using namespace std;
  3.     class Parent
  4.     {
  5.         public:
  6.         Parent (void) 
  7.         {     
  8.             cout << "Parent()n";
  9.         }
  10.         Parent (int i) 
  11.         { 
  12.             cout << "Parent("<< i << ")n"; 
  13.         };
  14.         Parent (void) 
  15.         { 
  16.             cout << "~Parent()n";
  17.         }; 
  18.     };
  19.     class Child1 : public Parent { };
  20.     class Child2 : public Parent
  21.     {
  22.         public:
  23.         Child2 (void) 
  24.         {
  25.             cout << "Child2()n";
  26.         }
  27.         Child2 (int i) : Parent (i) 
  28.         {
  29.             cout << "Child2(" << i << ")n"; 
  30.         }
  31.         ~Child2 (void) 
  32.         {
  33.             cout << "~Child2()n"; 
  34.         }
  35.     };
  36.     int main (void)
  37.     {
  38.         Child1 a;
  39.         Child2 b;
  40.         Child2 c(42);
  41.         return 0;
  42.     }

a)

   Parent()
   Parent()
   Child2()
   Parent(42)
   Child2(42)
   ~Child2()
   ~Parent()
   ~Child2()
   ~Parent()
   ~Parent()

b) error
c) runtime error
d) Parent(42)
Answer: b
Clarification: In this program, We got an error in overloading because we didn’t invoke the destructor of parent.

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

  1.     #include
  2.     using namespace std;
  3.     class X 
  4.     {
  5.         int m;
  6.         public:
  7.         X() : m(10)
  8.         {                                                       
  9.         }
  10.         X(int mm): m(mm)
  11.         {
  12.         }
  13.         int getm()
  14.         {
  15.             return m;
  16.         }
  17.     };
  18.     class Y : public X 
  19.     {
  20.         int n;
  21.         public:
  22.         Y(int nn) : n(nn) {}                                                
  23.         int getn() { return n; }
  24.     };
  25.     int main()
  26.     {
  27.         Y yobj( 100 );
  28.         cout << yobj.getm() << " " << yobj.getn() << endl;
  29.     }

a) 10 100
b) 100 10
c) 10 10
d) 100 100
Answer: a
Clarification: In this program, We are passing the value and getting the result by derived class.
Output:

$ g++ der5.cpp
$ a.out
10 100

9. Which operator is used to declare the destructor?
a) #
b) ~
c) @
d) $
Answer: b
Clarification: tilde(~) is used to declare destructor of a class.

10. Which constructor will initialize the base class data member?
a) derived class
b) base class
c) class
d) derived & base class
Answer: b
Clarification: Because it is having the proper data set to initialize, Otherwise it will throw an error.

250+ TOP MCQs on Catching Exceptions and Answers

This section on C++ programming interview questions and answers on “Catching Exceptions”. One shall practice these interview questions to improve their C++ programming skills needed for various interviews (campus interviews, walkin 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 interview questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ programming interview questions on “Catching Exceptions” along with answers, explanations and/or solutions:

1. How many parameters does the throw expression can have?
a) 1
b) 2
c) 3
d) 4
Answer: a
Clarification: In c++ program, We can be able to throw only one error at a time.

2. Where exception are handled?
a) inside the program
b) outside the regular code
c) both inside or outside
d) main program
Answer: b
Clarification: Exception are handled outside the regular code.

3. Which is used to check the error in the block?
a) try
b) throw
c) catch
d) handler
Answer: a
Clarification: The try block is used to check for errors, if there is any error means, it can throw it to catch block.

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

  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     class myexception: public exception
  5.     {
  6.         virtual const char* what() const throw()
  7.         {
  8.             return "exception arised";
  9.         }
  10.     } myex;
  11.     int main () 
  12.     {
  13.         try
  14.         {
  15.             throw myex;
  16.         }
  17.         catch (exception& e)
  18.         {
  19.             cout << e.what() << endl;
  20.         }
  21.         return 0;
  22.     }

a) exception arised
b) error
c) exception
d) runtime error
Answer: a
Clarification: In this program, We are arising a standard exception and catching that and returning a statement.
Output:

$ g++ goe.cpp
$ a.out
exception arised

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

  1.     #include 
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int age=5;
  6.         try 
  7.         {
  8.             if (age < 0)
  9.                 throw "Positive Number Required";
  10.             cout  << age << "nn";
  11.         }
  12.         catch(const char* Message)
  13.         {
  14.             cout << "Error: " << Message;
  15.         }
  16.         return 0;
  17.     }

a) 5
b) 10
c) 15
d) Positive Number Required
Answer: a
Clarification: In this program, We are checking the age of a person, If it is zero means, We will arise a exception.
Output:

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

  1.     #include 
  2.     using namespace std;
  3.     double division(int a, int b)
  4.     {
  5.         if ( b == 0 )
  6.         {
  7.             throw "Division by zero condition!";
  8.         }
  9.         return (a / b);
  10.     }
  11.     int main ()
  12.     {
  13.         int x = 50;
  14.         int y = 0;
  15.         double z = 0;
  16.         try 
  17.         {
  18.             z = division(x, y);
  19.             cout << z << endl;
  20.         }
  21.         catch (const char* msg) 
  22.         {
  23.             cout << msg << endl;
  24.         }
  25.         return 0;
  26.     }

a) 50
b) 0
c) Division by zero condition!
d) 100
Answer: c
Clarification: We are dividing the values and if one of the values is zero means, We are arising an exception.
Output:

$ g++ goe2.cpp
$ a.out
Division by zero condition!

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

  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     int main()
  5.     {
  6.         double Op1 = 10, Op2 = 5, Res;
  7.         char Op;
  8.         try 
  9.         {   
  10.             if (Op != '+' && Op != '-' && Op != '*' && Op != '/')
  11.                 throw Op;
  12.             switch(Op)
  13.             {
  14.             case '+':
  15.                 Res = Op1 + Op2;
  16.                 break;
  17.             case '-':
  18.                 Res = Op1 - Op2;
  19.                 break;
  20.             case '*':
  21.                 Res = Op1 * Op2;
  22.                 break;
  23.             case '/':
  24.                 Res = Op1 / Op2;
  25.                 break;
  26.              }
  27.              cout << "n" << Op1 << " " << Op << " "<< Op2 << " = " << Res;
  28.          }
  29.          catch (const char n)
  30.          {
  31.              cout << n << " is not a valid operator";
  32.          }
  33.          return 0;
  34.     }

a) 15
b) 5
c) 2
d) is not a valid operator
Answer: d
Clarification: It will arise a exception because we missed a operator.
Output:

$ g++ goe3.cpp
$ a.out
is not a valid operator

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

  1.     #include
  2.     #include "math.h"
  3.     using namespace std;
  4.     double MySqrt(double d)
  5.     {
  6.         if (d < 0.0)
  7.         throw "Cannot take sqrt of negative number";     
  8.         return sqrt(d);
  9.     }
  10.     int main()
  11.     {
  12.         double d = 5;
  13.         cout << MySqrt(d) << endl;
  14.     }

a) 5
b) 2.236
c) Error
d) Cannot take sqrt of negative number
Answer: b
Clarification: We are finding the square root of the number, if it is a positive number, it can manipulate, Otherwise it will arise a exception.
Output:

$ g++ goe4.cpp
$ a.out
2.236

9. How to handle the exception in constructor?
a) We have to throw an exception
b) We have to return the exception
c) We have to throw an exception & return the exception
d) We have to catch an exception
Answer: a
Clarification: As a constructor don’t have a return type, We have to throw the exception.

10. What should present when throwing a object?
a) constructor
b) copy-constructor
c) destructor
d) copy-destructor
Answer: b
Clarification: copy-constructor is mandatory for throwing a object.

250+ TOP MCQs on Run Time Type Information and Answers

This section on C++ interview questions and answers on “Run Time Type Information”. One shall practice these interview questions to improve their C++ programming skills needed for various interviews (campus interviews, walkin 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++ interview questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ interview questions on “Run Time Type Information” along with answers, explanations and/or solutions:

1. What is the Run-Time Type Information?
a) Information about an object’s data type at runtime
b) Information about the variables
c) Information about the given block
d) Information about the functions
Answer: a
Clarification: With the help of RTTI, We can get the information about the data type at the runtime.

2. Which operators are part of RTTI?
a) dynamic_cast()
b) typeid
c) both dynamic_cast<> & typeid
d) dynamic_cast[]
Answer: c
Clarification: The dynamic_cast<> operation and typeid operator in C++ are part of RTTI.

3. To which type of class, We can apply RTTI?
a) Encapsulation
b) Polymorphic
c) Derived
d) Static
Answer: b
Clarification: RTTI is available only for classes which are polymorphic, which means they have at least one virtual method.

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

  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     class base { virtual void dummy() {} };
  5.     class derived: public base { int a; };
  6.     int main () 
  7.     {
  8.         try 
  9.         {
  10.             base * pba = new derived;
  11.             base * pbb = new base;
  12.             derived * pd;
  13.             pd = dynamic_cast<derived*>(pba);
  14.             if (pd == 0) 
  15.                 cout << "Null pointer on first type-cast" << endl;
  16.             pd = dynamic_cast<derived*>(pbb);
  17.             if (pd == 0) 
  18.                 cout << "Null pointer on second type-cast" << endl;
  19.         } 
  20.         catch (exception& e) 
  21.         {
  22.             cout << "Exception: " << e.what();
  23.         }
  24.         return 0;
  25.     }

a) Null pointer on first type-cast
b) Null pointer on second type-cast
c) Exception
d) Null pointer on third type-cast
Answer: b
Clarification: In this program, We apply the dynamic cast to pd. Based on the value in the pd, it produces the output.
Output:

$ g++ rtti.cpp
$ a.out
Null pointer on second type-cast

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

  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     int main () 
  5.     {
  6.         int * a;
  7.         int b;
  8.         a = 0; b = 0;
  9.         if (typeid(a) != typeid(b))
  10.         {
  11.             cout << typeid(a).name();
  12.             cout << typeid(b).name();
  13.         }
  14.         return 0;
  15.     }

a) Pi
b) i
c) Both pi & i
d) f
Answer: c
Clarification: In this program, We are finding the typeid of the given variables.
Output:

$ g++ rtti1.cpp
$ a.out
Pii

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

  1.     #include 
  2.     #include 
  3.     #include 
  4.     using namespace std;
  5.     class base 
  6.     { 
  7.         virtual void f(){} 
  8.     };
  9.     class derived : public base {};
  10.     int main () 
  11.     {
  12.         try 
  13.         {
  14.             base* a = new base;
  15.             base* b = new derived;
  16.             cout << typeid(*a).name() << 't';
  17.             cout << typeid(*b).name();
  18.         } 
  19.         catch (exception& e) 
  20.         { 
  21.             cout << "Exception: " << e.what() << endl; 
  22.         }
  23.         return 0;
  24.     }

a) base*
b) derived*
c) 4base and 7derived
d) Exception:derived
Answer: c
Clarification: In this program, We apply the typeid to the polymorphic class.
Output:

$ g++ rtti2.cpp
$ a.out
4base    7derived

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

  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     class A
  5.     {
  6.         public:
  7.         virtual ~A();
  8.     };
  9.     int main() 
  10.     {
  11.         A* a = NULL;
  12.         try 
  13.         {
  14.             cout << typeid(*a).name() << endl; 
  15.         }
  16.         catch (bad_typeid)
  17.         {
  18.             cout << "Object is NULL" << endl;
  19.         }
  20.     }

a) int
b) float
c) double
d) object is NULL
Answer: d
Clarification: In this program, We are using the bad typeid() for a. So it is arising an exception.
Output:

$ g++ rtti3.cpp
$ a.out
object is NULL

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

  1.     #include 
  2.     using namespace std;
  3.     struct A 
  4.     {
  5.         virtual void f()  
  6.         { 
  7.             cout << "Class A" << endl; 
  8.         }
  9.     };
  10.     struct B : A 
  11.     {
  12.         virtual void f() 
  13.         { 
  14.             cout << "Class B" << endl;
  15.         }
  16.     };
  17.     struct C : A 
  18.     {
  19.         virtual void f() 
  20.         {
  21.             cout << "Class C" << endl; 
  22.         }
  23.     };
  24.     void f(A* arg) 
  25.     {
  26.         B* bp = dynamic_cast<B*>(arg);
  27.         C* cp = dynamic_cast<C*>(arg);
  28.         if (bp)
  29.             bp -> f();
  30.         else if (cp)
  31.             cp -> f();
  32.         else
  33.             arg -> f();  
  34.     };
  35.     int main() 
  36.     {
  37.         A aobj;
  38.         C cobj;
  39.         A* ap = &cobj;
  40.         A* ap2 = &aobj;
  41.         f(ap);
  42.         f(ap2);
  43.     }

a) Class C
b) Class A
c) Both Class C & A
d) Class D
Answer: c
Clarification: In this program, We applied the dynamic casting to structure and produced the output.
Output:

$ g++ rtti4.cpp
$ a.out
Class C
Class A

9. What is meant by type_info?
a) Used to hold the type information returned by the typeid operator
b) Used to hold the type information returned by the dynamic_cast
c) Used to hold the type information returned by the static_cast
d) Used to hold the type information returned by the static_id
Answer: a
Clarification: type_info is used to hold the type information returned by the typeid operator.

10. At which time does the static_cast can be applied?
a) Compile-time construct
b) Runtime construct
c) Both Compile-time & Runtime construct
d) Runtime deconstruct
Answer: a
Clarification: Static_cast can be applied to only compile-time construct and not during run time construct.

250+ TOP MCQs on STL – Pair and Answers

C++ Programming Multiple Choice Questions & Answers (MCQs) on “STL – Pair”.

1. What is a pair?
a) Container consisting of two data elements of the same type
b) Container consisting of two data elements of different type
c) Container consisting of one header and two data elements of the same type
d) Container consisting of two data elements can have the same or different type
Answer: d
Clarification: Pair is a container defined in STL which consist of two elements which can be of same or different types.

2. Which header file is required to use pair container in your program?
a)
b)
c) d)
Answer: b
Clarification: Pair container is defined under the header file therefore one should include header before using pair container.

3. Which of the following is the correct syntax of using pair p?
a) pair p;
b) pair p ;
c) pair [type,type] p;
d) pair p [type,type];
Answer: a
Clarification: A pair is declared using the this syntax pair identifier.

4. Which of the following operations can be performed on a pair?
a) Assignment of pairs
b) Copying of one pair to another
c) Comparison of two pairs
d) All of the mentioned
Answer: d
Clarification: A pair can be assigned, copied or can be compared. Hence all the above operations can e performed on pairs.

5. Which operator is used to access the first or second element of a pair?
a) ->
b) .
c) *
d) []
Answer: b
Clarification: .(dot) operator is used to access the first or second element of a pair. For example, if p = (1,2) is a pair then 2 can be accessed by using p.first and 2 can be accessed using p.second.

6. Which of the following is the correct syntax of accessing the first element of a pair p?
a) p.first
b) p.second
c) p[0]
d) p[1]
Answer: a
Clarification: To access the first element of a pair we use first. for example, if p = (1,2) is a pair then we will use p.first to access the first element of the pair.

7. Which of the following is the correct syntax of accessing the second element of a pair p?
a) p.first
b) p.second
c) p[0]
d) p[1]
Answer: b
Clarification: To access the second element of a pair we use second. for example, if p = (1,2) is a pair then we will use p.second to access the second element of the pair.

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

#include   
#include 
 
using namespace std;
 
int main () 
{
  pair <int,int> p(1,2);
  cout<<"Pair(first,second) = ("<<p.first<<","<<p.second<<")n";
  return 0;
}

a) Pair(first,second) = (1,2)
b) Compile-time error
c) Run-time error
d) Assignment is not correct
Answer: a
Clarification: This is a way of assigning a pair therefore the program is correct hence the program runs perfectly and outputs the value as follows.
Output:

$ ./a.out 
Pair(first,second) = (1,2)

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

#include   
#include 
 
using namespace std;
 
int main () 
{
  pair p(1,2);
  cout<<"Pair(first,second) = ("<<p.first<<","<<p.second<<")n";
  return 0;
}

a) Pair(first,second) = (1,2)
b) Compile-time error
c) Run-time error
d) Assignment is not correct
Answer: b
Clarification: A pair always expects tempalte arguments i.e. types of first and second during declaration of pair. In this program as we have not mentioned the template arguments i.e. types of first and second therefore the program gives and error.

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

#include   
#include 
 
using namespace std;
 
int main () 
{
  pair <int,int>p;
  p = make_pair(1,2);
  cout<<"Pair(first,second) = ("<<p.first<<","<<p.second<<")n";
  return 0;
}

a) Pair(first,second) = (1,2)
b) Compile-time error
c) Run-time error
d) Assignment is not correct
Answer: a
Clarification: make_pair() is a function provied to define the values for a pair. Hence the program is correct therefore the program runs successfully.
Output:

$ ./a.out 
Pair(first,second) = (1,2)

11. Which of the following is correct way of copying the values of pair p1 into other pair p2?
a) pair p2 = p1;
b) pair p2(p1);
c) Both pair p2 = p1; and pair p2(p1);
d) Pair p2.copy(p1);
Answer: c
Clarification: Both pair p2 = p1; and pair p2(p1); can be used to copy the data of one pair into other pair.

12. What happens if a pair is not initialized?
a) Both first and second part is initialized to zero or null
b) Both first and second part is initialized a garbage value
c) First is initialized to zero or null and second is initialized a garbage value
d) Second is initialized to zero or null and first is initialized a garbage value
Answer: a
Clarification: If a pair is not initialized then by default both parts of the pair is initialized to zero.

13. Which of the following Operator cannot be used with pairs?
a) +
b) ==
c) =
d) !=
Answer: a
Clarification: We can use only assignment and logical operators with pairs.

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

#include   
#include 
#include 
 
using namespace std;
 
int main () 
{
  pair <int,int> p1(1,2);
  pair <int,int> p2(3,4);
  cout<<"Pair(first,second) = ("<<p1.first<<","<<p1.second<<")n";
  p1.swap(p2);
  cout<<"Pair(first,second) = ("<<p1.first<<","<<p1.second<<")n";
  return 0;
}

a)

Pair(first,second) = (1,2)
Pair(first,second) = (3,4)

b)

Pair(first,second) = (3,4)
Pair(first,second) = (1,2)

c)

Pair(first,second) = (1,2)
Pair(first,second) = (1,2)

d)

Pair(first,second) = (3,4)
Pair(first,second) = (3,4)

View Answer

Answer: a
Clarification: Inititally the pair p1 = (1,2) therefore Pair(first,second) = (1,2) is printed and when we have used swap function to swap p1 with p2 the p1 and p2 is swapped therefore next time Pair(first,second) = (3,4) is printed.
Output:

$ ./a.out 
Pair(first,second) = (1,2)
Pair(first,second) = (3,4)

 
 

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

#include   
#include 
#include 
 
using namespace std;
 
int main () 
{
  pair <int,int> p1(1,2);
  pair <int,int> p2(3,4);
  if(p1 <= p2)
  	cout<<"P1 is small";
  else
  	cout<<"P2 is small";
  return 0;
}

a) P1 is small
b) P2 is small
c) Error
d) Segmentation fault
Answer: a
Clarification: As both the elements are small in p1 pair, therefore, the pair p1 is considered small hence the output is as follows.
Output:

$ ./a.out 
P1 is small

250+ TOP MCQs on More Containers and Answers

C++ Programming Multiple Choice Questions & Answers (MCQs) on “More Containers”.

1. Which container is used to store elements as key-value pair?
a) map
b) multimap
c) unordered map
d) all of the mentioned
Answer: d
Clarification: C++ provides these three containers(map, multimap and unordered map) to store elements as key-value pair.

2. Which container can have the same keys?
a) map
b) multimap
c) unordered map
d) set
Answer: b
Clarification: C++ provide multimap container that is used to make map that can contain same keys i.e. {a: 5} and {a:10} both can exist.

3. Which container is best to keep the collection of distinct elements?
a) multimap
b) heap
c) set
d) queue
Answer: c
Clarification: C++ provides a set container to store a collection of distinct elements. This container behaves similar to mathematical sets.

4. Which container is used to keep priority based elements?
a) queue
b) stack
c) set
d) priority queue
Answer: d
Clarification: C++ provides priority queue container that stores elements based on their priority. For example, if the absolute value is the priority then -6 will be kept before 4 in the priority queue.

5. Sets are implemented using _______________________
a) binary search tree
b) red black tree
c) avl tree
d) heap
Answer: a
Clarification: Sets are implemented using the search tree so that we can check the presence of any element to be inserted in O(logn) time in order to remove conflicts between elements.

6. Unordered map is implemented using _________________
a) binary search tree
b) red black tree
c) heap
d) hash table
Answer: d
Clarification: As unordered map has no order of keys therefore hash table is used to store key-value pairs in a hash table.

7. Map is implemented using ____________________
a) binary search tree
b) red black tree
c) heap
d) hash table
Answer: b
Clarification: The map has some order of stored keys therefore red black tree is used to maintain that order and access the elements as soon as possible.

8. Which of the following is correct about the map and unordered map?
a) Ordering of keys in maps whereas no such order in the unordered map
b) Maps are implemented red-black trees whereas unordered map are implemented using hash tables
c) Average search time in the unordered map is O(1) whereas it is O(logn) in case of maps
d) All of the mentioned
Answer: d
Clarification: All the above mentioned points are correct about maps and unordered maps. Maps uses red-black tree whereas unordered map uses hash tables therefore the average search time for the unordered map is O(1) whereas it is O(logn) in case of maps.

9. Which of the following queue container can expand or shrink from both directions?
a) deque
b) queue
c) priority queue
d) stack
Answer: a
Clarification: Deque is a short form for a doubly ended queue which can be expanded and shrinked from any side of the queue either from the front or from the back.

10. Which of the following is correct about map and multimap?
a) Map can have same keys whereas multimap cannot
b) Implementation of maps and multimap are different
c) Multimap can have same keys whereas the map cannot
d) Average search time of map is greater than multimap
Answer: c
Clarification: Multimap is similar to map, the only difference that they have is that in multimap elements can have the same keys where in the map we have only one to one key-value pair correspondence.

Global Education & Learning Series – C++ Programming Language.

250+ TOP MCQs on Functors and Answers

C++ Programming Multiple Choice Questions & Answers (MCQs) on “Functors”.

1. What are functors in C++?
a) Objects of a class which are treated as functions
b) Objects that are used to call the function of other classes
c) Functions that are called using pointer objects
d) Functions that are called only once in a program
Answer: a
Clarification: Functors are objects of a class that are treated like function i.e. they can also be used as function calls.

2. Which of the following operators are overloaded for functors?
a) []
b) ()
c) <<
d) >>
Answer: b
Clarification: () operator is overloaded to use functor property in a C++ program because this is the only operator used for a function call.

3. What is the correct function prototype of () operator overloading?
a) return_type operator(arguments)();
b) return_type operator(arguments);
c) return_type operator()(arguments);
d) return_type operator(Class_name)(arguments);
Answer: c
Clarification: The correct syntax of overloading a () operator in a class is as follows:
return_type operator()(arguments){}

4. Which of the following is correct about Functors?
a) Functors should not be declared outside the main function
b) Overloaded operator () function is not a member of the class
c) Functors should be declared global
d) Functors have a state
Answer: d
Clarification: Functors are objects of a class which also have other members and member functions which can be used to save states of that functors hence functors have a state. Functors can be declared anywhere in a program.

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

#include
using namespace std;
class Print
{
   public:
	void operator()(int a){
		cout<<a<<endl;
	}
};
int main()
{
	Print print;
	print(5);
	return 0;
}

a) 5
b) Compile-time error
c) Run-time error
d) Nothing is printed
Answer: a
Clarification: As we have overloaded the () operator inside the class Print, therefore, the print object is a functor, therefore, can be used as a function which is just printing the value passed as a parameter.

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

#include
using namespace std;
class Add
{
	int x;
   public:
	Add(int x){
		this->x = x;
	}
	int operator()(int a){
		return x+a;
	}
};
int main()
{
	Add add_5(5);
	int a = 5;
	cout<<add_5(a);
	return 0;
}

a) 5
b) 10
c) Error
d) Segmentation fault
Answer: b
Clarification: As add_5 is a functor whose x value is initialized as 5 therefore it adds 5 to the value passed as parameter and returns the sum.

7. Given the below class, what is the correct syntax of declaring a functor that adds 10 to each of the passed argument?

class Add
{
	int x;
   public:
	Add(int x){
		this->x = x;
	}
	int operator()(int a){
		return x+a;
	}
};

a) Add add_10(10);
b) Add add_10(10);
c) Add add_10(10);
d) Add add_10(5);
View Answer

Answer: c
Clarification: Given the above class, we can declare an object of this class, which will be a functor that adds 10 to every value passed to it, like this Add add_10(10); where 10 in bracket shows that the variable 10 is intialized to class member x which will be used to add 10 to every argument.

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

#include
#include 
using namespace std;
template <class T>
class Print
{
   public:
	int operator()(T a){
		cout<<a<<endl;
	}
};
int main()
{
	Print<string> str;
	Print<int> integer;
	int a = 100;
	string s = "Hello ";
	str(s);
	integer(a);
	return 0;
}

a)

100
100

b)

100
Hello 

c)

Hello 
Hello 

d)

Hello 
100

View Answer

Answer: d
Clarification: The program is correct and we have initialized two functors one for printing the int values to command line and other for printing string on console. Therefore the program runs fine.

 
 

9. Which of te following is a built-in example of functors in C++?
a) mltiplication f(a1, a2);
b) add f(a1, a2);
c) subtract f(a1, a2);
d) plus f(a1, a2);
Answer: d
Clarification: plus f(a1, a2); is one of the correct in-built functor available.

10. Which of the following header file is required to use in-bulit functors of C++?
a)
b)
c)
d)
Answer: b
Clarification: header file is required to use the fuctionality of in-built functors provided by C++.

11. What are unary functors?
a) Functors that accepts only one parameter
b) Functors that accepts two parameters
c) Functors that accepts more than one parameters
d) Functors that accepts other than a specific type of parameter
Answer: a
Clarification: Unary functors are those which accepts only one argument as a parameter in a functor.

12. What are binary functors?
a) Functors that accepts only one parameter
b) Functors that accepts more than one parameters
c) Functors that accepts two parameters
d) Functors that accepts other than a specific type of parameter
Answer: c
Clarification: Binary functors are those which accepts two arguments as a parameter in a functor.

13. How many ways are there to use functors?
a) 1
b) 2
c) 3
d) 4
Answer: b
Clarification: There are two ways of using functors one like function calls and other like normal class objects.

14. Which of the following is a logical unary functor?
a) logical_or f;
b) logical_and f;
c) logical_not f;
d) negate f;
Answer: c
Clarification: logical_and and logical_or requires two arguments to act upon whereas negate and logical_not requires only one argument but negate also produces non-logical results like negate of a number, therefore, it is not logical hence logical_not f; is an only logical functor.

15. Which of the following is an arithmetic unary functor?
a) logical_not f;
b) logical_and f;
c) logical_or f;
d) negate f;
Answer: d
Clarification: logical_and and logical_or requires two arguments to act upon whereas negate and logical_not requires only one argument but logical_not produces only logical results, therefore, will not work on arithmetic values whereas negate works with all types of values.

16. What of the following is the equivalent statement for the functor call,

where f is a functor and arg1 and arg2 are the arguments required by the functors?
a) f.call(arg1, arg2);
b) f.operator()(arg1, arg2);
c) f.operator(arg1, arg2);
d) f.operator(arg1, arg2)();
Answer: b
Clarification: f(arg1, arg2) means we are calling the overlaoded () operator method which can be called using object f as follows f.operator()(arg1,arg2); In general, object.operator()(argument_lists);