Which of the following is a correct statement about friend functions in C++? 

  • They are member functions of a class 
  • They can be inherited by derived classes 
  • They always improve performance of the program 
  • They do not have access to the 'this' pointer of the class
In C++, the 'this' pointer is an implicit pointer to the object on which a member function is called. Since friend functions are not member functions of a class, they do not have access to this pointer. They operate as external functions with special access permissions.

In a C++ application, you have a stream of data being logged... 

  • Write to a temporary file first 
  • Use std::ofstream::flush after every write 
  • Use file locking mechanisms 
  • Implement journaling or write-ahead logging
Implementing journaling or write-ahead logging means changes are first logged to a separate, sequential log. In case of failures, this log can be used to bring the file back to a consistent state. This provides a safeguard against data corruption or loss during unforeseen events.

You are designing a complex numerical algorithm with multiple cooperating classes that need to share internal data for efficiency. How might the friend keyword be used effectively in this context? 

  • Use the friend keyword for all member functions. 
  • Limit the use of friend to only those classes/functions that strictly require access. 
  • Use the friend keyword with external utility functions only. 
  • Declare all classes as friends of each other.
The friend keyword should be used judiciously. By limiting its use only to those classes or functions that genuinely need access, you maintain a degree of encapsulation and make the design intent clear, ensuring that only necessary components have deeper access.

A function without any parameters is declared with the keyword _______ in the parentheses. 

  • auto 
  • void 
  • int 
  • default
When declaring a function with no parameters in C++, the keyword "void" is used within the parentheses to explicitly state that no arguments are expected.

Considering cache efficiency, which STL container will provide the fastest element access and iteration in most cases? 

  • std::list 
  • std::deque 
  • std::vector 
  • std::set
std::vector is a dynamic array implementation. Due to its contiguous memory storage, it provides the best cache locality among the listed STL containers. This leads to faster element access and iteration, especially when compared to non-contiguous storage containers like std::list or node-based containers like std::set.

What feature of C++ templates allows you to provide a specific implementation for particular data types when certain types require a different implementation for optimal performance in a class template? 

  • Template specialization 
  • Template instantiation 
  • Function overloading 
  • Class inheritance
Template specialization is a feature in C++ that allows developers to define a different implementation of a template for a specific data type. This is especially useful when certain data types require unique handling or optimization compared to the general template definition.

When an array is passed to a function, it is always passed by _______. 

  • value 
  • reference 
  • name 
  • address
When an array is passed to a function, it is passed by name, which essentially means the address of the array is passed. This allows functions to modify the original array. The behavior is similar to passing by reference.

A class that contains at least one pure virtual function is called a(n) _______. 

  • subclass 
  • instantiated class 
  • abstract class 
  • inherited class
An abstract class in C++ is a class that contains at least one pure virtual function. This means the class cannot be instantiated and is meant to be inherited by other classes that provide implementations for the pure virtual functions.

What potential issue might arise when using switch-case statements with enumerated types? 

  • Memory leaks. 
  • Switching to an incorrect case accidentally. 
  • Skipping the default case. 
  • Not handling newly added enumerators in the switch.
When using switch-case statements with enumerated types, if a new enumerator is added to the enumeration and not correspondingly handled in the switch statement, it can lead to potential issues or bugs. This is because the switch might not have a case for the new enumerator, and if there's no default case or if the default case doesn't handle it appropriately, the program can behave unpredictably.

If an exception is thrown and not caught anywhere in the program, function _______ is called. 

  • exit() 
  • main() 
  • terminate() 
  • throw()
If an exception is thrown but not caught anywhere in the program, the special function terminate() is called in C++. By default, terminate() ends the program. However, a programmer can change the function that gets called by using set_terminate().