What is the primary purpose of function templates in C++?
- Code obfuscation
- Memory conservation
- Code reusability
- Exception handling
Function templates in C++ enable the creation of functions that can operate on different data types without having to rewrite the entire function for each type. This promotes code reusability and reduces redundancy.
Which STL container provides constant time access to elements but may take linear time to insert a new element?
- std::vector
- std::queue
- std::array
- std::list
The std::vector container provides constant time (O(1)) access to elements using random access iterators. However, insertions (especially in the middle or at the beginning) might take linear time (O(n)) as elements need to be shifted to make space for the new element.
Consider a class hierarchy with Base and Derived classes. An object of Derived throws an exception that is caught in a catch block for Base exceptions. What considerations might be relevant to the use of dynamic_cast inside the catch block?
- Checking the type of the exception.
- Re-throwing the exception.
- Modifying the caught object in the catch block.
- Use of multiple inheritance.
When an exception of Derived type is caught as its base type, using dynamic_cast can help determine the exact type of the caught exception. This can be useful if you want to take specific actions based on the exact type of the exception. Remember, dynamic_cast will return a nullptr if the cast is not valid for pointers or throw a std::bad_cast exception for references, so it's essential to check for these cases.
What would be the result of instantiating a class template with a user-defined type that does not meet the template’s expected type requirements?
- A runtime error
- A compilation error
- The template would adapt automatically
- The system would crash
Instantiating a class template with a type that doesn't meet the template's requirements would result in a compilation error. Templates rely on the types they are instantiated with to adhere to specific criteria. If those criteria aren't met, the compiler will produce an error message indicating the discrepancies.
Which loop structure is guaranteed to execute at least once even if the condition is false?
- for loop
- while loop
- do-while loop
- switch-case
The do-while loop is designed to execute its block of code at least once before checking the condition. This ensures that the loop body runs even if the condition is false from the start.
In which scenarios could the use of goto be considered acceptable or even advisable?
- To enhance the readability of deeply nested loops.
- To replace exception handling mechanisms.
- For breaking out of multiple nested loops.
- To implement recursive functions.
While the use of "goto" is generally discouraged in modern programming due to making code harder to read and maintain, there are rare cases, like breaking out of multiple nested loops or complex error handling before exceptions were widely adopted, where its use might be considered.
How might you address concerns about function templates impacting compilation times and binary size in a high-performance scientific computing application?
- Use inline functions
- Reduce template instantiation
- Avoid using templates
- Optimize template definitions
Function templates can increase both compilation times and the size of the binary due to multiple instantiations for different data types. One way to address these concerns is by reducing unnecessary template instantiations, making sure only required instantiations are compiled, and using techniques like explicit instantiation.
In C++, _______ functions cannot be virtual.
- inline
- recursive
- overloaded
- default
Inline functions in C++ are expanded at the place where they are called, rather than being invoked. Because of this, they cannot be virtual as the virtual mechanism relies on function calls.
In what scenario might a program have a memory leak due to an exception being thrown?
- When the destructor isn't called
- When using global variables
- When an exception is caught and rethrown
- When exception specifications are used
A common memory leak scenario is when an exception is thrown before the memory allocated dynamically (usually with new) is deallocated. If the exception prevents a destructor or a delete statement from being reached, the memory is never released, resulting in a memory leak.
What is the primary purpose of using enum in C++?
- For iterating over collections
- For handling exceptions
- To create user-defined data types
- For memory management
The primary purpose of enum (short for "enumeration") in C++ is to create user-defined data types where you can define a set of named integer constants. It provides a way to assign names to integral constants, which makes a program easier to read and maintain.