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.
The result of the expression (true || _______) in C++ will always be true.
- FALSE
- TRUE
- 0
- 1
The logical OR (`
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.
You are designing a graphics system that involves various shapes like Circle, Rectangle, and Triangle. How might you design the class structure considering reusability and organized hierarchy?
- Encapsulation
- Polymorphism
- Inheritance
- Composition
Inheritance would be the most suitable approach in this case. A base class named "Shape" can have common attributes like coordinates and colors. Derived classes like "Circle", "Rectangle", and "Triangle" can then inherit from this base class and add their unique methods and properties related to their specific shapes.
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.
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.