Which C++ statement is used to make a single conditional decision?
- for loop
- if statement
- switch statement
- while loop
The 'if' statement in C++ is used to make a single conditional decision. It allows you to execute a block of code only if a specified condition is true. This is fundamental for controlling the flow of your program based on conditions.
In nested loops, if a break statement is executed inside the inner loop, the control will exit the ______ loop.
- Inner
- Outer
- Both inner and outer
- None of the above
In nested loops, a break statement is used to exit the innermost loop. When it's executed, control exits the inner loop, not the outer one. Therefore, the correct option is 'Inner.'
A common mistake that leads to infinite loops is forgetting to update the ______ variable.
- Counter
- Loop
- Control
- Iterator
A common mistake that leads to infinite loops is forgetting to update the 'Counter' variable. In loops, the counter variable is crucial for controlling the loop's progress and termination.
A ______ statement can be used as a control structure to repeatedly execute a block of code as long as a particular condition remains true.
- For
- If
- Switch
- While
In programming, a "while" statement is a control structure that creates a loop. It repeatedly executes a block of code as long as a specified condition remains true. This is useful for scenarios where you want to perform an action multiple times until a certain condition is met.
Robert wants to write a function that can either accept two parameters or three. What feature of C++ should he use to achieve this?
- Function Overloading
- Function Pointers
- Template Functions
- Inheritance
Robert should use "Function Overloading" to write a function that can accept either two or three parameters. Function overloading allows him to define multiple functions with the same name in the same scope but with different parameter lists. The compiler will then determine which function to call based on the number or types of arguments passed to it. This flexibility enables Robert to create a single function name with different parameter options.
Sarah has a library of functions, and she wants to share only what the functions do, not how they do it. What should she share with others?
- Function Comments
- Function Declarations
- Function Definitions
- Function Parameters
Sarah should share the Function Declarations with others. Function Declarations provide information about the name, parameters, and return type of a function without revealing the actual implementation details. This allows users of the library to understand how to use the functions without being concerned about their internal workings.
John writes the entire logic of his function within the header file. What part of the function did he write?
- Function Call
- Function Header
- Function Implementation
- Function Prototype
John wrote the function implementation within the header file. The function implementation contains the actual logic and code that defines how the function performs its task. Writing the entire logic in the header file is not a typical practice and may lead to code organization issues.
In C++, which operator is used for member selection in pointers?
- -> (Arrow)
- . (Dot)
- :: (Scope Resolution)
- N/A
The -> (Arrow) operator is used in C++ to access members of an object or struct through a pointer. For instance, if you have a pointer to a struct ptr and want to access its member x, you can do so using ptr->x.
Can we use default arguments for both member methods of a class and standalone functions?
- No, for neither
- Yes, for both
- Yes, for member methods only
- Yes, for standalone functions only
Yes, for both. In C++, you can use default arguments for both member methods of a class and standalone (non-member) functions. Default arguments provide flexibility when calling functions or methods by allowing you to omit some arguments if they have defaults defined.
How does the __forceinline keyword (in some C++ compilers) differ from the inline keyword?
- It disables inlining
- It enforces inlining
- It has no effect
- It suggests inlining
The '__forceinline' keyword is a directive to the compiler that strongly enforces inlining of the function, even if the compiler would not typically inline it based on its heuristics. This keyword provides a more forceful hint to the compiler compared to the 'inline' keyword, ensuring that the function is inlined whenever possible.
What is the correct way to declare a function that takes two integers as parameters and returns their sum?
- int sum(int a, int b) { return a + b; }
- int sum(int a; int b) { return a + b; }
- sum(int a, int b) { return a + b; }
- void sum(int a, int b) { cout << a + b; }
Functions in C++ are declared by specifying their return type, followed by the function name and parameters enclosed in parentheses. The correct way to declare a function that takes two integers and returns their sum is by declaring the return type as int and specifying the parameters with commas in between. So, the correct declaration is int sum(int a, int b).
Alice has a class that includes a function to compute the area. She wants this function to compute the area for both circles (given radius) and rectangles (given length and breadth). Which concept can help her achieve this without renaming the functions?
- Function overloading
- Function overriding
- Operator overloading
- Polymorphism
Alice can achieve this using the concept of polymorphism, specifically method overloading. By overloading the computeArea function with different parameter types (radius for circles and length and breadth for rectangles), she can have a single function name that behaves differently based on the input, without renaming the functions.