250+ TOP MCQs on Exceptions and Answers

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

Here is a listing of C++ Programming quiz on “Exceptions” along with answers, explanations and/or solutions:

1. To where does the program control transfers when the exception is arisen?
a) catch
b) handlers
c) throw
d) try
Answer: b
Clarification: When an exception is arisen mean, the exception is caught by handlers and then it decides the type of exception.

2. Which keyword is used to check exception in the block of code?
a) catch
b) throw
c) try
d) handlers
Answer: c
Clarification: The try() statement is used for exceptions in c++.

3. What will happen when the exception is not caught in the program?
a) error
b) program will execute
c) block of that code will not execute
d) program will execute & displays wrong output
Answer: a
Clarification: When exceptions are not caught in any program then program throws error.

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

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

a) 0
b) error:Positive Number Required
c) compile time error
d) runtime error
Answer: a
Clarification: As the zero marks the beginning of the positive number, it is printed as output
Output:

$ g++ excep.cpp
$ a.out
0

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

  1.     #include 
  2.     using namespace std;
  3.     void PrintSequence(int StopNum)
  4.     {
  5.         int Num;
  6.         Num = 1;
  7.         while (true) 
  8.         {
  9.             if (Num >= StopNum)
  10.                 throw Num;
  11.             cout << Num;
  12.             Num++;
  13.         }
  14.     }
  15.     int main(void)
  16.     {
  17.         try 
  18.         {
  19.             PrintSequence(20);
  20.         }
  21.         catch(int ExNum)
  22.         {
  23.             cout << "Caught an exception with value: " << ExNum;
  24.         }
  25.         return 0;
  26.     }

a) compile time error
b) prints first 19 numbers
c) prints first 19 numbers and throws exception at 20
d) prints first 17 numbers
Answer: c
Clarification: In this program, we are printing upto 19 numbers and when executing the 20, we are raising a exception.
Output:

$ g++ excep1.cpp
$ a.out
12345678910111213141516171819Caught an exception with value: 20

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 = 2;
  15.         double z = 0;
  16.         try 
  17.         {
  18.             z = division(x, y);
  19.             cout << z;
  20.         }
  21.         catch(const char *msg) 
  22.         {
  23.             cerr << msg;
  24.         }
  25.         return 0;
  26.     }

a) 25
b) 20
c) Division by zero condition!
d) 35
Answer: a
Clarification: In this program, we resembling the division by using the exception handling.
Output:

$ g++ excep2.cpp
$ a.out
25

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

  1.     #include 
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         char* buff;
  6.         try 
  7.         {
  8.             buff = new char[1024];
  9.             if (buff == 0)
  10.                throw "Memory allocation failure!";
  11.             else
  12.                cout << sizeof(buff) << "Byte successfully allocated!"<<endl;
  13.         }
  14.         catch(char *strg)
  15.         {
  16.             cout<<"Exception raised: "<<strg<<endl;
  17.         }
  18.         return 0;
  19.     }

a) 4 Bytes allocated successfully
b) 8 Bytes allocated successfully
c) Memory allocation failure
d) Depends on the size of the data type
Answer: d
Clarification: As we are allocating the memory to the variables and if there are not sufficient size means, it will throw an exception.
Output:

$ g++ excep3.cpp
$ a.out
4 Bytes allocated successfully

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

  1.     #include 
  2.     using namespace std;
  3.     void Funct();
  4.     int main()
  5.     {
  6.         try 
  7.         {
  8.             Funct();
  9.         }
  10.         catch(double) 
  11.         {
  12.             cerr << "caught a double type..." << endl;
  13.         }
  14.         return 0;
  15.     }
  16.     void Funct()
  17.     {
  18.         throw 3;
  19.     }

a) caught a double type
b) compile time error
c) abnormal program termination
d) runtime error
Answer: c
Clarification: As we are throwing integer to double it will raise as abnormal program after termination throw statement.
Output:

$ g++ excep4.cpp
$ a.out
terminate called after throwing an instance of 'int'
Aborted

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

  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     int main()
  5.     {
  6.         try 
  7.         {
  8.             int * array1 = new int[100000000];
  9.             int * array2 = new int[100000000];
  10.             int * array3 = new int[100000000];
  11.             int * array4 = new int[100000000];
  12.             cout << "Allocated successfully";
  13.         }
  14.         catch(bad_alloc&) 
  15.         {
  16.             cout << "Error allocating the requested memory." << endl;
  17.         }
  18.         return 0;
  19.     }

