3.6 Generic programming and exception handling

3.6 GENERIC PROGRAMMING AND EXCEPTION HANDLING 

 

FUNCTION TEMPLATE 

 

  1. What is a function template in C++?

a) A function that accepts any number of arguments

b) A function that returns a template type

c) A generic function that can operate with any data type

d) A function defined within a class template
 

Answer: c) A generic function that can operate with any data type
 

Explanation: A function template in C++ is a generic function that can operate with any data type. It allows you to define a function once and use it with different data types without rewriting the code.

 

  1. How is a function template declared in C++?

a) Using the 'template' keyword followed by the function definition

b) Using the 'template' keyword followed by the function declaration

c) Using the 'function' keyword followed by the template type

d) Using the 'typename' keyword followed by the function definition
 

Answer: b) Using the 'template' keyword followed by the function declaration
 

Explanation: In C++, a function template is declared using the 'template' keyword followed by the function declaration, specifying the template type.

 

  1. Which symbol is used to denote a placeholder for the template type in a function template?

a) #

b) &

c) *

d) T
 

Answer: d) T
 

Explanation: The symbol 'T' (or any other valid identifier) is commonly used to denote a placeholder for the template type in a function template in C++.

  1. What is the benefit of using function templates in C++?

a) Improved performance

b) Reduced code duplication

c) Better memory management

d) Easier debugging
 

Answer: b) Reduced code duplication
 

Explanation: Function templates in C++ help reduce code duplication by allowing you to write a single function that can work with multiple data types, thus promoting code reusability.

 

  1. Can function templates be used with user-defined data types in C++?

a) Yes, but only with built-in data types

b) No, function templates only work with primitive data types

c) Yes, function templates can be used with any data type, including user-defined types

d) Yes, but only with classes, not structures
 

Answer: c) Yes, function templates can be used with any data type, including user-defined types
 

Explanation: Function templates in C++ can be used with any data type, including built-in types like int and float, as well as user-defined types such as classes and structures.

 

  1. Which of the following is true about function template specialization in C++?

a) It allows specifying different implementations for specific data types

b) It restricts the use of function templates to a single data type

c) It is not supported in C++

d) It improves code readability but increases compilation time
 

Answer: a) It allows specifying different implementations for specific data types
 

Explanation: Function template specialization in C++ allows you to provide different implementations for specific data types while still benefiting from the generic behavior of function templates.

 

  1. How is function template specialization defined in C++?

a) Using the 'specialize' keyword followed by the function definition

b) Using the 'template' keyword followed by the specialized function declaration

c) Using the 'template' keyword followed by the 'specialize' keyword

d) Using the 'template' keyword followed by the specialized function definition
 

Answer: d) Using the 'template' keyword followed by the specialized function definition
 

Explanation: Function template specialization in C++ is defined using the 'template' keyword followed by the specialized function definition for specific data types.

 

  1. What is the purpose of a non-type template parameter in C++ function templates?

a) To specify the return type of the function

b) To define the number of arguments accepted by the function

c) To specify a constant value that is not a data type

d) To restrict the data types that can be used with the function template
 

Answer: c) To specify a constant value that is not a data type
 

Explanation: Non-type template parameters in C++ function templates allow you to specify constant values (such as integers or pointers) that are not data types, providing additional flexibility in template usage.

 

  1. Which keyword is used to instantiate a function template with a specific data type in C++?

a) define

b) using

c) instantiate

d) template
 

Answer: c) instantiate
 

Explanation: In C++, the keyword used to instantiate a function template with a specific data type is 'instantiate'. You typically use the desired data type within angle brackets to specify the instantiation.

 

  1. Can function templates be overloaded in C++?

a) Yes, function templates can be overloaded based on the number of arguments

b) No, function templates cannot be overloaded

c) Yes, function templates can be overloaded based on the data types of arguments

d) Yes, but only with non-template functions
 

Answer: c) Yes, function templates can be overloaded based on the data types of arguments
 

Explanation: Function templates in C++ can be overloaded based on the data types of arguments, allowing you to define multiple function templates with the same name but different parameter types.


 

OVERLOADING FUNCTION TEMPLATE 

 

  1. What is function template overloading in C++?

a) Defining multiple function templates with the same name but different template parameter types

b) Defining multiple non-template functions with the same name but different parameter types

c) Overriding a function template with a specialized version for specific data types

d) Instantiating a function template with a specific data type
 

Answer: a) Defining multiple function templates with the same name but different template parameter types
 

Explanation: Function template overloading in C++ refers to the practice of defining multiple function templates with the same name but different template parameter types, allowing for more flexible usage of templates.

 

  1. Which of the following is true about function template overloading?

a) It allows defining multiple non-template functions with the same name and same parameter types

b) It restricts the use of function templates to a single data type

c) It is not supported in C++

d) It improves code readability but increases compilation time
 

Answer: a) It allows defining multiple non-template functions with the same name and same parameter types
 

Explanation: Function template overloading allows you to define multiple non-template functions with the same name but different parameter types, providing flexibility in function definitions.

 

  1. How is function template overloading achieved in C++?

a) By using the 'overload' keyword in front of the function template definition

b) By providing different template parameter types in separate function template definitions

c) By using the 'override' keyword in front of the function template definition

d) By instantiating the function template with different data types
 

Answer: b) By providing different template parameter types in separate function template definitions

 

Explanation: Function template overloading in C++ is achieved by providing different template parameter types in separate function template definitions, allowing the compiler to select the appropriate template based on the arguments passed.

 

  1. What is the primary advantage of function template overloading?

a) Improved performance

b) Reduced code duplication

c) Better memory management

d) Easier debugging
 

Answer: b) Reduced code duplication
 

Explanation: Function template overloading in C++ reduces code duplication by allowing you to define multiple versions of a function template with different parameter types, promoting code reusability.

 

  1. Can function template overloading be based solely on the return type of the function?

a) Yes

b) No

c) Only for non-template functions

