When might an inline function increase the overall size of the compiled code?
- When Used Sparingly
- When Used in Critical Path Code
- When Used in Header-Only Libraries
- When Used in Isolated Modules
Inline functions can increase the overall size of the compiled code when used in header-only libraries. Since the function's code is included in each translation unit that includes the header, it can result in code duplication and larger executables.
When declaring a function, which keyword is used to specify that the function should have a default argument?
- #NAME?
- const
- default
- default_argument
In C++, when declaring a function with default arguments, the = operator followed by a default value is used to specify the default argument. For example, int func(int a, int b = 0) declares a function func with a default argument of 0 for the parameter b.
What is the main advantage of function overloading?
- Better memory management
- Faster execution of functions
- Improved code readability and reusability
- Smaller executable file size
The main advantage of function overloading is improved code readability and reusability. It allows you to use the same function name for logically related operations, making your code more intuitive and easier to understand. It also promotes code reusability by reducing the need to create distinct function names for similar tasks.
For a given function, once you start providing default values for arguments from the right, you cannot skip providing default values for subsequent arguments on the ______.
- Left
- Middle
- None of the above
- Right
For a function with default argument values, you can only provide default values for arguments starting from the right and not in the middle or left. Skipping arguments without default values in the middle would lead to a compilation error.
If the function is a member of a class, and default values are provided at function _______, then these default values become part of the class.
- class declaration
- class definition
- function declaration
- function definition
When default values are provided at the function definition within a class, these defaults become part of the class and are associated with that function. They affect how instances of the class behave when using that function.
Which control structure in C++ allows for multiple outcomes based on the value of an expression?
- for loop
- if statement
- switch statement
- while loop
The switch statement in C++ is used to select one choice among many based on the value of a given expression. It allows for multiple outcomes based on different constant integral values.
In a switch statement, if there is no break statement after a case, what will happen?
- The program will continue to execute the code in subsequent cases until a break statement is encountered
- The program will generate a compilation error
- The program will skip the current case and move to the default case
- The program will throw a runtime exception
In C# and many other programming languages, when there is no break statement after a case, program execution will "fall through" to subsequent cases. This means that all code under subsequent case labels will be executed until a break statement is encountered or the switch statement ends. This behavior can be intentional when multiple cases should share the same code block.
Sarah is creating a math library and wants to write a sum function that can add two integers, two floats, or an integer and a float. Which C++ feature should she use?
- Function Overloading
- Function Overriding
- Operator Overloading
- Type Casting
Sarah should use Function Overloading. This allows her to define multiple functions with the same name but different parameter types. In this case, she can create overloaded functions that accept different combinations of integers and floats to achieve the desired behavior for her sum function.
Michael wants to ensure that his overloaded function is chosen correctly during compile time, even in ambiguous cases. What practices should he follow?
- Avoid using the same function name
- Use a different return type
- Use different parameter types
- Use explicit type casting
To ensure that overloaded functions are chosen correctly during compile time, developers should use different parameter types. This helps the compiler distinguish between the various overloaded functions and select the appropriate one based on the provided arguments.
Can recursive functions be declared as inline?
- It depends
- No
- Not always
- Yes
Recursive functions can be declared as inline, but it's important to understand the implications. While technically possible, inlining recursive functions can lead to code bloat and stack overflow errors, as each inlined call consumes stack space. The decision to inline recursive functions should be made judiciously based on the specific use case.
Olivia's program requires monitoring real-time data, and she needs to keep a loop running indefinitely to process incoming data. However, she is concerned about performance overhead. What best practice can she adopt?
- Employ asynchronous programming
- Implement a busy-wait loop
- Use a sleep or delay function
- Use multiple threads
To minimize performance overhead while keeping a loop running indefinitely for real-time data processing, Olivia should adopt the best practice of employing asynchronous programming. Asynchronous programming allows the program to continue processing other tasks while waiting for incoming data, reducing the need for a busy-wait loop or excessive CPU usage. It's a more efficient way to handle real-time data without wasting system resources.
Alice has a configuration setting that can have five possible values. Instead of using a long if-else structure, what would be a more elegant and readable approach to process the configuration setting?
- Using a dictionary or a key-value store
- Using a loop
- Using a nested if-else structure
- Using a switch statement
When dealing with a configuration setting that has multiple possible values, using a dictionary or a key-value store is a more elegant and readable approach. This allows for direct mapping of configuration values to specific actions or settings, improving code maintainability and readability.