Consider a function declared with default arguments. If we provide a lesser number of arguments than expected when calling the function, how are the provided arguments matched?
- Arguments are matched based on data types.
- Arguments are matched from left to right.
- Arguments are matched from right to left.
- Compiler throws an error.
When calling a function with default arguments, the provided arguments are matched from left to right. If you provide fewer arguments, the remaining ones are matched with their corresponding default values, if any.
Steve needs to ensure that a variable can only take on one of three values: RED, GREEN, or BLUE. What should he use in C++ to achieve this?
- Array
- Enum
- Struct
- Union
Steve should use an enum (enumeration) in C++ to achieve this. An enum is a user-defined data type that consists of a set of named integer constants. In this case, he can create an enum with values RED, GREEN, and BLUE, ensuring that the variable can only have one of these predefined values.
Why is function overloading not possible by changing the return type alone in C++?
- It causes runtime errors
- It creates ambiguity
- It leads to syntax errors
- It's allowed in C++
Function overloading in C++ is based on the number and types of function parameters. Changing only the return type doesn't provide enough information to distinguish between overloaded functions. This would lead to ambiguity, making it impossible to determine which function to call.
Why is the use of the "using namespace std;" directive common in C++ programs?
- It allows access to standard C++ libraries
- It eliminates the need for including header files
- It improves program performance
- It reduces code complexity
"using namespace std;" is used in C++ programs to simplify the use of standard C++ libraries. It eliminates the need for prefixing standard library elements with "std::" and thus reduces code verbosity and complexity. It doesn't affect program performance or eliminate the need for including header files.
Lisa has a function that calculates the area of a rectangle. She wants to modify the function such that if only one argument is provided, it calculates the area of a square. Which C++ feature can help her achieve this?
- Default Arguments
- Function Overloading
- Operator Overloading
- Type Conversion
Lisa can use Default Arguments in C++ to modify her function. By assigning a default value to one of the function's parameters, she can make it so that if only one argument is provided, it calculates the area of a square by using the default value as the second argument.
Emily is writing a function to calculate the factorial of a number using a loop. Which loop structure would be the most appropriate for this task?
- Do-While Loop
- For Loop
- Switch Statement
- While Loop
Emily should use a "For Loop" to calculate the factorial of a number. A "For Loop" is well-suited for tasks that involve iterating a specific number of times, such as calculating the factorial of a number, where she can easily control the loop's start, end, and iteration conditions. She can iterate through the numbers from 1 to the given number, multiplying them together to calculate the factorial.
Anna wants to write a program that checks if a number is positive, negative, or zero. Which conditional structure would be most suited for this task?
- For Loop
- If-Else Statement
- Switch Statement
- While Loop
Anna should use the "If-Else" statement for this task. It allows her to evaluate a condition (in this case, whether the number is positive, negative, or zero) and execute different code blocks based on the result.
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.