Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
Filter by Categories
nmims post
Objective Type Set
Online MCQ Assignment
Question Solution
Solved Question
Uncategorized

Multiple choice question for engineering

Set 1

1. What is meant by template specialization?
a) It will have certain data types to be fixed
b) It will make certain data types to be dynamic
c) Certain data types are invalid
d) None of the mentioned

View Answer

Answer: a [Reason:] In the template specialization, it will make the template to be specific for some data types.

2. Which is similar to template specialization?
a) template
b) function overloading
c) function template overloading
d) none of the mentioned

View Answer

Answer: c

3. Which is called on allocating the memory for array of objects?
a) destructor
b) constructor
c) method
d) none of the mentioned

View Answer

Answer: b [Reason:] When you allocate memory for an array of objects, the default constructor must be called to construct each object. If no default constructor exists, you’re stuck needing a list of pointers to objects.

4. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     template <class T>
  4.     inline T square(T x)
  5.     {
  6.         T result;
  7.         result = x * x;
  8.         return result;
  9.     };
  10.     template <>
  11.     string square<string>(string ss)
  12.     {
  13.         return (ss+ss);
  14.     };
  15.     int main()
  16.     {
  17.         int i = 2, ii;
  18.         string ww("A");
  19.         ii = square<int>(i);
  20.         cout << i << ": " << ii;
  21.         cout << square<string>(ww) << ":" << endl;
  22.     }

a) 2:4AA
b) 2:4
c) AA
d) 2:4A

View Answer

Answer: a [Reason:] Template specialization is used when a different and specific implementation is to be used for a specific data type. In this program, We are using integer and character. Output: $ g++ spec.cpp $ a.out 2:4AA

5. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     template <typename T = float, int count = 3>
  4.     T multIt(T x)
  5.     {
  6.         for(int ii = 0; ii < count; ii++)
  7.         {
  8.             x = x * x;
  9.         }
  10.         return x;
  11.     };
  12.     int main()
  13.     {
  14.         float xx = 2.1;
  15.         cout << xx << ": " << multIt<>(xx) << endl;
  16.     }

a) 2.1
b) 378.228
c) 2.1: 378.228
d) None of the mentioned

View Answer

Answer: c [Reason:] In this program, We specifed the type in the template function. We need to compile this program by adding -std=c++0x. Output: $ g++ -std=c++0x spec1.cpp $ a.out 2.1: 378.228

6. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     template <class T>
  4.     class XYZ
  5.     {
  6.         public:
  7.         void putPri();
  8.         static T ipub;
  9.         private:
  10.         static T ipri; 
  11.     };
  12.     template <class T>
  13.     void XYZ<T>::putPri()
  14.     {
  15.         cout << ipri++ << endl;
  16.     }
  17.     template <class T> T XYZ<T>::ipub = 1;
  18.     template <class T> T XYZ<T>::ipri = 1.2;
  19.     int main()
  20.     {
  21.         XYZ<int> a;
  22.         XYZ<float> b;
  23.         a.putPri();
  24.         cout << a.ipub << endl;
  25.         b.putPri();
  26.     }

a) 1
b) 1.2
c) 1
    1.2
d) 1
    1
    1.2

View Answer

Answer: d [Reason:] In this program, We are passing the value of specified type and printing it by specialization. Output: $ g++ spec2.cpp $ a.out 1 1 1.2

7. What is the output of this program?

  1.     #include <iostream>
  2.     #include <string>
  3.     #include <cstring>
  4.     using namespace std;
  5.     template <class type>
  6.     type MyMax(const type Var1, const type Var2)
  7.     {
  8.         cout << "no specialization";
  9.         return Var1 < Var2 ? Var2 : Var1;
  10.     }
  11.     template <>
  12.     const char *MyMax(const char *Var1, const char *Var2)
  13.     {
  14.         return (strcmp(Var1, Var2)<0) ? Var2 : Var1;
  15.     }
  16.     int main()
  17.     {
  18.         string Str1 = "class", Str2 = "template";
  19.         const char *Var3 = "class";
  20.         const char *Var4 = "template";
  21.         const char *q = MyMax(Var3, Var4);
  22.         cout << q << endl;
  23.         return 0;
  24.     }