a) Allocated successfully
b) Error allocating the requested memory
c) Depends on the memory of the computer
d) Error
Answer: c
Clarification: In this program, we allocating the memory to the arrays by using exception handling and we handled the exception by standard exception.
Output:

$ g++ excep5.cpp
$ a.out
Allocated successfully

10. What will happen when the handler is not found for an exception?
a) calls the standard library function terminate()
b) raise an error
c) executes the remaining block
d) raise an error and executes the remaining block
Answer: a
Clarification: None.

250+ TOP MCQs on Subscripting and Answers

This section on C++ interview questions and answers on “Subscripting”. 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 “Subscripting” along with answers, explanations and/or solutions:

1. subscript operator is used to access which elements?
a) string
b) char
c) array
d) float
Answer: c
Clarification: To access any element of an array we use following syntax array[i], where i is called subscript representing the ith element of an array, whereas no such cases in char and strings.

2. How many arguments will the subscript operator will take for overloading?
a) 1
b) 2
c) 0
d) as many as possible
Answer: a
Clarification: The subscript operator overload takes only one argument, but it can be of any type.

3. Pick out the correct statement.
a) subscript operator has a higher precedence than the assignment operator
b) subscript operator has a lower precedence than the assignment operator
c) subscript operator is used with string elements
d) subscript operator is used with char elements
Answer: a
Clarification: Subscription operator has more precedence otherwise if that is not the case then the statement var = arr[i] will be meaningless and will have no effect.

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

  1.     #include 
  2.     using namespace std;
  3.     const int SIZE = 10;
  4.     class safe
  5.     {
  6.         private:
  7.         int arr[SIZE];
  8.         public:
  9.         safe()
  10.         {
  11.             register int i;
  12.             for (i = 0; i < SIZE; i++)
  13.             {
  14.                 arr[i] = i;
  15.             }
  16.         }
  17.         int &operator[](int i)
  18.         {
  19.             if (i > SIZE)
  20.             {
  21.                 cout << "Index out of bounds" <<endl;
  22.                 return arr[0];
  23.             }
  24.             return arr[i];
  25.         }
  26.     };
  27.     int main()
  28.     {
  29.         safe A;
  30.         cout << A[5];
  31.         cout  << A[12];
  32.         return 0;
  33.     }

a)

5Index out of bounds
 0

b) 40
c) 50
d) 51
Answer: a
Clarification: In this program, We are returning the elements in the specified array location and if it is out of bound means it will return the first element.
Output:

$ g++ sub.cpp
$ a.out
5Index out of bounds
0

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

  1.     #include 
  2.     using namespace std;
  3.     class numbers
  4.     {
  5.         private:
  6.         int m_nValues[10];
  7.         public:
  8.         int& operator[] (const int nValue);
  9.     };
  10.     int& numbers::operator[](const int nValue)
  11.     {
  12.         return m_nValues[nValue];
  13.     }
  14.     int main()
  15.     {
  16.         numbers N;
  17.         N[5] = 4;
  18.         cout <<  N[5];
  19.         return 0;
  20.     }

a) 5
b) 4
c) 3
d) 6
Answer: b
Clarification: In this program, We are getting the values and returning it by overloading the subscript operator.
Output:

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

  1.     #include 
  2.     using namespace std;
  3.     const int limit = 4;
  4.     class safearray
  5.     {
  6.         private:
  7.         int arr[limit];
  8.         public:
  9.         int& operator [](int n)
  10.         {
  11.             if (n == limit - 1)
  12.             {
  13.                 int temp;
  14.                 for (int i = 0; i < limit; i++)
  15.                 {
  16.                     if (arr[n + 1] > arr[n])
  17.                     {
  18.                         temp = arr[n];
  19.                         arr[n] = arr[n + 1];
  20.                         arr[n + 1] = temp;
  21.                     }     
  22.                 }  
  23.             }
  24.             return arr[n];
  25.         }
  26.     };
  27.     int main()
  28.     {
  29.         safearray sa1;
  30.         for(int j = 0; j < limit; j++)
  31.             sa1[j] = j*10;
  32.         for(int j = 0; j < limit; j++)
  33.         {
  34.             int temp = sa1[j];
  35.             cout << "Element " << j << " is " << temp;
  36.         }
  37.         return 0;
  38.     }