d) Only for specialized function templates
 

Answer: b) No
 

Explanation: Function template overloading in C++ cannot be based solely on the return type of the function. Overloading is determined by the number and types of function parameters, not the return type.

 

  1. When resolving which overloaded function template to use, what is the precedence order in C++?

a) Most specialized version first, then the most general version

b) Most general version first, then the most specialized version

c) Any version can be chosen arbitrarily

d) Version with the smallest number of template parameters
 

Answer: a) Most specialized version first, then the most general version
 

Explanation: When resolving which overloaded function template to use, the compiler chooses the most specialized version first, and if none match, it falls back to the most general version.

 

  1. What happens if there is ambiguity between two overloaded function templates during compilation?

a) It results in a compilation error

b) The compiler randomly chooses one of the templates

c) The compiler selects the template with the fewest template parameters

d) The compiler selects the template with the most template parameters
 

Answer: a) It results in a compilation error
 

Explanation: If there is ambiguity between two overloaded function templates during compilation, it results in a compilation error because the compiler cannot determine which version to use.

 

  1. Can non-template functions be overloaded with function templates in C++?

a) Yes

b) No

c) Only if the non-template functions have the same name

d) Only if the non-template functions have different return types
 

Answer: a) Yes
 

Explanation: Non-template functions can be overloaded with function templates in C++, allowing you to provide different implementations based on the parameter types.

 

  1. Which keyword is used to explicitly specify a template parameter type in function template overloading?

a) template

b) typename

c) overload

d) specific
 

Answer: b) typename
 

Explanation: In function template overloading, the typename keyword is used to explicitly specify a template parameter type, helping resolve ambiguity and guide the compiler in selecting the correct overload.

 

  1. In function template overloading, can the function implementations differ in the number of template parameters?

a) Yes

b) No

c) Only if the number of template parameters is greater in the more specialized version

d) Only if the number of template parameters is fewer in the more specialized version
 

Answer: b) No
 

Explanation: In function template overloading, the function implementations must have the same number of template parameters.


 

CLASS TEMPLATE

 

What is a class template in C++?

a) A class with predefined data members and member functions

b) A generic class that can work with any data type

c) A class designed for inheritance

d) A class with static member functions only
 

Answer: b) A generic class that can work with any data type
 

Explanation: A class template in C++ is a generic class that can work with any data type. It allows you to define a class once and use it with different data types without rewriting the code.

 

How is a class template declared in C++?

a) Using the 'class' keyword followed by the class definition

b) Using the 'template' keyword followed by the class definition

c) Using the 'generic' keyword followed by the class definition

d) Using the 'typename' keyword followed by the class definition
 

Answer: b) Using the 'template' keyword followed by the class definition
 

Explanation: In C++, a class template is declared using the 'template' keyword followed by the class definition, specifying the template parameters.

 

Which symbol is commonly used to denote a placeholder for the template type in a class template?

a) #

b) &

c) *

d) T
 

Answer: d) T
 

Explanation: The symbol 'T' (or any other valid identifier) is commonly used to denote a placeholder for the template type in a class template in C++.

 

What is the primary advantage of using class templates in C++?

a) Improved performance

b) Reduced code duplication

c) Better memory management

d) Easier debugging
 

Answer: b) Reduced code duplication
 

Explanation: Class templates in C++ help reduce code duplication by allowing you to define a generic class that can work with multiple data types, promoting code reusability.

 

Can class templates be used with user-defined data types in C++?

a) Yes, but only with built-in data types

b) No, class templates only work with primitive data types

c) Yes, class templates can be used with any data type, including user-defined types

d) Yes, class templates can be used with classes but not structures
 

Answer: c) Yes, class templates can be used with any data type, including user-defined types
 

Explanation: Class templates in C++ can be used with any data type, including built-in types like int and float, as well as user-defined types such as classes and structures.

 

Which of the following is true about class template specialization in C++?

a) It allows specifying different implementations for specific data types

b) It restricts the use of class templates to a single data type

c) It is not supported in C++

d) It improves code readability but increases compilation time
 

Answer: a) It allows specifying different implementations for specific data types
 

Explanation: Class template specialization in C++ allows you to provide different implementations for specific data types while still benefiting from the generic behavior of class templates.

 

How is class template specialization defined in C++?

a) Using the 'specialize' keyword followed by the class template definition

b) Using the 'template' keyword followed by the specialized class definition

c) Using the 'template' keyword followed by the 'specialize' keyword

d) Using the 'template' keyword followed by the specialized class definition
 

Answer: d) Using the 'template' keyword followed by the specialized class definition
 

Explanation: Class template specialization in C++ is defined using the 'template' keyword followed by the specialized class definition for specific data types.

 

What is the purpose of a non-type template parameter in class templates in C++?

a) To specify the return type of the class member functions

b) To define the number of arguments accepted by the class member functions

c) To specify a constant value that is not a data type

d) To restrict the data types that can be used with the class template
 

Answer: c) To specify a constant value that is not a data type
 

Explanation: Non-type template parameters in class templates in C++ allow you to specify constant values (such as integers or pointers) that are not data types, providing additional flexibility in template usage.

 

Which keyword is used to explicitly specify a template parameter type in class templates?

a) template

b) typename

c) class

d) specific
 

Answer: b) typename
 

Explanation: In class templates, the typename keyword is used to explicitly specify a template parameter type, helping resolve ambiguity and guide the compiler in selecting the correct instantiation.

 

In class templates, can the member functions have different implementations for different template parameter types?

a) Yes

b) No

c) Only if the number of template parameters is greater in the more specialized version

d) Only if the number of template parameters is fewer in the more specialized version
 

Answer: a)


 

FUNCTION DEFINITION OF CLASS TEMPLATE  

 

In C++, how is a member function of a class template defined outside the class declaration?

a) Prefix the function definition with the 'template' keyword

b) Prefix the function definition with the 'class' keyword

c) Use the template declaration followed by the class name and function signature

