A function that calls itself directly or indirectly is known as a _______ function.
- recursive
- iterative
- overloaded
- main
A function that calls itself, either directly or by calling another function that eventually results in the original function being called again, is termed recursive.
In C++, which keyword is used to declare an abstract class?
- virtual
- volatile
- pure
- abstract
An abstract class in C++ is a class that cannot be instantiated and is meant to be inherited by other classes. It's declared using the virtual keyword, especially when a virtual function is set to 0, making it a pure virtual function, thus making the class abstract.
How does the short-circuit evaluation work in logical operators?
- Sequentially
- Left-to-right
- Both operands
- None required
Short-circuit evaluation refers to the evaluation of logical expressions in a left-to-right manner, stopping as soon as the outcome is determined. For instance, in the logical AND operation (&&), if the left operand is false, the right operand won't even be evaluated because the entire expression is already false.
What does the term “Diamond Problem” refer to in the context of C++ inheritance?
- Multiple inheritance ambiguity
- Memory leak in inheritance
- Abstract base class issue
- Diamond-shaped class structure
The "Diamond Problem" arises due to multiple inheritance when a particular class is accessible through multiple paths, often causing ambiguity. For instance, if two base classes A and B have the same method and a derived class inherits both A and B, there's ambiguity on which method to call if not overridden in the derived class.
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.
In a financial application, you are processing transaction data in a loop. When a fraudulent transaction is detected, it needs to be logged and then the processing needs to continue with the next transaction. How would the continue statement be utilized effectively in this scenario?
- To exit the application.
- To restart the loop from the beginning.
- To skip the fraudulent transaction and continue with the next.
- To reprocess the fraudulent transaction.
The continue statement is used to skip the current iteration of a loop and move to the next one. In the given scenario, once a fraudulent transaction is detected and logged, the continue statement can be used to skip any further processing for that transaction and immediately start processing the next transaction in the loop.