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.

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.

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.

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.

Which operator is used to compare if two values are equal in C++? 

  • == 
  • === 
  • !=
In C++, the == operator is used for equality comparison. It checks if the values of two operands are equal. On the other hand, = is an assignment operator, === is not valid in C++, and != is the not-equal operator.

What is the primary purpose of the ofstream class in file handling? 

  • Reading files 
  • Writing to files 
  • Deleting files 
  • Copying files
The ofstream class stands for output file stream. It is primarily used for performing output operations on files, meaning it's used for writing data to files. This class provides methods and properties needed to write data.

Which of the following data types is suitable for storing a character in C++? 

  • boolean
  • char 
  • float 
  • int 
In C++, the char data type is specifically designed to store characters. It typically occupies 1 byte in memory and can represent a single character from the ASCII table.

To prevent a class from being inherited, we declare it as _______. 

  • final 
  • static 
  • private 
  • mutable
In C++, the "final" keyword is used to prevent a class from being inherited. This means that no other class can be derived from a class declared as "final".

When class members are labeled as private, they are inaccessible outside the class, enforcing _______. 

  • encapsulation 
  • abstraction 
  • polymorphism 
  • inheritance
Encapsulation is a key concept in object-oriented programming where the internal details of a class are hidden and only specific functionalities are exposed. Making class members private enforces this encapsulation.

Regarding memory alignment and data packing, which of the following is true for structs in C++? 

  • Struct members are always packed tightly with no padding. 
  • Structs cannot be aligned in memory. 
  • Struct members have a defined order, but might have padding. 
  • Structs use dynamic memory for data storage.
Memory alignment and data packing are important considerations in C++ for optimizing memory usage and performance. In a struct, the order of declaration of members matters because the compiler might introduce padding between members to align data appropriately for the target architecture. This can affect the overall size of the struct.