a) template
b) class
c) no specialization
d) none of the mentioned

View Answer

Answer: a [Reason:] In this program, We are computing the result in the specalized block of the program. Output: $ g++ spec3.cpp $ a.out template

8. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     template<class T = float, int i = 5> class A
  4.     {
  5.         public:
  6.         A();
  7.         int value;
  8.     };
  9.     template<> class A<> 
  10.     { 
  11.         public: A(); 
  12.     };
  13.     template<> class A<double, 10>
  14.     { 
  15.         public: A(); 
  16.     };
  17.     template<class T, int i> A<T, i>::A() : value(i)
  18.     {
  19.         cout << value;
  20.     }
  21.     A<>::A() 
  22.     {
  23.         cout << "default";
  24.     }
  25.     A<double, 10>::A() 
  26.     {
  27.         cout << "10" << endl;
  28.     }
  29.     int main() 
  30.     {
  31.         A<int, 6> x;
  32.         A<> y;
  33.         A<double, 10> z;
  34.     }

a) 6
b) 10
c) 6default10
d) None of the mentioned

View Answer

Answer: c [Reason:] In this program, We are defining three templates and specializing it and passing the values to it and printing it. Output: $ g++ spec5.cpp $ a.out 6default10

9. How many types of specialization are there in c++?
a) 1
b) 2
c) 3
d) 4

View Answer

Answer: b [Reason:] There are two types specialization. They are full specialization and partial specialization.

10. What is other name of full specialization?
a) explicit specialization
b) implicit specialization
c) function overloading template
d) none of the mentioned

View Answer

Answer: a

Set 2

1. What do all STL containers define?
a) Iterator types
b) Begin methods
c) End methods
d) All of the mentioned

View Answer

Answer: d [Reason:] All the STL containers define the iterator types for that container, e.g., iterator and const_iterator, e.g., vector::iterator and the begin/end methods for that container, e.g., begin() and end().

2. What do we return if we use simple array on a internal container?
a) Methods
b) Pointers
c) Objects
d) None of the mentioned

View Answer

Answer: b [Reason:] Pointers are legal iterators, so if your internal container is a simple C array, then all you need to do is return the pointers.

3. What is mandatory for designing a new container?
a) Classes
b) Iterators
c) Container
d) None of the mentioned

View Answer

Answer: b

4. What are the design requirements for building a container from the sratch?
a) Container interface requirements
b) Allocator interface requirements
c) Iterator requirements
d) All of the mentioned

View Answer

Answer: d [Reason:] These are the design specific requirements for building a container from the scratch.

5. How many iterators are needed for the defining a new container?
a) 1
b) 2
c) 3
d) 4

View Answer

Answer: c [Reason:] There are three main iterators needed for designing a container. They are const iterator, Reverse iterator and Iterator traits.

6. What is the use of the allocater interface in the user-defined container?
a) Storage management
b) Memory management
c) Storage & Memory management
d) None of the mentioned

View Answer

Answer: a

7. How many types of container classes are there in c++?
a) 1
b) 2
c) 3
d) As many as possible

View Answer

Answer: b [Reason:] There are two type of container classes in c++. They are value containers and reference containers.

8. What is the name of the container which contains group of multiple objects?
a) Heterogeneous container
b) Homogeneous container
c) Both Homogeneous & Heterogeneous container
d) None of the mentioned

View Answer

Answer: a

9. What is the output of this program?

  1.     #include <iostream>
  2.     #include <string>
  3.     #include <algorithm>
  4.     using namespace std;
  5.     int main() 
  6.     {
  7.         string s = "spaces in text";
  8.         s.erase(remove(s.begin(), s.end(), ' ' ), s.end() ) ;
  9.         cout << s << endl;
  10.     }

a) spaces
b) spaces in
c) spaces in text
d) spacesintext

View Answer

Answer: d [Reason:] In this program, We formed a alogorithm to remove spaces in the string. Output: $ g++ dan.cpp $ a.out spacesintext