d) Use the class name followed by the template parameters and function signature
 

Answer: d) Use the class name followed by the template parameters and function signature
 

Explanation: When defining a member function of a class template outside the class declaration in C++, you need to use the class name followed by the template parameters and function signature.

 

What is the syntax for defining a member function of a class template outside the class declaration?

a) template <typename T> ClassName<T>::FunctionName() { }

b) template <class T> ClassName<T>::FunctionName() { }

c) ClassName<T>::template FunctionName() { }

d) ClassName::template <T> FunctionName() { }
 

Answer: a) template <typename T> ClassName<T>::FunctionName() { }
 

Explanation: The correct syntax for defining a member function of a class template outside the class declaration in C++ is to use the 'template' keyword followed by the template parameter and the class name, followed by the function signature.

 

When defining a member function of a class template outside the class declaration, do you need to specify the template parameters again?

a) Yes, you must specify the template parameters again

b) No, template parameters are not needed in the function definition

c) Only if the function has a different set of template parameters

d) Only if the function is a static member function
 

Answer: a) Yes, you must specify the template parameters again
 

Explanation: When defining a member function of a class template outside the class declaration, you must specify the template parameters again, even if they were already declared in the class template.

 

What is the purpose of defining member functions of a class template outside the class declaration?

a) To improve code readability

b) To reduce compilation time

c) To separate interface from implementation

d) To enforce encapsulation
 

Answer: c) To separate interface from implementation
 

Explanation: Defining member functions of a class template outside the class declaration helps separate the interface (defined in the class declaration) from the implementation, making the code easier to maintain and understand.

 

In C++, can you define member functions of a class template inline within the class declaration?

a) Yes

b) No

c) Only if the member function is declared as 'static'

d) Only if the member function has no implementation
 

Answer: a) Yes
 

Explanation: In C++, you can define member functions of a class template inline within the class declaration, just like regular member functions of a non-template class.

 

When defining a member function of a class template outside the class declaration, do you need to use the 'template' keyword again?

a) Yes, the 'template' keyword must be used again

b) No, the 'template' keyword is not needed in the function definition

c) Only if the member function has a different set of template parameters

d) Only if the member function is a static member function
 

Answer: a) Yes, the 'template' keyword must be used again
 

Explanation: When defining a member function of a class template outside the class declaration in C++, you must use the 'template' keyword again to specify the template parameters.

 

What happens if you forget to specify the template parameters when defining a member function of a class template outside the class declaration?

a) It results in a compilation error

b) The compiler automatically infers the template parameters

c) It uses the template parameters declared in the class declaration

d) It causes a runtime error
 

Answer: a) It results in a compilation error
 

Explanation: Forgetting to specify the template parameters when defining a member function of a class template outside the class declaration in C++ results in a compilation error because the compiler cannot determine the correct instantiation.

 

In C++, can you specialize a member function of a class template outside the class declaration?

a) Yes

b) No

c) Only if the class template is not fully specialized

d) Only if the member function is declared as 'virtual'
 

Answer: b) No
 

Explanation: In C++, you cannot specialize a member function of a class template outside the class declaration. Specialization is only applicable to the entire class template, not individual member functions.

 

Can you define the member functions of a class template both inline and outside the class declaration?

a) Yes

b) No

c) Only if the member functions are declared as 'static'

d) Only if the class template is not fully specialized
 

Answer: a) Yes
 

Explanation: In C++, you can define the member functions of a class template both inline within the class declaration and outside the class declaration. This provides flexibility in organizing code based on its complexity.

 

When defining a member function of a class template outside the class declaration, do you need to specify the template keyword before the return type?

a) Yes

b) No

c) Only if the member function is a static member function

d) Only if the member function is a const member function
 

Answer: b) No
 

Explanation: When defining a member function of a class template outside the class declaration, you do not need to specify the 'template' keyword before the return type. You only need to specify the template parameters after the class name.

 

STANDARD TEMPLATE LIBRARY (Containers, Algorithms, Iterators), 


 

Which of the following containers in the C++ Standard Template Library (STL) provides constant-time insertion and removal of elements from both ends?

a) std::vector

b) std::list

c) std::deque

d) std::set
 

Answer: c) std::deque
 

Explanation: std::deque (double-ended queue) provides constant-time insertion and removal of elements from both ends, making it efficient for operations like push_front(), pop_front(), push_back(), and pop_back().

 

Which STL container does not allow duplicate elements and automatically sorts its elements?

a) std::vector

b) std::list

c) std::set

d) std::map
 

Answer: c) std::set
 

Explanation: std::set is an associative container in STL that does not allow duplicate elements and automatically sorts its elements in ascending order.

 

Which STL algorithm is used to sort elements in a container?

a) sort()

b) find()

c) reverse()

d) accumulate()
 

Answer: a) sort()
 

Explanation: The sort() algorithm in STL is used to sort the elements in a container in ascending order by default. It can be customized to sort in descending order or based on custom comparison functions.

 

Which iterator in STL allows traversal of elements in a container in both forward and reverse directions?

a) Forward iterator

b) Reverse iterator

c) Bidirectional iterator

d) Random access iterator
 

Answer: c) Bidirectional iterator
 

Explanation: Bidirectional iterators in STL allow traversal of elements in a container in both forward and reverse directions, supporting operations like ++ and -- for moving to the next and previous elements, respectively.

 

Which STL container is typically implemented as a doubly linked list?

a) std::vector

b) std::list

c) std::deque

d) std::map
 

Answer: b) std::list
 

Explanation: std::list in STL is typically implemented as a doubly linked list, where each element points to both its predecessor and successor, allowing efficient insertion and removal operations anywhere in the list.

 

Which STL algorithm is used to find the first occurrence of a value in a container?

a) search()

b) find()

c) find_first_of()

d) find_if()
 

Answer: b) find()
 

Explanation: The find() algorithm in STL is used to find the first occurrence of a value in a container. It returns an iterator to the found element or the end iterator if the value is not found.

 