a) 0102030
b) 1020300
c) 3020100
d) error
Answer: a
Clarification: In this program, we are returning the array element by the multiple of 10.
Output:

$ g++ sub2.cpp
$ a.out
0102030

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

  1.     #include  
  2.     using namespace std;
  3.     class A
  4.     {
  5.         public:
  6.         int x;
  7.         A(int n = 0) : x(n) {};
  8.         int& operator[](int n)
  9.         {
  10.              cout << "0" ;
  11.              return x;
  12.         }
  13.         int operator[](int n) const
  14.         {
  15.              cout << "1" ;
  16.              return x;
  17.         }
  18.      };
  19.     void foo(const A& a)
  20.     {
  21.         int z = a[2];
  22.     }
  23.     int main()
  24.     {
  25.         A a(7);
  26.         a[3]  = 8;
  27.         int z = a[2];
  28.         foo(a);
  29.         return 0;
  30.     }

a) 110
b) 111
c) 011
d) 001
Answer: d
Clarification: In this program, we overloading the operator[] by using subscript operator.
Output:

$ g++ sub3.cpp
$ a.out
001

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

  1.     #include 
  2.     using namespace std;
  3.     class sample
  4.     {
  5.         private:
  6.         int* i;
  7.         int j;
  8.         public:
  9.         sample (int j);
  10.         ~sample ();
  11.         int& operator [] (int n);
  12.     };
  13.     int& sample::operator [] (int n)
  14.     {
  15.         return i[n];
  16.     }
  17.     sample::sample (int j)
  18.     {
  19.         i = new int [j];
  20.         j = j;
  21.     }
  22.     sample::~sample ()
  23.     {
  24.         delete [] i;
  25.     }
  26.     int main ()
  27.     {
  28.         sample m (5);
  29.         m [0] = 25;
  30.         m [1] = 20;
  31.         m [2] = 15;
  32.         m [3] = 10;
  33.         m [4] = 5;
  34.         for (int n = 0; n < 5; ++ n)
  35.         cout << m [n];
  36.         return 0;
  37.     }

a) 252015105
b) 510152025
c) 51015
d) 51015210
Answer: a
Clarification: In this program, we are printing the array in the reverse order by using subscript operator.
Output:

$ g++ sub4.cpp
$ a.out
252015105

9. What do we need to do to pointer for overloading the subscript operator?
a) reference pointer
b) dereference pointer
c) store it in heap
d) memory locator
Answer: b
Clarification: If you have a pointer to an object of some class type that overloads the subscript operator, you have to dereference that pointer in order to free the memory.

10. What do we need to use when we have multiple subscripts?
a) operator()
b) operator[]
c) operator
d) operator<>
Answer: a
Clarification: The reason is that operator[] always takes exactly one parameter, but operator() can take any number of parameters.

250+ TOP MCQs on Function Templates – 1 and Answers

C++ programming interview questions on “Function Templates” along with answers, explanations and/or solutions:

1. What is a function template?
a) creating a function without having to specify the exact type
b) creating a function with having an exact type
c) creating a function without having blank spaces
d) creating a function without class

Answer: a
Clarification: Function template is used to create a function without having to specify the exact type.

2. Which is used to describe the function using placeholder types?
a) template parameters
b) template type parameters
c) template type
d) type parameters

Answer: b
Clarification: During runtime, We can choose the appropriate type for the function and it is called as template type parameters.

3. Pick out the correct statement.
a) you only need to write one function, and it will work with many different types
b) it will take a long time to execute
c) duplicate code is increased
d) it will take a long time to execute & duplicate code is increased

Answer: a
Clarification: Because of template type parameters, It will work with many types and saves a lot of time.

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

  1.     #include 
  2.     using namespace std;
  3.     template<typename type>
  4.     type Max(type Var1, type Var2)
  5.     {
  6.         return Var1 > Var2 ? Var1:Var2;
  7.     }
  8.     int main()
  9.     {
  10.         int p;
  11.         p = Max(100, 200);
  12.         cout << p << endl;
  13.         return 0;
  14.     }

a) 100
b) 200
c) 300
d) 100200

Answer: b
Clarification: In this program, We are returning the maximum value by using function template.
Output:

