A function template enables you to write a single function that can handle data of _______ type(s). 

  • single 
  • multiple 
  • dual 
  • fixed
Function templates in C++ allow developers to write a single function definition that can work with data of various types. Instead of writing multiple functions for each type, templates allow for type-generic implementations, meaning one can handle data of multiple types with the same function logic.

In what scenario might a weak_ptr be particularly useful to prevent? 

  • Memory Leaks 
  • Buffer Overflow 
  • Circular References 
  • Stack Overflow
A weak_ptr is a type of smart pointer that holds a non-owning reference to an object that's managed by shared_ptr. It's particularly useful to prevent circular references which can cause memory leaks because objects reference each other preventing them from being deleted.

How does the compiler treat the conditions in a nested if-else structure? 

  • Evaluates all conditions regardless of truth value 
  • Evaluates until the first true condition is found 
  • Skips evaluation of conditions if outside condition is false 
  • Processes only the outermost condition
In a nested if-else structure, the compiler evaluates conditions sequentially. Once a true condition is found, the subsequent "else if" or "else" conditions are not evaluated. If the outermost condition in a nested structure is false, the inner conditions won't be evaluated at all.

How does the C++ compiler handle tail-recursive functions? 

  • Converts them into loops 
  • Generates an error 
  • Ignores the tail recursion 
  • Produces a warning
Many modern C++ compilers can recognize tail-recursive functions and optimize them by converting the recursion into a loop, thus avoiding the overhead of repeated function calls and potentially consuming less stack space. This transformation, commonly known as tail call optimization (TCO), can lead to more efficient runtime behavior, especially for functions that would otherwise involve deep or infinite recursion.

In C++, if a class makes a non-member function its friend, that function gets the ability to access the _______ and _______ members of the class. 

  • public, private 
  • private, protected 
  • public, static 
  • static, protected
In C++, when a non-member function is declared as a friend of a class, it can access both private and protected members of that class. Public members are accessible to all functions, so the unique ability granted by the friendship is access to the private and protected members.

When using the logical AND operator, if the left operand is false, the right operand is _______ evaluated. 

  • always 
  • sometimes 
  • never 
  • conditionally
In C++, the logical AND (&&) operator employs short-circuit evaluation. If the left operand is false, the right operand isn't evaluated because the overall result will already be false.

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.

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.

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.

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.

In binary file operations, to write data of various data types, you use the _______ function. 

  • write 
  • fwrite 
  • push 
  • output
The write function is used in C++ for binary file operations. It allows the user to write data of various data types to a file. Unlike formatted output, write writes raw data bytes to the file.

How does the return statement interact with constructors or destructors in C++? 

  • It can exit a constructor prematurely. 
  • It causes the destructor to be called immediately. 
  • It can be used to return a value from the constructor. 
  • It has no use in constructors and destructors.
Constructors don't return values, so the return statement isn't used to return a value. However, it can be used to exit a constructor prematurely under certain conditions. Destructors also don't return values, and the use of a return statement in them would be to exit early, which is very rare.