Which STL container is implemented as a dynamic array that can automatically resize itself when needed?

a) std::vector

b) std::list

c) std::deque

d) std::array
 

Answer: a) std::vector
 

Explanation: std::vector in STL is implemented as a dynamic array that can automatically resize itself when needed to accommodate more elements. It provides contiguous memory storage and efficient random access.

 

Which STL algorithm is used to apply a binary operation to the elements in a container and accumulate the results?

a) transform()

b) accumulate()

c) reduce()

d) fold()
 

Answer: b) accumulate()
 

Explanation: The accumulate() algorithm in STL is used to apply a binary operation to the elements in a container and accumulate the results, starting with an initial value.

 

Which iterator in STL allows traversal of elements in a container in any order and supports arithmetic operations like + and -?

a) Forward iterator

b) Reverse iterator

c) Bidirectional iterator

d) Random access iterator
 

Answer: d) Random access iterator
 

Explanation: Random access iterators in STL allow traversal of elements in a container in any order and support arithmetic operations like + and -, allowing efficient random access to elements.

 

Which STL container stores elements in a sorted order based on a key and allows fast retrieval of elements using the key?

a) std::vector

b) std::list

c) std::deque

d) std::map
 

Answer: d) std::map
 

Explanation: std::map in STL is an associative container that stores elements in a sorted order based on a key. It allows fast retrieval of elements using the key and does not allow duplicate keys.

 

EXCEPTION HANDLING CONSTRUCT  (TRY , CATCH , THROW)

 

In C++, which keyword is used to start a block of code that may throw an exception?

a) start

b) throw

c) catch

d) try
 

Answer: d) try
 

Explanation: In C++, the try keyword is used to start a block of code where exceptions may be thrown. It is followed by the block of code that needs to be monitored for exceptions.

 

What is the purpose of the catch block in C++ exception handling?

a) To throw an exception

b) To handle the exception thrown by the try block

c) To terminate the program

d) To return a value from the try block
 

Answer: b) To handle the exception thrown by the try block
 

Explanation: The catch block in C++ exception handling is used to handle the exception thrown by the try block. It specifies the type of exception it can handle and the actions to be taken when that exception occurs.

 

Which keyword is used to explicitly throw an exception in C++?

a) try

b) catch

c) throw

d) resume
 

Answer: c) throw
 

Explanation: In C++, the throw keyword is used to explicitly throw an exception. It is followed by the exception object that describes the error condition.

 

Can a single try block have multiple catch blocks in C++?

a) Yes

b) No

c) Only if the catch blocks handle different exception types

d) Only if the catch blocks handle the same exception type
 

Answer: a) Yes
 

Explanation: A single try block can have multiple catch blocks in C++. Each catch block can handle a different type of exception or the same type of exception with different handling logic.

 

Which catch block should appear first if multiple catch blocks are used for handling exceptions of different types?

a) The catch block for the most derived exception type

b) The catch block for the base exception type

c) The catch block for the exception type with the highest priority

d) The catch block for the exception type with the lowest priority
 

Answer: a) The catch block for the most derived exception type
 

Explanation: When multiple catch blocks are used for handling exceptions of different types in C++, the catch block for the most derived exception type should appear first, followed by less derived types.

 

What happens if an exception is thrown inside a catch block in C++?

a) The program terminates

b) The exception is ignored

c) The catch block is executed again

d) The exception is propagated to the next enclosing try block or caught by an outer catch block
 

Answer: d) The exception is propagated to the next enclosing try block or caught by an outer catch block
 

Explanation: If an exception is thrown inside a catch block in C++, the exception is propagated to the next enclosing try block or caught by an outer catch block, allowing for further handling.

 

Can the catch block in C++ exception handling specify multiple exception types?

a) Yes

b) No

c) Only if the exception types are related through inheritance

d) Only if the exception types are unrelated
 

Answer: a) Yes
 

Explanation: In C++ exception handling, a catch block can specify multiple exception types separated by the OR (|) operator, allowing it to handle exceptions of different types.

 

Which block is optional in C++ exception handling?

a) try block

b) catch block

c) throw block

d) Both try and catch blocks are mandatory
 

Answer: b) catch block
 

Explanation: In C++ exception handling, the catch block is optional. You can have a try block without any associated catch blocks, but the opposite is not true.

 

Which standard exception class in C++ is used as a base class for all standard runtime exception classes?

a) std::exception

b) std::runtime_error

c) std::logic_error

d) std::bad_alloc
 

Answer: a) std::exception
 

Explanation: std::exception is the base class for all standard runtime exception classes in C++. It provides a common interface for accessing the exception's information.

 

In C++ exception handling, what happens if an exception is thrown outside of a try block?

a) The program terminates

b) The exception is caught by the catch block within the nearest try block

c) The exception is caught by the catch block within the main() function

d) The program continues execution without handling the exception
 

Answer: a) The program terminates
 

Explanation: If an exception is thrown outside of a try block in C++, and there is no associated catch block to handle it, the program terminates abruptly.



 

MULTIPLE EXCEPTION HANDLING

 

In C++, can a single catch block handle multiple types of exceptions?

a) Yes

b) No

c) Only if the exceptions are related through inheritance

d) Only if the exceptions are unrelated
 

Answer: a) Yes
 

Explanation: In C++, a single catch block can handle multiple types of exceptions by specifying them separated by the OR (|) operator, allowing for more concise exception handling code.

 

What is the benefit of using a single catch block to handle multiple types of exceptions in C++?

a) It reduces code duplication

b) It increases code readability

c) It improves performance

d) It ensures more precise exception handling
 

Answer: a) It reduces code duplication
 

Explanation: Using a single catch block to handle multiple types of exceptions in C++ reduces code duplication by consolidating exception handling logic for related exceptions, leading to cleaner and more maintainable code.

 

Can the catch block for handling multiple types of exceptions in C++ determine the specific type of the caught exception?

a) Yes

b) No