$ g++ ftemp.cpp
$ a.out
200

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

  1.     #include 
  2.     using namespace std;
  3.     template<typename 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<float> Var2;
  26.         cout << Var1.Funct1(200) << endl;
  27.         cout << Var2.Funct2(3.123) << endl;
  28.         return 0;
  29.     }

a)

   200
   3.123

b)

   3.123
   200

c) 200
d) 3.123

Answer: a
Clarification: In this program, We are passing the values and getting it back from template. And we are using the constructor and destructor for the function template.
Output:

$ g++ ftemp1.cpp
$ a.out
200
3.123

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

  1.     #include 
  2.     using namespace std;
  3.     template<typename type>
  4.     class TestVirt
  5.     {
  6.         public:
  7.         virtual type TestFunct(type Var1)
  8.         {
  9.             return Var1 * 2;
  10.         }
  11.     };
  12.     int main()
  13.     {
  14.         TestVirt<int> Var1;
  15.         cout << Var1.TestFunct(100) << endl;
  16.         return 0;
  17.     }

a) 100
b) 200
c) 50
d) 150

Answer: b
Clarification: In this program, We are using class to pass the value and then we are manipulating it.
Output:

$ g++ ftemp3.cpp
$ a.out
200

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

  1.     #include 
  2.     using namespace std;
  3.     template<typename T>
  4.     inline T square(T x)
  5.     {
  6.         T result;
  7.         result = x * x;
  8.         return result;
  9.     };
  10.     int main()
  11.     {
  12.         int i, ii;
  13.         float x, xx;
  14.         double y, yy;
  15.         i = 2;
  16.         x = 2.2;
  17.         y = 2.2;
  18.         ii = square(i);
  19.         cout << i << " "  << ii << endl;
  20.         yy = square(y);
  21.         cout << y << " " << yy << endl;
  22.     }

a)

   2 4
   2.2 4.84

b) 2 4
c) error
d) 3 6

Answer: a
Clarification: In this program, We are passing the values and calculating the square of the value by using the function template.
Output:

$ g++ ftemp4.cpp
$ a.out
2 4
2.2 4.84

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

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

a) 2.1
b) 3.1
c)

   2.1
   3.1
   4.1

d) 3.2

Answer: d
Clarification: In this program, We are using the for loop to increment the value by 1 in the function template.
Output:

$ g++ ftemp5.cpp
$ a.out
2.1
3.1
4.1

9. What can be passed by non-type template parameters during compile time?
a) int
b) float
c) constant expression
d) string

Answer: c
Clarification: Non-type template parameters provide the ability to pass a constant expression at compile time. The constant expression may also be an address of a function, object or static class member.

10. From where does the template class derived?
a) regular non-templated C++ class
b) templated class
c) regular non-templated C++ class or templated class
d) main function

Answer: c
Clarification: Class derived template is derived from regular non-templated C++ class or templated class.

250+ TOP MCQs on Exception Handling – 1 and Answers

C++ Programming Multiple Choice Questions & Answers (MCQs) on “Exception Handling – 1”.

1. What is an exception in C++ program?
a) A problem that arises during the execution of a program
b) A problem that arises during compilation
c) Also known as the syntax error
d) Also known as semantic error
Answer: a
Clarification: An exception is defined as the problem in C++ program that arises during the execution of the program for example divide by zero error.

2. By default, what a program does when it detects an exception?
a) Continue running
b) Results in the termination of the program
c) Calls other functions of the program
d) Removes the exception and tells the programmer about an exception
Answer: b
Clarification: By default, whenever a program detects an exception the program crashes as it does not know how to handle it hence results in the termination of the program.

3. Why do we need to handle exceptions?
a) To avoid unexpected behaviour of a program during run-time
b) To let compiler remove all exceptions by itself
c) To successfully compile the program
d) To get correct output
Answer: a
Clarification: We need to handle exceptions in a program to avoid any unexpected behaviour during run-time because that behaviour may affect other parts of the program. Also, an exception is detected during run-time, therefore, a program may compile successfully even with some exceptions cases in your program.

4. How Exception handling is implemented in the C++ program?
a) Using Exception keyword
b) Using try-catch block
c) Using Exception block
d) Using Error handling schedules
Answer: b
Clarification: C++ provides a try-catch block to handle exceptions in your program.

