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.
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.
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.
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.
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.
Maria is writing a program to print numbers from 1 to 10. Which loop would be the most straightforward for this purpose?
- do-while loop
- for loop
- switch loop
- while loop
Maria should use a for loop for this task. A for loop is designed for iterating over a range of values, making it the most straightforward choice for printing numbers from 1 to 10. It allows precise control over the iteration.
Robert observed a strange behavior in his program where the default value of a function argument inside a member function of a class wasn't recognized. Where might he have defined the default value to cause this issue?
- In a Separate Header File
- In a Separate Source File
- Inside the Class Declaration
- Inside the Function Definition
Robert might have defined the default value of the function argument inside the class declaration. In C++, default values for function arguments should be specified in the function declaration, not in the class declaration. Placing them in the class declaration would cause issues with recognition.
How does a break statement inside a nested loop behave?
- It causes a runtime error.
- It exits all loops in the current iteration.
- It exits only the innermost loop.
- It terminates the entire program.
A break statement inside a nested loop will exit only the innermost loop where it's encountered. This behavior allows you to break out of the current loop level without affecting outer loops. If needed, you can label loops to specify which loop to break out of explicitly.
Lisa is defining two functions with the same name and parameters but a different return type. She believes she is using function overloading. Is she correct?
- No, she is not using function overloading.
- She is defining a template function.
- She is using function overriding.
- Yes, she is using function overloading.
Lisa is not using function overloading. Function overloading is based on different parameter types or a different number of parameters. Having the same name and parameters with only different return types is not allowed in C++, as it would lead to ambiguity.
What is the default value of an uninitialized integer variable in C++?
- -1
- 0
- 1
- Undefined
In C++, the default value of an uninitialized integer variable is undefined. It means if you declare an integer variable and do not assign a value to it, then its value is anything; it can be any garbage value. Relying on or using this value leads to unpredictable results and is considered a bad programming practice.