c) Only if the exception types are unrelated

d) Only if the exception types are related through inheritance
 

Answer: a) Yes
 

Explanation: In C++, the catch block for handling multiple types of exceptions can determine the specific type of the caught exception through dynamic_cast or typeid operators, allowing for different handling logic based on the exception type.

 

Which keyword is used to specify multiple exception types in a single catch block in C++?

a) and

b) or

c) &

d) |
 

Answer: d) |
 

Explanation: In C++, the | (OR) operator is used to specify multiple exception types in a single catch block, allowing the catch block to handle exceptions of any of the specified types.

 

What happens if an exception is thrown that is not handled by any catch block in C++?

a) The program terminates

b) The exception is caught by the catch block within the nearest try block

c) The exception is caught by the catch block within the main() function

d) The program continues execution without handling the exception
 

Answer: a) The program terminates
 

Explanation: If an exception is thrown that is not handled by any catch block in C++, the program terminates abruptly, unless there is an appropriate catch block in an enclosing try block or within the main() function.

 

Can multiple catch blocks be used within a single try block in C++?

a) Yes

b) No

c) Only if the catch blocks handle unrelated exception types

d) Only if the catch blocks handle related exception types
 

Answer: a) Yes
 

Explanation: Multiple catch blocks can be used within a single try block in C++, allowing for different handling logic for different types of exceptions that may be thrown within the try block.

 

When multiple catch blocks are used within a try block in C++, in what order should they be specified?

a) In any order

b) From most specific to most general

c) From most general to most specific

d) Alphabetically based on exception type names
 

Answer: b) From most specific to most general
 

Explanation: When multiple catch blocks are used within a try block in C++, they should be specified from most specific to most general, ensuring that more specialized exceptions are handled before more general ones.

 

Can catch blocks in C++ catch exceptions by reference?

a) Yes

b) No

c) Only if the exception types are unrelated

d) Only if the exception types are related through inheritance
 

Answer: a) Yes
 

Explanation: Catch blocks in C++ can catch exceptions by reference, allowing for efficient exception handling and access to the exception object's state.

 

Which catch block will be executed if an exception is thrown that matches multiple catch blocks within the same try block in C++?

a) The first catch block that matches the exception type

b) The last catch block that matches the exception type

c) All catch blocks that match the exception type

d) None of the catch blocks will be executed
 

Answer: a) The first catch block that matches the exception type
 

Explanation: If an exception is thrown that matches multiple catch blocks within the same try block in C++, only the first catch block that matches the exception type will be executed, and subsequent catch blocks will be skipped.

 

What is the term used to describe the process of throwing an exception from within a catch block in C++?

a) Nested exception

b) Re-throwing

c) Recursive exception

d) Catching again
 

Answer: b) Re-throwing
 

Explanation: The process of throwing an exception from within a catch block in C++ is called re-throwing, where the caught exception is thrown again to be handled by an outer try block or caught by another catch block.


 

RETHROWING EXCEPTION

 

What does rethrowing an exception mean in C++?

a) Throwing an exception again within the same catch block

b) Propagating an exception to an outer try block for further handling

c) Handling an exception multiple times within nested catch blocks

d) Catching an exception and ignoring it
 

Answer: b) Propagating an exception to an outer try block for further handling
 

Explanation: Rethrowing an exception in C++ refers to throwing an exception again within a catch block, typically to propagate it to an outer try block or to a higher level of the call stack for further handling.

 

In C++, how is an exception rethrown within a catch block?

a) Using the throw keyword without any exception object

b) Using the throw keyword followed by the caught exception object

c) Using the throw keyword followed by a new exception object

d) Using the catch keyword followed by the caught exception object
 

Answer: b) Using the throw keyword followed by the caught exception object
 

Explanation: To rethrow an exception within a catch block in C++, you use the throw keyword followed by the caught exception object, allowing the exception to be propagated to an outer try block or caught by another catch block.

 

When might rethrowing an exception be useful in C++ exception handling?

a) To ignore the exception and continue program execution

b) To prevent the exception from being caught by an outer try block

c) To handle the exception multiple times within nested catch blocks

d) To delegate the handling of the exception to a higher level in the call stack
 

Answer: d) To delegate the handling of the exception to a higher level in the call stack
 

Explanation: Rethrowing an exception in C++ can be useful when you want to delegate the handling of the exception to a higher level in the call stack, allowing it to be caught and handled by an outer try block or by the calling function.

 

What happens if an exception is rethrown but there is no enclosing try block to catch it?

a) The program terminates

b) The exception is ignored

c) The rethrown exception is caught by the catch block within the same try block

d) The exception is caught by the catch block within the main() function
 

Answer: a) The program terminates
 

Explanation: If an exception is rethrown but there is no enclosing try block to catch it, the program terminates abruptly unless there is an appropriate catch block in an outer try block or within the main() function to handle the exception.

 

In C++, can you modify the caught exception object before rethrowing it?

a) Yes

b) No

c) Only if the exception object is a const reference

d) Only if the exception object is a non-const reference
 

Answer: a) Yes
 

Explanation: In C++, you can modify the caught exception object before rethrowing it. However, it is generally recommended to rethrow the exception without modifications to avoid confusion in exception handling.

 

Which keyword is used to explicitly rethrow an exception in C++?

a) throw

b) catch

c) rethrow

d) propagate
 

Answer: a) throw
 

Explanation: In C++, the throw keyword is used to explicitly rethrow an exception within a catch block, allowing the caught exception to be propagated to an outer try block or caught by another catch block.

 

Can rethrowing an exception change the exception's stack unwinding behavior in C++?

a) Yes

b) No

c) Only if the rethrown exception is caught by a different catch block

d) Only if the rethrown exception is caught by the same catch block
 

Answer: a) Yes
 

Explanation: Rethrowing an exception can change the exception's stack unwinding behavior in C++. If the rethrown exception is caught by a different catch block, the stack unwinding will continue from the point of rethrowing.

 