5. What is the correct syntax of the try-catch block?
a)

try
{
	// programable codes.....
}
catch(Exceptions)
{
	// Code for handling exceptions....
}

b)

try()
{
	// programable codes.....
}
catch(Exceptions)
{
	// Code for handling exceptions....
}

c)

try
{
	// programable codes.....
}
catch
{
	// Code for handling exceptions....
}

d)

try()
{
	// programable codes.....
}
catch
{
	// Code for handling exceptions....
}

View Answer

Answer: a
Clarification: Try-catch block has the following syntax:

try{
	// codes that needs to check for exceptions
}
catch(Exception E1){
      // codes for handling exception.... 
      // Exception E denotes the type of exception this block is handling.
}
catch(Exception E2){
	// other exception that needs to be handled...
}

You can have any number of catch blocks catching different exceptions…..

 
 

6. Which part of the try-catch block is always fully executed?
a) try part
b) catch part
c) finally part
d) throw part
Answer: c
Clarification: finally part of the try-catch block is always executed whether exceptions are caught or not.

7. Which of the following is an exception in C++?
a) Divide by zero
b) Semicolon not written
c) Variable not declared
d) An expression is wrongly written
Answer: a
Clarification: Exceptions are those which are encountered during run-time of the program. semicolon, variable not declared and the wrong expression are compile-time errors, therefore, they are not exceptions. Divide by zero is the problem that is encountered during run-time, therefore, it is an exception.

8. What is an error in C++?
a) Violation of syntactic and semantic rules of a languages
b) Missing of Semicolon
c) Missing of double quotes
d) Violation of program interface
Answer: a
Clarification: An error occurs when rules and laws of a language is violated while writing programs in that language.

9. What is the difference between error and exception?
a) Both are the same
b) Errors can be handled at the run-time but the exceptions cannot
c) Exceptions can be handled at the run-time but the errors cannot
d) Both can be handled during run-time
Answer: c
Clarification: Exceptions can be handled during run-time whereas errors cannot be because exceptions occur due to some unexpected conditions during run-time whereas about errors compiler is sure and tells about them during compile-time.

10. What are the different types of exceptions?
a) 1
b) 2
c) 3
d) 4
Answer: b
Clarification: There are two types of exceptions: Synchronous and asynchronous exceptions. Synchronous exceptions that are caused by the event which can be controlled by the program whereas Asynchronous exceptions are those which are beyond the control of the program.

11. Which keyword is used to throw an exception?
a) try
b) throw
c) throws
d) except
Answer: b
Clarification: ‘throw’ keyword is used to throw exceptions if something bad happens.

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

#include 
#include 
#include 
using namespace std;
 
void func(int a, int b)
{
 
	if(b == 0){
		throw "This value of b will make the product zero. " 
                      "So please provide positive values.n";
	}
	else{
		cout<<"Product of "<<a<<" and  "<<b<<" is: "<<a*b<<endl;
	}
}
 
int main()
{
	try{
		func(5,0);
	}
	catch(const char* e){
		cout<<e;
	}
}

a) 0
b) 5
c) This value of b will make the product zero. So please provide positive values.
d) Product of 5 and 0 is: 0
Answer: c
Clarification: As the value of b = 0 is provided to the func() and the function is throwing an exception whenever the value of b = 0. Therefore the function throws the exception which will be printed on the screen.
Output:

$ ./a.out 
This value of b will make the product zero. So please provide positive values.

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

#include 
#include 
#include 
using namespace std;
void func(int a, int b)
{
	if(b == 0){
		throw "This value of b will make the product zero. " 
                       "So please provide positive values.n";
	}
	else{
		cout<<"Product of "<<a<<" and  "<<b<<" is: "<<a*b<<endl;
	}
}
 
int main()
{
	try{
		func(5,0);
	}
	catch(char* e){
		cout<<e;
	}
}

a) 0
b) Aborted (core dumped)
c) This value of b will make the product zero. So please provide positive values.
d) Product of 5 and 0 is: 0
Answer: b
Clarification: As the func() is throwing a const char* string but we the catch block is not catching any const char* exception i.e. exception thrown is not handled therefore the program results into Aborted(core dumped).
Output:

$ ./a.out 
terminate called after throwing an instance of 'char const*'
Aborted (core dumped)

