Technical Interview Questions and Answers :: C++
111 / 187
• Templates allow implementing the concept of generic programming.
• Template allows us to generate a family of classes or a family of functions to handle different data types.
• Template classes and functions eliminate code duplication for different types and thus make the program development easier and more manageable.
• We can use multiple parameters in both the class templates and function templates.
• Like other functions, templates can be overloaded.
112 / 187
Some peculiar problems other than logic or syntax errors, they are known as exceptions. Exceptions are run-time anomalies or unusual conditions that a program may encounter while executing. Anomalies might include the condition such a division by zero, array outside of bound etc.
113 / 187
Exception handling mechanism is basically built upon through keywords namely try, catch, and throw. The keyword try is used to preface a block of statements which may generate an exception. This block of statements is known as try block. When an exception is detected, it is thrown using a throw statement in the try block. A catch statement is defined by the keyword catch “catches†the exception thrown by the throw statement in the try block.
114 / 187
The purpose of exception handling mechanism is to provide means to detect and report an “exception circumstanceâ€. So that appropriate action can be taken. The mechanism suggests a separate error handling code that performs the following tasks.
1. Find the problem (hit the exception).
2. Inform than an error has occurred (throw the exception).
3. Receives the error information (catch the exception).
4. Take corrective action (handle the exception).
115 / 187
When an exception that is desired to be handled is detected, it is thrown using throw statement in one of the following forms:
Throw(exception)
Throw exception;
Throw; // used for re-throwing an exception.
116 / 187
It is possible that a program segment has more than one condition to throw an exception. In such case we can associate more than one catch statement with a try. For eg:
Try
{
//try block;
}
catch (type 1 arg)
{
// catch block1;
}
catch (type 2 arg)
{
// catch block 2;
}
……………….
……………….
Catch (type n arg)
{
// catch block n;
}
117 / 187
A handler may decide to rethrow the exception caught without processing it. In such situation, we may simply invoke throw without any argument.
Eg: throw;
This cause the current exception to be thrown to the next enclosing try/catch sequence and is caught by a catch statement listed after that enclosing try block.
118 / 187
A collection of generic classes and function is called the standard template library (STL). STL components are part of c++ standard library. STL consist of three main components. They are
• Containers
• Algorithms
• Iterators
119 / 187
A container is an object that actually stores data. It is a way data is organized in memory, the STL containers are implemented by template classes and therefore can be easily customized to hold different types of data.
120 / 187
There are three major categories of containers:
• Sequence container
1) Vectors
2) Dequeue
3) list
• Associative container
1. set
2. multiset
3. map
4. multimap
• Derived container
1. stack
2. queue
3. priority queue
You can check latest and updated Important C++ Interview Questions and Answers on this page. You can filter the question set by the company's name. Looking for the right answer? Don't worry! You can click on view answer section and check the answer with the explanation. You can discuss the answer in the forum with other users easily. Login with Facebook and save the answer To Reading List right now!
Our site contains top IT companies placement paper and mock test to help the freshers and experienced candidate to get hands on experience on the pattern and type of questions. We have curated all the C++ technical interview questions and answers from previous candidates.