What is the purpose of the throw keyword in exception handling in C++?
- to propagate
- to hide
- to prevent
- to execute
In C++ exception handling, the throw keyword is used to signal the occurrence of an exception. When an error condition arises, using the throw keyword can help propagate the exception up the call stack until it's caught by an appropriate catch block.
How does the continue statement affect the execution of a nested loop structure?
- It jumps to the next iteration of the innermost loop.
- It jumps to the next iteration of the outermost loop.
- It stops the entire program execution.
- It restarts all loops from the beginning.
The continue statement causes the current iteration of the loop to terminate, and the control jumps to the next iteration of the innermost loop that contains the continue. It won't affect any outer loops in the nested structure unless it's placed in the outer loop itself.
Which type of class inheritance allows a derived class to inherit attributes and methods from more than one base class?
- Single
- Multilevel
- Multiple
- Hierarchical
Multiple inheritance in C++ allows a derived class to inherit attributes and methods from more than one base class. This contrasts with single inheritance where a class can inherit only from one superclass. C++ supports multiple inheritance, unlike some other OO languages.
The function _______ allows you to swap the elements of two containers in C++ STL.
- push_back
- begin
- swap
- emplace_back
The swap function in C++ STL allows for a quick swap of elements between two containers. This operation is usually done in constant time as only the internal pointers are exchanged.
In C++ STL, the function _______ is used to remove consecutive duplicate elements in a range.
- remove
- unique
- delete
- erase
The unique algorithm in C++ STL is used to eliminate consecutive duplicate elements from a range. The range after the unique elements is left with unspecified values.
The keyword _______ is used to grant a function or class access to the private and protected members of another class in C++.
- using
- friend
- grant
- access
The keyword "friend" in C++ is used to specify that a particular external function or another class can access the private and protected members of the class in which it is declared. This is fundamental to the concept of friend functions and friend classes.
You are working on a large-scale simulation software where numerous animal species are modeled. Which type of inheritance might be most suitable to model individual animal species without encountering the diamond problem?
- Single Inheritance
- Multiple Inheritance
- Hierarchical Inheritance
- Multilevel Inheritance
Single inheritance ensures that a class inherits from only one other class, thus avoiding the diamond problem. The diamond problem arises in languages that support multiple inheritance and can result in ambiguity in the inheritance hierarchy. Single inheritance ensures a clear, linear progression without any ambiguities.
What is the primary reason for using smart pointers over raw pointers in modern C++?
- To increase execution speed
- To use less memory
- To look modern
- To manage memory automatically
Smart pointers, such as unique_ptr, shared_ptr, and weak_ptr, were introduced in C++ to provide automated memory management. They automatically handle the destruction of objects they point to, thereby preventing common issues like memory leaks and dangling pointers.
What is the role of the return statement in a lambda function?
- To indicate the end of the lambda function.
- To return a value from the lambda to the calling function.
- It determines the type of the lambda.
- It can't be used in lambda functions.
Lambda functions, like regular functions, can return values. If the lambda's body contains more than one statement, a return statement can be used to specify which value is returned. If the lambda's body consists of a single return statement, the return type is automatically inferred by the compiler.
Imagine you're refactoring a legacy C++ codebase. It heavily uses friend functions, leading to a maintenance burden and difficult-to-follow code. What strategy might you adopt to improve encapsulation and maintainability without sacrificing performance?
- Use inheritance exclusively.
- Encapsulate the required data and use getter/setter functions.
- Make all data public to avoid using friend functions.
- Refactor to use forward declarations.
While getter and setter methods might introduce a slight overhead, modern compilers can inline these functions to ensure minimal performance impact. By encapsulating data, you enhance the maintainability and structure of the code.