14. What is Re-throwing an exception means in C++?
a) An exception that is thrown again as it is not handled by that catching block
b) An exception that is caught twice
c) An exception that is not handled in one caught hence thrown again
d) All of the mentioned
Answer: d
Clarification: Exception that is caught by a catch block but not handled by that catch block can be re-thrown by that catch block to further try-catch block.

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

#include 
#include 
#include 
using namespace std;
void func(int a, int b)
{
	if(b < 1){
		throw b;
	}
	else{
		cout<<"Product of "<<a<<" and  "<<b<<" is: "<<a*b<<endl;
	}
}
 
int main()
{
	try
        {
		try
                {			
		    func(5,-1);
		}
		catch(int b)
                {
			if(b==0)
				throw "value of b is zeron";
			else
				throw "value of b is less than zeron";
		}
	}
	catch(const char* e)
        {
		cout<<e;
	}
 
}

a) value of b is zero
b) value of b is less than zero
c) Product of 5 and -1 is: -5
d) Aborted(core dumped)
Answer: b
Clarification: Here the func() throws the value of b which is caught by the inner try-catch block, which again throws the message inorder to handle different cases of b which is caught by the outer try-catch block. Now as the value of b is negative the program outputs the message as shown.
Output:

$ ./a.out 
value of b is less than zero

250+ TOP MCQs on Sequences and Answers

This section on online C++ test on “Sequences”. One shall practice these test 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 online C++ test questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of online C++ test questions on “Sequences” along with answers, explanations and/or solutions:

1. How many items are there in sequence container?
a) 2
b) 3
c) 4
d) 5
Answer: d
Clarification: There are five items in sequence container. They are array, vector, list, forward_list and dequeue.

2. Which of the following class template are based on arrays?
a) vector
b) list
c) dequeue
d) both vector & dequeue
Answer: d
Clarification: Class template vector and class template dequeue both are based on arrays.

3. Which of the following will return the new element at the end of container?
a) front
b) back
c) push_back
d) pop_back
Answer: b
Clarification: Q3: back() in containers are used to access the last element of the sequence.

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

  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         deque<int> mydeque (5);  
  7.         deque<int>::reverse_iterator rit = mydeque.rbegin();
  8.         int i = 0;
  9.         for (rit = mydeque.rbegin(); rit!= mydeque.rend(); ++rit)
  10.             *rit = ++i;
  11.         for (deque<int> :: iterator it = mydeque.begin();
  12.         it != mydeque.end(); ++it)
  13.         cout << ' ' << *it;
  14.         return 0;
  15.     }

a) 12345
b) 1234
c) 54321
d) 43210
Answer: c
Clarification: In this program, We used the operation of rbegin and rend on dequeue and produced the result.
Output:

$ g++ seq.cpp
$ a.out
5 4 3 2 1

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

  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         unsigned int i;
  7.         deque<int> a (3,100);
  8.         deque<int> b (5,200);
  9.         a.swap(b);
  10.         cout << "a contains:";
  11.         for (deque<int>::iterator it = a.begin(); it != a.end(); ++it)
  12.             cout << ' ' << *it;
  13.         cout << "b contains:";
  14.         for (deque<int>::iterator it = b.begin(); it != b.end(); ++it)
  15.             cout << ' ' << *it;
  16.         return 0;
  17.     }

a) a contains: 200 200 200 200 200b contains: 100 100 100
b) a contains: 100 100 100 100 100b contains: 200 200 200
c) a contains: 200 200 200 200 200b contains: 200 200 200
d) a contains: 200 200 200 200 200b contains: 100 200 150
Answer: a
Clarification: In this program, We swapped the values of both dequeues and printing the dequeues.
Output:

$ g++ seq1.cpp
$ a.out
a contains: 200 200 200 200 200b contains: 100 100 100

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

  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         unsigned int i;
  7.         deque<int> mydeque;
  8.         mydeque.push_back (100);
  9.         mydeque.push_back (200);
  10.         mydeque.push_back (300);
  11.         for(deque<int> :: iterator it = mydeque.begin(); it != mydeque.end(); ++it)
  12.         {
  13.         }
  14.         mydeque.clear();
  15.         mydeque.push_back (110);
  16.         mydeque.push_back (220);
  17.         for(deque<int> :: iterator it = mydeque.begin(); it != mydeque.end(); ++it)
  18.             cout << ' ' << *it;
  19.         cout << 'n';
  20.         return 0;
  21.     }

