Object Oriented Programming: Chapter 5: Templates and Exception Handling

Templates and Exception Handling: Two Marks Important Questions and Answers

C++ Object Oriented Programming

Object Oriented Programming C++: Chapter 5: Templates and Exception Handling : Anna University Part A Two Marks Important Questions and Answers

Object Oriented Programming

Chapter 5: Templates and Exception Handling

 

Two Marks Questions with Answers

 

1. What is a difference between a function template and template function ?

Answer: Function template is a generic programming technique in which a framework for the desired functionality is created and using various data types the implantation is used.

For example ‒

void add(T &a,T &b)

{

}

The template function is invoking each implementation. For example –

add(10,30);

add(11.11,22.22)

 

2. Write the benefits of using the template functions in C++.

Answer: When identical operations are performed on the elements of belonging to different data types then for each data type a separate function needs to be written. Using function template a single function can be written to which the appropriate data type is passed as an argument. Thus template functions brings the reusability of the code and thus the code becomes compact.

 

3. Distinguish between function overload and function templates.

Answer: : Function overloading allows the definition of more than one function with the same name and provides a means of choosing between the functions based on parameter matching. Template functions tell the compiler how to create new functions, based on the instantiation data types. Functions generated from the template behaves like normal functions.

The function body will differ in function overloading whereas the function body will not differ in function template.

 

4. What is a template?

        OR

What is the use of templates ?

Answer: Template is a tool for general programming that allows to write the code for any data type element.

 

5. What do you mean by the term 'Generic Programming'

Answer: :

•  The generic programming is a technique that allows to write the code for any data type elements.

• The template is used as a tool for generic programming.

 

6. Define exception. Give example.

Answer: Exceptions is a mechanism that handles any unavoidable circumstances that may occur in the program. C++ provides built in error handling mechanism which is known as exception handling. The exception handling is done using three keywords try, catch and throw.

For example following function makes use of exception handling mechanism ‒   

void divide()

{

double a,b;

cout<<"\n Enter the values of a and b";

cin>>a>>b;

try

{

      if(b= = 0)

            throw b;

      cout<<"Result: "<<a/b<<endl;

}

catch (double b)

{

      cout<<Can't Divide by zero"<<endl;

}

}

 

7. What is try‒catch block ?

Answer: The try‒catch block is used for handling the exceptions. The try block contains the portion of the code that is to be examined for error detection. Using catch block the exception is caught and processed.

 

8. What is the purpose of throw ?

Answer:: The throw is used to throw or raise an exception

 

9. In C++, how do we handle multiple exceptions.

Answer: By using multiple catch statements the multiple exceptions can be thrown. This is how the multiple exceptions are handled in C++.

 

Object Oriented Programming: Chapter 5: Templates and Exception Handling : Tag: Oops, Computer Programming : C++ Object Oriented Programming - Templates and Exception Handling: Two Marks Important Questions and Answers


Object Oriented Programming: Chapter 5: Templates and Exception Handling



Under Subject


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs) - Laboratory

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation