What is the primary difference between direct and indirect recursion?
- Direct calls itself, while indirect calls another function that calls it back.
- Direct uses less memory than indirect.
- Direct is slower than indirect.
- Direct is always safer than indirect.
Direct recursion occurs when a function calls itself directly, whereas indirect recursion involves a function calling another function that eventually calls the original function. It's a cycle of calling between two or more functions.
In a for loop, what will happen if the condition is omitted?
- It will throw a syntax error.
- The loop will execute once.
- The loop will never execute.
- The loop will execute indefinitely.
In a for loop, if the condition is omitted, it's treated as always true. Therefore, the loop will execute indefinitely unless there's a break statement or some external factor that interrupts its execution.
The conditions in a switch-case statement must be of _______ data type.
- floating
- string
- integer or enum
- boolean
In a switch-case structure in C++, the conditions or cases must be of an integer or enumeration type. Floating-point and boolean values cannot be used as cases.
When a class contains a pointer to memory allocated in class, we should define a _______.
- destructor
- constructor
- overloader
- allocator
When a class contains a pointer that has memory allocated to it dynamically, it's crucial to have a destructor to release that memory when the object is destroyed to prevent memory leaks.
When a continue statement is encountered in a loop, the program control resumes from _______.
- next statement
- loop condition
- beginning of loop
- end of loop
The continue statement, when encountered inside a loop, causes the program to skip the rest of the current iteration and move directly to the loop condition to check if the next iteration should commence. In essence, it "continues" to the next iteration of the loop without finishing the current one.
What is the use of a catch block with an ellipsis (...)?
- To catch integer exceptions.
- To catch all exceptions irrespective of its type.
- To denote omission in code.
- To handle multiple exceptions at once.
A catch block with an ellipsis (...) is used to catch all exceptions, irrespective of their type. This can be useful when you want to ensure that no exceptions go unhandled, but it's often a good practice to handle specific exceptions with their respective catch blocks.
The function _______ is used to close a file.
- fstream
- read
- close
- write
The close function is used in conjunction with file streams in C++ to close an open file. It's crucial to close files after operations to release system resources and ensure that all changes, if any, are flushed and saved appropriately to the file system.
What is a destructor used for in C++?
- To allocate memory.
- To initialize objects.
- To handle exceptions.
- To release resources.
A destructor is a special member function of a class that is executed whenever an object of that class goes out of scope or is explicitly destroyed. Its primary role is to release any resources that the object may have acquired during its lifetime.
What does the term “Diamond Problem” refer to in the context of C++ inheritance?
- Multiple inheritance ambiguity
- Memory leak in inheritance
- Abstract base class issue
- Diamond-shaped class structure
The "Diamond Problem" arises due to multiple inheritance when a particular class is accessible through multiple paths, often causing ambiguity. For instance, if two base classes A and B have the same method and a derived class inherits both A and B, there's ambiguity on which method to call if not overridden in the derived class.
What is the purpose of a pure virtual function in C++?
- To provide default functionality
- To force derived classes to provide an implementation
- To enhance runtime performance
- To prevent method overloading
A pure virtual function in C++ is declared using "= 0" and doesn't have an implementation in the base class. It's a way to ensure that derived classes provide their own implementation of the function, effectively making the base class abstract. This ensures a consistent interface.