a) 110
b) 220
c) Both 110 & 220
d) 330
Answer: c
Clarification: In this program, We cleared the old values presented in the dequeue with the new values.
Output:

$ g++ seq2.cpp
$ a.out
110 220

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

  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         vector<int> myvector;
  7.         int * p;
  8.         unsigned int i;
  9.         p = myvector.get_allocator().allocate(5);
  10.         for (i = 0; i < 5; i++) 
  11.             myvector.get_allocator().construct(&p[i], i);
  12.         for (i = 0; i < 5; i++)
  13.             cout << ' ' << p[i];
  14.         for (i = 0; i < 5; i++)
  15.             myvector.get_allocator().destroy(&p[i]);
  16.         myvector.get_allocator().deallocate(p, 5);
  17.         return 0;
  18.     }

a) 1 2 3 4 5
b) 0 1 2 3 4
c) 1 2 3 4
d) 5 4 3 2 1
Answer: b
Clarification: In this program, We allocated the values to the vector by using get allocater and then we are destroying it.
Output:

$ g++ seq3.cpp
$ a.out
0 1 2 3 4

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

  1.     #include 
  2.     #include 
  3.     #include 
  4.     using namespace std;
  5.     bool same_integral_part (double first, double second)
  6.     {  
  7.         return ( int(first) == int(second) ); 
  8.     }
  9.     struct is_near 
  10.     {
  11.         bool operator() (double first, double second)
  12.         { 
  13.             return (fabs(first - second) < 5.0); 
  14.         }
  15.     };
  16.     int main ()
  17.     {
  18.         double mydoubles[] = { 12.15,  2.72, 73.0,  12.77,  3.14, 12.77, 73.35, 72.25, 15.3,  72.25 };
  19.         list<double> mylist (mydoubles, mydoubles + 10);
  20.         mylist.sort();
  21.         mylist.unique();
  22.         mylist.unique (same_integral_part);
  23.         mylist.unique (is_near());
  24.         for (list<double> :: iterator it = mylist.begin(); it != mylist.end(); ++it)
  25.             cout << ' ' << *it;
  26.         cout << 'n';
  27.         return 0;
  28.     }

a) 2.72 12.15 72.25
b) 12.15 73.0 12.77
c) 73.35
d) 74.45
Answer: a
Clarification: In this program, We are eliminating the values by using the unique operation in the list.
Output:

$ g++ seq4.cpp
$ a.out
2.72 12.15 72.25

9. How the list containers are implemented?
a) Using Double linked list
b) Using Single linked list
c) Using Single & Double linked list
d) Using linear linked list
Answer: a
Clarification: List containers are implemented as doubly-linked lists. Doubly linked lists can store each of the elements they contain in different and unrelated storage locations.

10. Which of the following does not support any insertion or deletion?
a) Array
b) Vector
c) Dequeue
d) List
Answer: a
Clarification: Because array is not dynamic in nature, So they can’t be manipulated.

250+ TOP MCQs on Array Type Manipulation and Answers

C++ Programming Multiple Choice Questions & Answers (MCQs) on “Array Type Manipulation”.

1. Which of the header file is used for array type manipulation?
a)
b)
c)
d) std namespace
Answer: d
Clarification: Array type manipulation functions are declared incside the namespace std so you can use namespace std to use these functions.

2. What is the use of is_array() function in C++?
a) To check if a variable is array type or not
b) To check if a variable is 1-D array type or not
c) To check if a variable is 2-D array type or not
d) To check if a variable is 1-D or 2-D array type or not
Answer: a
Clarification: is_array() function is used to check whether a given variable is of array type or not.

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

#include 
#include 
using namespace std;
int main()
{
	cout<<is_array<int>::value;
	cout<<is_array<char[10]>::value;
	cout<<is_array<string>::value;
	return 0;
}

a) 010
b) 101
c) 001
d) 110
Answer: a
Clarification: As int and string are not of array type therefore 0 is printed corresponding to them and char[10] is an array of character of size 10 therefore 1 is printed corresponding to this. Hence answer is 010.

4. What is the use of is_same() function in C++?
a) To check if a variable is array type or not
b) To check whether two variables have the same characteristics
c) To check if two variable is of array type or not
d) To check whether two variables are different or not
Answer: b
Clarification: is_same() function is used to check whether two variables have the same characteristics or not.

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

