An exception thrown by a function in the _______ block can be caught in the subsequent catch block.
- try
- throw
- return
- main
In the 'try' block, we place the code that might throw an exception. Any exception that occurs inside the 'try' block can be caught by the subsequent 'catch' block, allowing for appropriate error handling.
The return statement can be omitted in a function with a return type other than void if _______.
- it's a template function
- it throws an exception
- it's a friend function
- it's an inline function
In C++, if a function with a return type other than void does not have a return statement, but throws an exception that exits the function, it's acceptable and will not result in a compilation error.
In C++, _______ is used to restrict access to class members and maintain encapsulation.
- public
- protected
- private
- global
The "private" access specifier in C++ restricts access to class members, ensuring they can't be accessed or modified outside the class, thereby maintaining encapsulation and data integrity within the class.
You are debugging a C++ application and find that a goto statement is causing erratic jumps and consequently, unexpected behavior. Which refactoring approach might be the most beneficial to enhance code readability and maintainability?
- Replace goto with structured loops.
- Encapsulate the goto in a function.
- Add comments explaining the goto.
- Ignore it and move on.
Using goto statements can lead to spaghetti code that's hard to maintain and debug. It's considered a best practice to avoid using goto in C++. Replacing goto with structured loops or conditionals can often make the code clearer and more maintainable. While encapsulation is a good practice in general, it doesn't address the fundamental problems associated with goto. Comments can help but don't solve the root issue.
In a real-time graphics application where performance is critical, you need to manage a dynamically-allocated array that can change size. Which of the following STL containers would be the most appropriate choice considering the balance between dynamic size management and performance?
- vector
- list
- deque
- forward_list
The STL vector is a dynamic array that provides O(1) access time for its elements. It's efficient when resizing (amortized constant time for insertions/deletions at the end), making it suitable for scenarios requiring a balance between dynamic size management and performance.
What is the impact on performance when using float versus double in mathematical calculations in C++?
- float is faster
- double is faster
- No difference
- It depends on context
While "float" may use less memory, "double" provides higher precision. The performance impact depends on the architecture and context. Some architectures are optimized for double precision, making operations as fast, if not faster, than float operations.
How does a member function differ from a regular function in C++?
- It can only be called by an object.
- It does not have a return type.
- It always has parameters.
- It cannot be defined inside a class.
A member function in C++ is associated with an object of the class and has access to the object's data members. Unlike regular functions, member functions can be called using the object of the class and can manipulate the class's attributes directly. Regular functions don't have this implicit access.
In C++, function overloading is resolved at _______.
- runtime
- declaration time
- compile-time
- execution-time
Function overloading allows multiple functions in the same scope to have the same name as long as they have different parameter lists. In C++, the appropriate overloaded function is chosen at compile-time based on the function call's arguments.
Which keyword is used in C++ to make a function virtual?
- static
- const
- virtual
- override
The keyword virtual is used in C++ to make a function virtual. When a function is declared as virtual, it can be overridden in any derived class. This allows for dynamic polymorphism, where the decision on which method version to execute is made at runtime based on the object's actual type.
Which of the following statements about the if-else control structure is true?
- It can only test for numeric conditions.
- It executes the else block if the if condition is true.
- It allows for multiple conditions to be checked in sequence.
- It requires both if and else parts.
The if-else control structure in C++ provides a way to perform decision-making operations. If the condition inside the 'if' block evaluates to true, the 'if' block of code is executed; otherwise, the 'else' block is executed, if present. It can be used to check multiple conditions in sequence by nesting or using "else if".