What is the purpose of a pure virtual function in C++? 

  • To provide default functionality 
  • To force derived classes to provide an implementation 
  • To enhance runtime performance 
  • To prevent method overloading
A pure virtual function in C++ is declared using "= 0" and doesn't have an implementation in the base class. It's a way to ensure that derived classes provide their own implementation of the function, effectively making the base class abstract. This ensures a consistent interface.

Can a friend function of a class access the private members of that class? 

  • Only if the function is defined inside the class 
  • No, it can't 
  • Yes, it can 
  • Only if the class has no protected members
Friend functions are specially designated functions that are allowed to access the private and protected members of a class. This is their primary purpose. Although they can breach the encapsulation principle, they're useful in scenarios where certain external functions require closer integration with a class's internals.

You are developing a financial application where precision is crucial. Which arithmetic operation could potentially introduce errors due to floating-point representation? 

  • Addition 
  • Division 
  • Multiplication 
  • Subtraction
While all floating-point arithmetic can introduce precision errors due to the nature of their representation, division is particularly notorious because it can generate repeating fractional components that might not be accurately represented in binary floating-point. This can lead to small errors that accumulate in financial calculations.

In the context of operator overloading, the expression a + b is equivalent to _______. 

  • a.add(b) 
  • a.operator+(b) 
  • a.plus(b) 
  • a.combine(b)
Operator overloading allows operators to be redefined and used where one or both of the operands are of a user-defined class. In the context of C++, the expression a + b where the + operator is overloaded can be reinterpreted as a.operator+(b). This allows developers to specify the behavior of the + operator for user-defined types.

In a large-scale C++ project, two classes, Logger and FileHandler, are tightly coupled, sharing a lot of private data between them using friendship. How might you refactor these classes to reduce coupling without losing functionality? 

  • Combine both classes into a single class. 
  • Extract shared functionality into a separate utility class. 
  • Create global variables for shared data. 
  • Introduce an intermediary interface.
Introducing an intermediary interface or abstraction can decouple these classes. This interface can define the necessary methods and data that both classes need, allowing them to interact without direct access to each other's private data, leading to better separation of concerns.

While developing a complex algorithm in C++, you notice that multiple nested loops and conditionals are making the logic increasingly hard to follow. How might judicious use of the return statement improve code clarity and reduce nested structures? 

  • By exiting functions early when conditions are met. 
  • By skipping iterations in loops. 
  • By jumping to specific parts of the code. 
  • By creating nested functions.
The "early return" pattern, where you exit a function as soon as you know the result, can simplify code by reducing the need for deeply nested structures. Instead of having multiple nested conditionals, you can check a condition and return early if it's met, leading to more linear and readable code. Using the return statement to skip iterations or jump to specific code parts is not its intended use in C++.

An infinite loop can be intentionally created for program structures like event listeners using for(;;), which is often referred to as a _______ loop. 

  • endless 
  • continuous 
  • forever 
  • looping
The term endless loop is often used to describe loops that have no termination condition, such as for(;;), and they continue executing indefinitely until externally terminated.

The _______ smart pointer in C++ does not allow the pointer to be shared among objects. 

  • unique_ptr 
  • weak_ptr 
  • shared_ptr 
  • custom_ptr
unique_ptr is a smart pointer that owns a dynamically allocated object exclusively. It ensures that only one unique_ptr can point to the object at a time. If another unique_ptr tries to take ownership, the original will relinquish its hold, ensuring uniqueness.

When implementing a finite state machine (FSM) in C++, which control structure might be more advantageous? 

  • Use if-else exclusively 
  • Use switch-case exclusively 
  • Use loops 
  • Depend on external libraries
A switch-case is generally more advantageous for implementing FSM in C++ because it improves readability when dealing with a set of known, discrete values (like states). It's also more performant than a chain of if-else statements in scenarios where there are multiple states.

In C++, using goto to jump over the initialization of a variable will result in _______. 

  • compilation error 
  • uninitialized variable 
  • runtime exception 
  • optimized behavior
Jumping over the initialization of a variable using the goto statement will result in a compilation error because the compiler will identify it as an attempt to bypass variable initialization.