The expression a×b can be represented as a _______ b in C++.
- *
- %
- +
- -
In C++, the asterisk () is used as the multiplication operator. So, "a×b" is represented as "ab" in C++ programming.
Which method will you use to write a single character to the file stream in C++?
- put()
- write()
- insert()
- push()
The put() method is used to write a single character to a file stream in C++. The write() method is typically used to write blocks of binary data, while insert() and push() aren't standard methods for this purpose in C++ streams.
The _______ keyword is used when we want to explicitly pass by reference but prevent the function from modifying the argument.
- const
- static
- virtual
- inline
The const keyword is used in C++ to indicate that a value shouldn't be modified. When passing an argument by reference, using const before the parameter type ensures that the function does not alter the original data, providing a level of safety.
The _______ function is automatically called when an object is destroyed.
- destructor
- finalizer
- deleter
- remover
In C++, when an object's lifetime ends, its destructor is called automatically. A destructor is a member function with the same name as its class preceded by a tilde (~).
In the context of recursion, what is a "recursive case"?
- A case that ends the recursion
- A scenario where recursion isn't used
- A case that repeats the same steps
- A case that calls the function itself recursively
In recursive algorithms, the "recursive case" refers to the scenario or condition where the function calls itself. This is contrasted with the "base case", which provides a termination criterion for the recursion. Without a base case, recursive functions can run indefinitely, leading to a stack overflow. The recursive case helps break down the problem into smaller, more manageable sub-problems.
Which access specifier in C++ allows a class member to be accessible only within the class and friends of the class?
- public
- private
- protected
- external
The private access specifier in C++ ensures that class members are only accessible within the class and not outside. Additionally, friend functions or classes can access these private members, but they remain hidden from other parts of the program.
What is the effect of the continue statement inside a for loop?
- It pauses the loop for a second
- It terminates the loop
- It jumps to the next iteration
- It reruns the current iteration
Inside a for loop, the continue statement causes the loop to skip the rest of its body and jump immediately to the next iteration. This is particularly useful when certain conditions within the loop need to be avoided for specific iterations.
How does an inline function improve the efficiency of a C++ program?
- By reducing memory usage
- By reducing runtime overhead
- By decreasing code size
- By performing lazy evaluation
Inline functions are a suggestion to the compiler to insert the complete body of the function wherever that function is used in the code. This can improve efficiency by eliminating the overhead of a function call, potentially leading to faster runtime performance.
What is the potential risk of passing parameters by reference?
- Performance overhead
- Inability to return values
- Accidental data modification
- Pointers become null
Passing parameters by reference gives a function direct access to the original data. This can be risky as inadvertent changes in the function can lead to unintended modifications to the data. Care must be taken to ensure the integrity of the data and to avoid unexpected side effects. This risk doesn't exist when passing parameters by value since the function works with a copy of the data.
The use of _______ in nested if-else structures can sometimes enhance readability and maintainability of the code.
- curly braces
- semicolons
- return statements
- comments
Using curly braces { } in nested if-else structures ensures clear block demarcation, helping readers understand the scope and flow of the code. Without them, it's easy to misinterpret where conditions start and end, especially in deep nesting.