What is the primary purpose of rethrowing an exception in C++?

a) To handle the exception locally within the same catch block

b) To suppress the exception and continue program execution

c) To delegate the handling of the exception to an outer try block or calling function

d) To create a new exception object with the same error message
 

Answer: c) To delegate the handling of the exception to an outer try block or calling function
 

Explanation: The primary purpose of rethrowing an exception in C++ is to delegate the handling of the exception to an outer try block or the calling function, allowing for more centralized and appropriate exception handling.

 

Can you rethrow an exception without catching it first in C++?

a) Yes

b) No

c) Only if the exception type is predefined in the standard library

d) Only if the exception type is custom-defined by the user
 

Answer: b) No
 

Explanation: In C++, you cannot rethrow an exception without catching it first. Rethrowing requires catching the exception within a catch block and then explicitly using the throw keyword to rethrow it.

 

How does rethrowing an exception affect the call stack in C++?

a) It adds a new entry to the call stack

b) It removes the current entry from the call stack

c) It resumes execution from the point of rethrowing

d) It unwinds the call stack until an appropriate catch block is found
 

Answer: d) It unwinds the call stack until an appropriate catch block is found
 

Explanation: Rethrowing an exception in C++ unwinds the call stack until an appropriate catch block is found, allowing the exception to be caught and handled by an outer try block or calling function.

 

CATCHING ALL EXCEPTION

 

What is the purpose of catching all exceptions in C++?

a) To ignore specific exceptions and continue program execution

b) To catch any exception that may occur and handle it uniformly

c) To handle only standard exceptions defined by the C++ standard library

d) To improve performance by reducing the number of catch blocks
 

Answer: b) To catch any exception that may occur and handle it uniformly
 

Explanation: Catching all exceptions in C++ allows for uniform handling of any exception that may occur, providing a centralized place to handle errors and ensure graceful program termination.

 

Which catch block syntax is used to catch all exceptions in C++?

a) catch ()

b) catch (...)

c) catch (Exception e)

d) catch (std::exception& e)
 

Answer: b) catch (...)
 

Explanation: The catch (...) syntax in C++ is used to catch all exceptions, regardless of their types. It serves as a catch-all mechanism for handling any exception that is thrown.

 

Can catching all exceptions affect the program's performance in C++?

a) Yes, it may degrade performance due to increased overhead

b) No, catching all exceptions has no impact on performance

c) Only if the catch block is placed within a loop

d) Only if the catch block rethrows the caught exception
 

Answer: a) Yes, it may degrade performance due to increased overhead
 

Explanation: Catching all exceptions in C++ may degrade performance due to increased overhead, especially if the catch block is executed frequently or if the catch block is placed within a performance-sensitive code section.

 

What is the recommended approach for catching all exceptions in C++?

a) Using specific catch blocks for known exception types

b) Using a single catch-all block for handling all exceptions uniformly

c) Using try-catch blocks only for critical sections of code

d) Using catch blocks with no parameters to catch all exceptions
 

Answer: a) Using specific catch blocks for known exception types
 

Explanation: The recommended approach for exception handling in C++ is to use specific catch blocks for known exception types whenever possible, providing targeted handling for different types of errors.

 

In C++, can you access information about the caught exception when using a catch-all block?

a) Yes, through the exception object passed as a parameter

b) No, catch-all blocks do not provide access to exception information

c) Only if the catch block rethrows the caught exception

d) Only if the catch block uses the typeid operator
 

Answer: a) Yes, through the exception object passed as a parameter
 

Explanation: In C++, you can access information about the caught exception when using a catch-all block by examining the exception object passed as a parameter to the catch block.

 

What is a potential drawback of using a catch-all block in C++?

a) It may lead to incorrect exception handling logic

b) It may hide specific errors and make debugging difficult

c) It may increase the size of the executable file

d) It may cause memory leaks
 

Answer: b) It may hide specific errors and make debugging difficult
 

Explanation: A potential drawback of using a catch-all block in C++ is that it may hide specific errors by handling them uniformly, making it difficult to identify the root cause of the problem during debugging.

 

When catching all exceptions in C++, should you rethrow the caught exception?

a) Yes, to ensure proper propagation of the exception

b) No, rethrowing exceptions caught by a catch-all block is not recommended

c) Only if the catch block performs additional error handling

d) Only if the catch block is nested within another try block
 

Answer: b) No, rethrowing exceptions caught by a catch-all block is not recommended
 

Explanation: It is generally not recommended to rethrow exceptions caught by a catch-all block in C++. Rethrowing can lead to loss of exception information and make debugging more challenging.

 

Can you use multiple catch blocks along with a catch-all block in C++?

a) Yes

b) No

c) Only if the catch-all block is the first catch block

d) Only if the catch-all block is the last catch block
 

Answer: a) Yes
 

Explanation: In C++, you can use multiple catch blocks along with a catch-all block, allowing for specific exception handling in addition to handling any remaining exceptions uniformly.

 

Which approach is more preferable when handling exceptions in C++: catching specific exceptions or using a catch-all block?

a) Catching specific exceptions

b) Using a catch-all block

c) Both approaches are equally preferable

d) It depends on the complexity of the code and error handling requirements
 

Answer: a) Catching specific exceptions
 

Explanation: Catching specific exceptions is generally more preferable in C++ as it allows for targeted error handling and better code readability. Catch-all blocks should be used sparingly and only when necessary.

 

What should you consider when using a catch-all block in C++?

a) The catch-all block should be placed at the beginning of the try block

b) The catch-all block should handle exceptions without any logging or error reporting

c) The catch-all block should be used as the only catch block in the program

d) The catch-all block should be used judiciously and complemented with specific catch blocks

 

EXCEPTION WITH ARGUMENT 

 

In C++, how can you pass additional information when throwing an exception?

a) By using the throw keyword followed by the error message

b) By using the throw keyword followed by an exception object

c) By using the catch block to extract information from the call stack

d) By using the return statement within the try block
 