10. What is the output of this program?

  1.     #include <vector> 
  2.     #include <algorithm>
  3.     #include <iostream>
  4.     #include <iterator>
  5.     using namespace std;
  6.     int square(int i) { return i * i; }
  7.     int main()
  8.     {
  9.         vector<int> V, V2;
  10.         V.push_back(0);
  11.         V.push_back(1);
  12.         V.push_back(2);
  13.         transform(V.begin(), V.end(), back_inserter(V2), square);
  14.         copy(V2.begin(), V2.end(), ostream_iterator<int>(cout, " "));
  15.         cout << endl;
  16.     }

a) 0
b) 1
c) 2
d) 0 1 4

View Answer

Answer: d [Reason:] In this program, We formed an algorithm to find the square of the given number. Output: $ g++ dan1.cpp $ a.out 0 1 4

Set 3

1. Which is used to handle the exceptions in c++?
a) catch handler
b) handler
c) exception handler
d) none of the mentioned

View Answer

Answer: c

2. Which type of program is recommended to include in try block?
a) static memory allocation
b) dynamic memory allocation
c) const reference
d) pointer

View Answer

Answer: b [Reason:] While during dynamic memory allocation, Your system may not have sufficient resources to handle it, So it is better to use it inside the try block.

3. Which statement is used to catch all types of exceptions?
a) catch()
b) catch(Test t)
c) catch(…)
d) none of the mentioned

View Answer

Answer: c [Reason:] This catch statement will catch all types of exceptions that arises in the program.

4. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int x = -1;
  6.         try 
  7.         {
  8.             if (x < 0)
  9.             {
  10.                 throw x;
  11.             }
  12.             else
  13.             {
  14.                 cout<<x;
  15.             }
  16.         }
  17.         catch (int x )
  18.         {
  19.             cout << "Exception occurred: Thrown value is " << x << endl;
  20.         }
  21.         return 0;
  22.     }

a) -1
b) 0
c) Exception occurred: Thrown value is -1
d) Error

View Answer

Answer: c [Reason:] As the given value is -1 and according to the condition, We are arising an exception. Output: $ g++ etae.cpp $ a.out Exception occurred: Thrown value is -1

5. What is the output of this program?

  1.     #include <iostream>
  2.     #include <typeinfo>
  3.     using namespace std;
  4.     class Polymorphic {virtual void Member(){}};
  5.     int main () 
  6.     {
  7.         try
  8.         {
  9.             Polymorphic * pb = 0;
  10.             typeid(*pb);   
  11.         }
  12.         catch (exception& e)
  13.         {
  14.             cerr << "exception caught: " << e.what() << endl;
  15.         }
  16.         return 0;
  17.     }

a) exception caught: std::bad_typeid
b) exception caught: std::bad_alloc
c) exception caught: std::bad_cast
d) none of the mentioned

View Answer

Answer: a [Reason:] In this program, We used a bad type id for the polymorphic operator, So it is arising an bad_typeid exception. Output: $ g++ etae.cpp $ a.out exception caught: std::bad_typeid

6. What is the output of this program?

  1.     #include <iostream>
  2.     #include <exception>
  3.     using namespace std;
  4.     void myunexpected () 
  5.     {
  6.         cout << "unexpected handler calledn";
  7.         throw;
  8.     }
  9.     void myfunction () throw (int,bad_exception) 
  10.     {
  11.         throw 'x';
  12.     }
  13.     int main (void)
  14.     {
  15.         set_unexpected (myunexpected);
  16.         try 
  17.         {
  18.             myfunction();
  19.         }    
  20.         catch (int) 
  21.         { 
  22.             cout << "caught intn"; 
  23.         }
  24.         catch (bad_exception be) 
  25.         { 
  26.             cout << "caught bad_exceptionn"; 
  27.         }
  28.         catch (...) 
  29.         { 
  30.             cout << "caught other exception n"; 
  31.         }
  32.         return 0;
  33.     }

a) unexpected handler called
b) caught bad_exception
c) caught other exception
d) both unexpected handler called & caught bad_exception

View Answer

Answer: d [Reason:] In this program, We are calling set_unexpected and myfunction, So it is printing the output as the given. Output: $ g++ etae.cpp $ a.out unexpected handler called caught bad_exception

7. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std; 
  3.     int main()
  4.     {
  5.         int x = -1;
  6.         char *ptr;
  7.         ptr = new char[256];
  8.         try 
  9.         {
  10.             if (x < 0)
  11.             {
  12.                 throw x;
  13.             }
  14.             if (ptr == NULL)
  15.             {
  16.                 throw " ptr is NULL ";
  17.             }
  18.         }
  19.         catch (...) 
  20.         {
  21.             cout << "Exception occurred: exiting "<< endl;
  22.         }
  23.         return 0;
  24.     }

a) -1
b) ptr is NULL
c) exception occured: exiting
d) none of the mentioned

View Answer

Answer: c [Reason:] catch(…) is used to catch all types of exceptions arising in the program. Output: $ g++ etea.cpp $ a.out Exception occured: exiting

8. What is the output of this program?

  1.     #include <iostream>
  2.     #include <exception>
  3.     using namespace std;
  4.     void myunexpected ()
  5.     {
  6.         cout << "unexpected calledn";
  7.         throw 0;
  8.     }
  9.     void myfunction () throw (int) 
  10.     {
  11.         throw 'x';
  12.     }
  13.     int main () 
  14.     {
  15.         set_unexpected (myunexpected);
  16.         try 
  17.         {
  18.             myfunction();
  19.         }
  20.         catch (int) 
  21.         {
  22.             cout << "caught intn";
  23.         }
  24.         catch (...)  
  25.         { 
  26.             cout << "caught other exceptionn"; 
  27.         }
  28.         return 0;
  29.     }

a) caught other exception
b) caught int
c) unexpected called
d) both caught int & unexpected called

View Answer

Answer: d [Reason:] As we are calling set_unexpected (myunexpected) function, this is printing as unexpected called and because of operator compliance it is arising an exception. Output: $ g++ etea.cpp $ a.out unexpected called caught int

9. How to handle error in the destructor?
a) throwing
b) terminate
c) both throwing & terminate
d) none of the mentioned

View Answer

Answer: b [Reason:] It will not throw an exception from the destructor but it will the process by using terminate() function.

10. What kind of exceptions are available in c++?
a) handled
b) unhandled
c) static
d) dynamic

View Answer

Answer: b

Set 4

1. What is meant by template parameter?
a) It can be used to pass a type as argument
b) It can be used to evaluate a type
c) It can of no return type
d) None of the mentioned

View Answer

Answer: a [Reason:] A template parameter is a special kind of parameter that can be used to pass a type as argument.

2. Which keyword can be used in template?
a) class
b) typename
c) both class & typename
d) function

View Answer

Answer: c

3. What is the validity of template parameters?
a) inside that block only
b) inside the class
c) whole program
d) any of the mentioned

View Answer

Answer: a

4.What is the output of this program?

  1.    #include <iostream>
  2.    using namespace std;
  3.    template <class T, int N>
  4.    class mysequence 
  5.    {
  6.        T memblock [N];
  7.        public:
  8.        void setmember (int x, T value);
  9.        T getmember (int x);
  10.    };
  11.    template <class T, int N>
  12.    void mysequence<T,N> :: setmember (int x, T value) 
  13.    {
  14.        memblock[x] = value;
  15.    }
  16.    template <class T, int N>
  17.    T mysequence<T,N> :: getmember (int x) 
  18.    {
  19.        return memblock[x];
  20.    }
  21.    int main () 
  22.    {  
  23.        mysequence <int, 5> myints;
  24.        mysequence <double, 5> myfloats;
  25.        myints.setmember (0, 100);
  26.        myfloats.setmember (3, 3.1416);
  27.        cout << myints.getmember(0) << 'n';
  28.        cout << myfloats.getmember(3) << 'n';
  29.        return 0;
  30.    }

a) 100
b) 3.1416
c) 100
3.1416
d) none of the mentioned

View Answer

Answer: c [Reason:] In this program, We are printing the integer in the first function and float in the second function. Output: $ g++ farg.cpp $ a.out 100 3.1416

5. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     template <class T>
  4.     T max (T& a, T& b) 
  5.     {
  6.         return (a>b?a:b);
  7.     }
  8.     int main () 
  9.     {
  10.         int i = 5, j = 6, k;
  11.         long l = 10, m = 5, n;
  12.         k = max(i, j);
  13.         n = max(l, m);
  14.         cout << k << endl;
  15.         cout << n << endl;
  16.         return 0;
  17.     }

a) 6
b) 6
    10
c) 5
    10
d) 6
    5

View Answer

Answer: b [Reason:] In this program, We are using the ternary operator on the template function. Output: $ g++ farg.cpp $ a.out 6 10

6. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     template <class type>
  4.     class Test
  5.     {
  6.         public:
  7.         Test()
  8.         {
  9.         };
  10.         ~Test()
  11.         {  
  12.         };
  13.         type Funct1(type Var1)
  14.         {
  15.             return Var1;
  16.         }
  17.         type Funct2(type Var2)
  18.         {
  19.             return Var2;
  20.         }
  21.     };
  22.     int main()
  23.     {
  24.         Test<int> Var1;
  25.         Test<double> Var2;
  26.         cout << Var1.Funct1(200);
  27.         cout << Var2.Funct2(3.123);
  28.         return 0;
  29.     }

a) 100
b) 200
c) 3.123
d) 2003.123

View Answer

Answer: d [Reason:] In this program, We are passing the value and returning it from template. Output: $ g++ farg3.cpp $ a.out 2003.123

7. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     template <typename T, int count>
  4.     void loopIt(T x)
  5.     {
  6.         T val[count];
  7.         for(int ii = 0; ii < count; ii++)
  8.         {
  9.             val[ii] = x++;
  10.             cout <<  val[ii] << endl;
  11.         }
  12.     };
  13.     int main()
  14.     {
  15.         float xx = 2.1;
  16.         loopIt<float, 3>(xx);
  17.     }

a) 2.1
b) 3.1
c) 4.1
d) 2.1
    3.1
    4.1

View Answer

Answer: d [Reason:] In this program, We are using the non-type template parameter to increment the value in the function template. Output: $ g++ farg4.cpp $ a.out 2.1 3.1 4.1

8. Why we use :: template-template parameter?
a) binding
b) rebinding
c) both binding & rebinding
d) none of the mentioned

View Answer

Answer: c [Reason:] It is used to adapt a policy into binary ones.

9. Which parameter is legal for non-type template?
a) pointer to member
b) object
c) class
d) none of the mentioned

View Answer

Answer: a [Reason:] The following are legal for non-type template parameters:integral or enumeration type, Pointer to object or pointer to function, Reference to object or reference to function, Pointer to member.

10. Which of the things does not require instantiation?
a) functions
b) non virtual member function
c) member class
d) all of the mentioned

View Answer

Answer: d [Reason:] The compiler does not generate definitions for functions, non virtual member functions, class or member class because it does not require instantiation.

Set 5

1. How many parameters does the throw expression can have?
a) 1
b) 2
c) 3
d) 4

View Answer

Answer: a [Reason:] 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) none of the mentioned

View Answer

Answer: b

3. Which is used to check the error in the block?
a) try
b) throw
c) catch
d) none of the mentioned

View Answer

Answer: a [Reason:] The try block is used to check for errors, if there is any error means, it can throw it to catch block.

4. What is the output of this program?

  1.     #include <iostream>
  2.     #include <exception>
  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

View Answer

Answer: a [Reason:] 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 is the output of this program?

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

View Answer

Answer: a [Reason:] In this program, We are checking the age of a person, If it is zero means, We will arise a exception. Output: $ g++ goe1.cpp $ a.out 5

6. What is the output of this program?

  1.     #include <iostream>
  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) None of the mentioned

View Answer

Answer: c [Reason:] 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 is the output of this program?

  1.     #include <iostream>
  2.     #include <string>
  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

View Answer

Answer: d [Reason:] It will arise a exception because we missed a operator. Output: $ g++ goe3.cpp $ a.out is not a valid operator

8. What is the output of this program?

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

View Answer

Answer: b [Reason:] 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) none of the mentioned

View Answer

Answer: a [Reason:] 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) none of the mentioned

View Answer

Answer: b