Lucy wants to declare a function that can add two numbers but doesn't want to specify how it does it. What should she provide?
- Function Argument
- Function Call
- Function Declaration
- Function Definition
Lucy should provide a function declaration. A function declaration defines the function's name, return type, and parameter list without specifying the actual implementation or logic. This allows her to declare the function's existence and signature without writing the code for adding two numbers.
What happens if an inline function is called inside a loop?
- The function is executed for each iteration of the loop
- The function is executed only once
- The loop becomes an infinite loop
- The loop will not compile
When an inline function is called inside a loop, the function is executed for each iteration of the loop. This can potentially lead to code bloat if the function is large, but it can also result in better performance as there is no function call overhead.
Function overloading allows multiple functions to have the same name but with a different ______.
- Access Modifier
- Namespace
- Return Type
- Scope
Function overloading primarily focuses on the function's parameter list, allowing you to have multiple functions with the same name but varying parameter types or numbers. The return type is not considered when distinguishing between overloaded functions.
Fiona read about both inline functions and macros. In what situations might an inline function be a better choice over a macro?
- When code size needs to be minimized, when conditional compilation is required, when multiple statement blocks are needed, when performance is the primary concern
- When memory efficiency is crucial, when cross-platform compatibility is a priority, when macros introduce code duplication, when dynamic polymorphism is needed
- When type safety is essential, when function parameters require validation, when complex operations are involved, when debugging is necessary
- When variable scoping is critical, when the preprocessor directives are needed, when compile-time errors are acceptable, when portability is not a concern
Inline functions are often a better choice over macros in situations where type safety, parameter validation, complex operations, and debugging are essential. Unlike macros, inline functions provide type checking and encapsulation, making them more reliable and maintainable. Macros are generally used for conditional compilation and code size reduction but lack the type safety and debugging capabilities of inline functions.
Which of the following statements correctly starts a C++ program?
- int begin() { }
- int main() { }
- int start() { }
- void main() { }
The correct statement to start a C++ program is "int main() { }." In C++, the main function is defined with the return type "int," and it serves as the entry point for program execution. It can also accept command-line arguments within its parentheses.
In nested loops, how does the break keyword behave when used inside the inner loop?
- It generates a compilation error
- It skips the current iteration of the inner loop
- It terminates both the inner and outer loops
- It terminates only the inner loop
When the break keyword is used inside an inner loop, it terminates only that inner loop and allows the outer loop to continue running. It does not affect the outer loop directly.
What will be the output of a while loop if its condition is initially false?
- The loop will execute indefinitely
- The loop will execute normally
- The loop will execute once
- The loop won't execute
If the condition of a while loop is initially false, the loop won't execute at all. It will skip the loop body and move on to the next code outside the loop.
Jessica is facing issues in her program where virtual functions with default arguments aren't behaving as expected. What should she be careful about while using default arguments with virtual functions?
- Default Argument Scope
- Function Overriding
- Inline Functions
- Virtual Function Resolution
Jessica should be careful about function overriding when using default arguments with virtual functions. Virtual functions are resolved at runtime based on the actual object's type, and default arguments are resolved at compile-time. This can lead to unexpected behavior if the derived class changes the default argument values.
What will be the result of the expression 5 & 3 in C++?
- 0
- 1
- 15
- 8
In C++, the '&' operator is a bitwise AND operator. When you perform a bitwise AND operation between 5 and 3 (binary 101 & 011), you get 001, which is decimal 1.
Which statement is best suited for selecting one of many blocks of code to be executed?
- if-else statement
- for loop
- switch statement
- while loop
The 'switch' statement in C++ is best suited for selecting one of many blocks of code to be executed. It allows you to define multiple code blocks (cases) and select one based on the value of an expression. This is particularly useful when you need to choose from several options.