Answer: b) By using the throw keyword followed by an exception object
 

Explanation: In C++, additional information can be passed when throwing an exception by creating an exception object and throwing it using the throw keyword.

 

Which data type can be used to pass additional information with an exception object in C++?

a) int

b) double

c) std::string

d) char
 

Answer: c) std::string
 

Explanation: In C++, std::string is commonly used to pass additional information with an exception object because it allows for flexible and descriptive error messages.

 

What is the syntax for throwing an exception with an argument in C++?

a) throw ExceptionType(argument);

b) throw(argument);

c) throw ExceptionType(argument);

d) throw ExceptionType argument;
 

Answer: c) throw ExceptionType(argument);
 

Explanation: The syntax for throwing an exception with an argument in C++ involves using the throw keyword followed by the exception type and the argument enclosed in parentheses.

 

How can you access the additional information passed with an exception object in a catch block in C++?

a) Using the catch block's parameter list

b) By querying the exception object's internal state

c) By accessing the exception object's data members directly

d) By using the typeid operator
 

Answer: a) Using the catch block's parameter list
 

Explanation: In C++, the additional information passed with an exception object can be accessed within a catch block by using the catch block's parameter list, which binds the exception object to a local variable.

 

Which approach is preferable for passing additional information with exceptions in C++?

a) Passing error codes as integers

b) Passing error messages as string literals

c) Creating custom exception classes with relevant data members

d) Using global variables to store error information
 

Answer: c) Creating custom exception classes with relevant data members
 

Explanation: The preferable approach for passing additional information with exceptions in C++ is to create custom exception classes with relevant data members, allowing for structured and meaningful error reporting.

 

When creating a custom exception class in C++, what should you consider when designing the class?

a) Including all possible error messages in the class

b) Keeping the class simple with no data members

c) Adding data members to provide relevant context for the exception

d) Inheriting from the std::exception class directly
 

Answer: c) Adding data members to provide relevant context for the exception
 

Explanation: When designing a custom exception class in C++, it's important to add data members that provide relevant context for the exception, such as error codes, error messages, or other pertinent information.

 

Can you throw built-in data types such as int or double as exceptions in C++?

a) Yes

b) No

c) Only if the data type is explicitly converted to an exception object

d) Only if the data type is cast to a pointer before throwing
 

Answer: b) No
 

Explanation: In C++, you cannot throw built-in data types such as int or double as exceptions directly. Exceptions are typically thrown using exception objects, which are instances of exception classes.

 

How can you ensure proper handling of exceptions with arguments in C++?

a) By catching all exceptions and printing their arguments

b) By using try-catch blocks with specific exception types

c) By relying on the compiler to automatically handle exceptions with arguments

d) By avoiding exceptions altogether and using return codes for error handling
 

Answer: b) By using try-catch blocks with specific exception types
 

Explanation: Proper handling of exceptions with arguments in C++ involves using try-catch blocks with specific exception types to ensure that the appropriate error handling logic is executed based on the type of exception thrown.

 

Can you pass multiple arguments with an exception object when throwing an exception in C++?

a) Yes

b) No

c) Only if the arguments are concatenated into a single string

d) Only if the arguments are passed as an array
 

Answer: b) No
 

Explanation: In C++, you cannot directly pass multiple arguments with an exception object when throwing an exception. However, you can create a custom exception class with multiple data members to represent different aspects of the error.

 

What is the purpose of passing additional information with exceptions in C++?

a) To provide context for the error and aid in debugging

b) To increase the size of the exception object

c) To hide the error details from the programmer

d) To improve performance by reducing the overhead of exception handling
 

Answer: a) To provide context for the error and aid in debugging
 

Explanation: Passing additional information with exceptions in C++ helps provide context for the error, aiding in debugging and making it easier to identify and address the root cause of the problem.

 

EXCEPTION SPECIFICATION FOR FUNCTION

 

What is an exception specification in C++?

a) A set of rules specifying when and how exceptions are thrown in a function

b) A mechanism for specifying the types of exceptions that a function can throw

c) A built-in feature for automatically handling exceptions within a function

d) A directive indicating whether a function should throw exceptions or not
 

Answer: b) A mechanism for specifying the types of exceptions that a function can throw
 

Explanation: An exception specification in C++ allows you to specify the types of exceptions that a function can throw, providing information to callers about potential error conditions.

 

Which keyword is used to specify an exception specification for a function in C++?

a) throw

b) catch

c) noexcept

d) except
 

Answer: a) throw
 

Explanation: In C++, the throw keyword is used to specify an exception specification for a function, followed by the types of exceptions that the function can throw.

 

What does the noexcept specifier indicate in a function declaration in C++?

a) That the function cannot throw any exceptions

b) That the function must throw at least one exception

c) That the function may throw any type of exception

d) That the function can throw exceptions based on runtime conditions
 

Answer: a) That the function cannot throw any exceptions
 

Explanation: The noexcept specifier in C++ indicates that the function cannot throw any exceptions. If an exception is thrown within a noexcept function, the program will terminate.

 

Can a C++ function have both an exception specification and a noexcept specifier?

a) Yes

b) No

c) Only if the function is defined in a header file

d) Only if the function is marked as inline
 

Answer: a) Yes
 

Explanation: In C++, a function can have both an exception specification (using the throw keyword) and a noexcept specifier, providing additional information about the function's exception-handling behavior.

 

Which of the following is true about noexcept specifications in C++?

a) They guarantee that a function will not throw exceptions under any circumstances

b) They allow functions to throw exceptions conditionally based on runtime factors

c) They are used to specify the types of exceptions that a function can throw

d) They are deprecated in modern C++ and should be avoided
 

Answer: a) They guarantee that a function will not throw exceptions under any circumstances
 

Explanation: noexcept specifications in C++ guarantee that a function will not throw exceptions under any circumstances, improving code reliability and performance.

 

What happens if a function marked as noexcept throws an exception during execution?

a) The program continues execution normally