#include 
#include 
using namespace std;
int main()
{
	cout<<is_same<int,char>::value;
	cout<<is_same<char[10], char[10]>::value;
	cout<<is_same<char*[10], string>::value;
	return 0;
}

a) 011
b) 101
c) 010
d) 110
Answer: c
Clarification: In 1st and 3rd case both the variables passed to is_same() function are different whereas for 2nd they are same. Hence the answer is 010.

6. What is the use of rank() function in C++?
a) Returns size of each dimension
b) Returns how many total elements can be stored in an array
c) Returns how many elements are in array currently
d) Returns the dimension of an array
Answer: d
Clarification: rank() function returns the rank of the array i.e. the dimension of an array. For example, int arr[10][10] has rank 2.

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

#include 
#include 
using namespace std;
int main()
{
	cout<<rank<int[10]>::value;
	cout<<rank<char[10][10]>::value;
	cout<<rank<string[10][10][10]>::value;
	return 0;
}

a) 111
b) 123
c) 321
d) 121
Answer: b
Clarification: In this program first array has the single dimension, second one has two dimensions and third one has three dimension therefore the program prints 123.

8. Which of the following is correct about extent() function?
a) Returns how many elements are in array currently
b) Returns the size of the 1st dimension
c) Returns how many total elements can be stored in an array
d) Returns the size of a given dimension
Answer: d
Clarification: The extent() function takes two parameters one denoting the array other showing the dimension for which the size we want to know.

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

#include 
#include 
using namespace std;
int main()
{
	cout<<extent<string[10][20][30], 0>::value;
	cout<<extent<string[10][20][30], 1>::value;
	cout<<extent<string[10][20][30], 2>::value;
	return 0;
}

a) 101010
b) 102030
c) 302010
d) 102010
Answer: b
Clarification: In first cout we are passing 0 and size of first dimension of array is 10 therefore 10 is printed. In following cases we have passed 1 and 2 therefore 20 and 30 are printed respectively.

10. Which of the following is correct about remove_extent() function?
a) Removes the given dimension from an array
b) Removes the first dimension from the right of the array
c) Removes the first dimension from the left of the array
d) Removes the last dimension from the left of the array
Answer: c
Clarification: remove_extent() function removes the first dimension i.e. the first dimension from the given array.

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

#include 
#include 
using namespace std;
int main()
{
	cout<<rank<remove_extent<string[10][20]>::type>::value;
	cout<<rank<remove_extent<string[10][20][30]>::type>::value;
	return 0;
}

a) 11
b) 12
c) 21
d) 22
Answer: b
Clarification: As we are removing the dimensions from these array and then printing the rank of arrays. Therefore as initially they have 2 and 3 as their rank so after deleting the rank becomes 1 and 2 hence the output is 12.

12. Which of the following is correct about remove_all_extents() function?
a) Removes the all dimension from an array
b) Removes the first dimension from the left of the array
c) Removes the first dimension from the right of the array
d) Removes the last dimension from the left of the array
Answer: a
Clarification: As the name suggests remove_all_extent() function removes all the dimensions from the array. So rank os array after this operation becomes 0.

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

#include 
#include 
using namespace std;
int main()
{
	cout<<rank<remove_all_extents<string[10][20]>::type>::value;
	return 0;
}

a) 1
b) 0
c) Error
d) Segmentation fault
Answer: b
Clarification: As we ahve deleted all the dimensions of this array therefore the rank of the array becomes zero hence the output is 0.

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

#include 
#include 
using namespace std;
int main()
{
	cout<<extent<remove_extent<string[10][20]>::type>::value;
	cout<<extent<remove_extent<string[10][20][30]>::type>::value;
	return 0;
}

a) 1010
b) 1020
c) 2020
d) 2030
Answer: c
Clarification: As we are deleting the first dimension from both the arrays and then printing the extent i.e. size of dimension therefore the answer is 2020 as both the array have 20 as the size of their second dimension.

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

#include 
#include 
using namespace std;
int main()
{
	cout<<extent<remove_all_extents<string[10][20][30]>::type>::value;
	return 0;
}

a) 20
b) 10
c) Error
d) 0
Answer: d
Clarification: As we have removed all the dimensions from the array therefore the output of extent is 0.