What is the use of a catch block with an ellipsis (...)?
- To catch integer exceptions.
- To catch all exceptions irrespective of its type.
- To denote omission in code.
- To handle multiple exceptions at once.
A catch block with an ellipsis (...) is used to catch all exceptions, irrespective of their type. This can be useful when you want to ensure that no exceptions go unhandled, but it's often a good practice to handle specific exceptions with their respective catch blocks.
The function _______ is used to close a file.
- fstream
- read
- close
- write
The close function is used in conjunction with file streams in C++ to close an open file. It's crucial to close files after operations to release system resources and ensure that all changes, if any, are flushed and saved appropriately to the file system.
What is a destructor used for in C++?
- To allocate memory.
- To initialize objects.
- To handle exceptions.
- To release resources.
A destructor is a special member function of a class that is executed whenever an object of that class goes out of scope or is explicitly destroyed. Its primary role is to release any resources that the object may have acquired during its lifetime.
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.
A function that calls itself directly or indirectly is known as a _______ function.
- iterative
- recursive
- inline
- overloaded
A function that calls itself, either directly or indirectly through other functions, is known as a recursive function. Recursion is a powerful programming technique where a problem is broken down into smaller instances of the same problem, typically leading to simpler solutions.
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.
The goto statement can only jump to labels within the same _______.
- function
- file
- scope
- class
The goto statement in C++ can only jump to labels within the same function. Jumping between functions or other scopes would introduce significant complexity and is not allowed.