b) The program enters an undefined state

c) The program terminates immediately

d) The exception is caught by an outer try-catch block
 

Answer: b) The program enters an undefined state
 

Explanation: If a function marked as noexcept throws an exception during execution, the program enters an undefined state, leading to potential program termination or other unpredictable behavior.

 

Which keyword is used to specify that a function may throw any type of exception?

a) throw

b) catch

c) noexcept

d) anythrow
 

Answer: d) anythrow
 

Explanation: In C++, the anythrow keyword is used to specify that a function may throw any type of exception, providing flexibility in exception handling.

 

In C++, what does it mean if a function's exception specification is declared as noexcept(true)?

a) The function cannot throw any exceptions

b) The function may throw exceptions

c) The function will throw exceptions based on runtime conditions

d) The function is marked as deprecated and should not be used
 

Answer: a) The function cannot throw any exceptions
 

Explanation: In C++, noexcept(true) specifies that the function cannot throw any exceptions, providing a compile-time guarantee of exception safety.

 

Can a function's exception specification include specific exception types?

a) Yes

b) No

c) Only if the function is declared as noexcept

d) Only if the function is declared as throw
 

Answer: a) Yes
 

Explanation: A function's exception specification in C++ can include specific exception types using the throw keyword, indicating the types of exceptions that the function may throw.

 

Which of the following is a benefit of using noexcept specifications in C++?

a) Improved code readability

b) Better runtime performance

c) Enhanced exception handling

d) Increased compiler compatibility
 

Answer: b) Better runtime performance
 

Explanation: noexcept specifications in C++ can lead to better runtime performance by allowing the compiler to optimize code generation, as it can assume that no exceptions will be thrown from noexcept functions.


 

Handling Uncaught and Unexpected Exceptions. (ACtE0306)

 

What is an uncaught exception in C++?

a) An exception that is caught by a catch block

b) An exception that is thrown but not handled by any catch block

c) An exception thrown outside of a try block

d) An exception that is thrown explicitly using the throw keyword
 

Answer: b) An exception that is thrown but not handled by any catch block
 

Explanation: An uncaught exception in C++ refers to an exception that is thrown but not handled by any catch block, potentially leading to program termination.

 

How does C++ handle an uncaught exception?

a) It ignores the exception and continues program execution

b) It prints an error message and continues program execution

c) It terminates the program

d) It automatically retries the function call that caused the exception
 

Answer: c) It terminates the program
 

Explanation: In C++, an uncaught exception results in program termination, preventing further execution to avoid potential undefined behavior.

 

What is an unexpected exception in C++?

a) An exception that occurs due to a logical error in the program

b) An exception that is thrown by a function that does not declare it

c) An exception that occurs during dynamic memory allocation

d) An exception that is thrown by the standard library functions
 

Answer: b) An exception that is thrown by a function that does not declare it
 

Explanation: An unexpected exception in C++ occurs when a function throws an exception that is not declared in its exception specification (throw clause).

 

How can you handle unexpected exceptions in C++?

a) By using try-catch blocks to catch all exceptions

b) By specifying catch-all blocks for all functions

c) By using the unexpected handler mechanism

d) By explicitly rethrowing all exceptions
 

Answer: c) By using the unexpected handler mechanism
 

Explanation: In C++, unexpected exceptions can be handled by providing a custom unexpected handler using std::set_unexpected, which allows you to define custom behavior when an unexpected exception occurs.

 

Which standard library function is used to set a custom unexpected handler in C++?

a) std::set_exception_handler

b) std::set_unexpected

c) std::set_exception

d) std::set_uncaught_exception_handler
 

Answer: b) std::set_unexpected
 

Explanation: In C++, std::set_unexpected is used to set a custom unexpected handler, allowing you to define custom behavior for unexpected exceptions.

 

What happens if an unexpected exception occurs in C++ and no custom unexpected handler is provided?

a) The program terminates

b) The exception is caught by the catch block within the nearest try block

c) The exception is caught by the catch block within the main() function

d) The exception is ignored
 

Answer: a) The program terminates
 

Explanation: If an unexpected exception occurs in C++ and no custom unexpected handler is provided, the program terminates abruptly to avoid potential undefined behavior.

 

Can you have multiple unexpected handlers in a C++ program?

a) Yes

b) No

c) Only if they are defined in separate translation units

d) Only if they are defined in the same translation unit
 

Answer: b) No
 

Explanation: In C++, only one unexpected handler can be active at a time. If multiple unexpected handlers are defined, the last one set using std::set_unexpected takes precedence.

 

What is the purpose of providing a custom unexpected handler in C++?

a) To catch all exceptions thrown by the program

b) To handle exceptions that occur during program termination

c) To define custom behavior for unexpected exceptions

d) To prevent exceptions from being thrown in the program
 

Answer: c) To define custom behavior for unexpected exceptions
 

Explanation: The purpose of providing a custom unexpected handler in C++ is to define custom behavior for unexpected exceptions, allowing you to handle them gracefully or perform cleanup operations before program termination.

 

What does the unexpected handler do in C++ when an unexpected exception occurs?

a) It terminates the program

b) It prints an error message and continues program execution

c) It calls std::terminate to terminate the program

d) It rethrows the unexpected exception
 

Answer: c) It calls std::terminate to terminate the program
 

Explanation: In C++, the unexpected handler, when invoked due to an unexpected exception, calls std::terminate to terminate the program, unless a custom unexpected handler has been provided.

 

Can you recover from an unexpected exception in C++?

a) Yes, by defining a custom recovery mechanism in the unexpected handler

b) No, once an unexpected exception occurs, the program terminates

c) Only if the unexpected exception is caught by a catch block

d) Only if the unexpected handler rethrows the exception
 

Answer: b) No, once an unexpected exception occurs, the program terminates
 

Explanation: In C++, once an unexpected exception occurs, the program terminates, and recovery is not possible. It is essential to handle unexpected exceptions proactively to avoid program termination.