How does C++ enforce encapsulation and abstraction in multi-level inheritance scenarios?
- By using abstract classes only.
- By private and protected access specifiers.
- Through use of virtual functions alone.
- By using friend functions.
In C++, encapsulation and abstraction in multi-level inheritance are primarily enforced using private and protected access specifiers. While private members are inaccessible outside the class, protected members can be accessed by derived classes, ensuring a controlled inheritance mechanism.
Which type of loop is best suited when the number of iterations is known ahead of time?
- do-while loop
- while loop
- for loop
- recursive function
A for loop is best suited when the number of iterations is known ahead of time, as it allows for an initial statement (typically for initialization), a condition check, and an increment/decrement operation in a compact syntax.
Consider a scenario where you have a large dataset and you need to frequently erase and insert elements in the middle of the data. Which STL container should be avoided in order to prevent frequent reallocations and data movements?
- vector
- list
- deque
- set
The STL vector maintains its elements in a contiguous block of memory. Inserting or erasing elements in the middle requires shifting elements, which can be expensive, especially for large datasets. This might cause frequent reallocations and data movements, impacting performance.
A friend function is defined outside the class but has the ability to access the _______ members of the class.
- static
- private
- public
- mutable
A friend function, although not a member of a class, can access its private and protected members. This provides an external function the capability to interact closely with the internals of the class.
To read an entire string from a file, instead of a single character, use the _______ function.
- getline
- read
- fetch
- getString
The getline function is used in C++ to read an entire line from an input stream, such as a file. This is particularly useful when dealing with strings that have spaces or when reading full sentences.
How is a friend class defined in C++?
- By inheriting the original class.
- By using the friendship keyword.
- By prefixing the class with the friend keyword.
- By including the class's header file in the original class.
A friend class in C++ is a class that is given permission to access the private and protected members of another class. To declare a class as a friend of another class, the friend keyword is used before the class declaration in the original class.
In which year was the C++98 standard officially published?
- 1990
- 1995
- 1998
- 2002
The C++98 standard was officially published in 1998. It was the first standardized version of the C++ language, providing a foundation for many modern C++ features and establishing a baseline for future improvements.
What happens if you try to delete memory using the delete operator more than once?
- It gets deleted twice
- No effect
- Program crashes
- Memory gets duplicated
Deleting a memory location more than once leads to undefined behavior, most commonly resulting in program crashes. Always ensure memory is deleted once and pointers are set to nullptr afterwards.
The return type of a function that does not return any value is specified as _______.
- char
- int
- double
- void
The "void" return type specifies that a function doesn't return a value. It's used when the primary purpose of the function is to perform an action, not produce data.
The bitwise NOT operator (~) performs a _______ operation on each bit of a number.
- negation
- AND
- OR
- XOR
The bitwise NOT operator (~) inverts each bit of a number. If the bit is 0, it becomes 1, and if it's 1, it becomes 0. This is essentially a negation operation on individual bits.
When dealing with complex conditions, it might be beneficial to use a _______ in conjunction with if-else structures to improve code clarity and maintainability.
- truth table
- destructor
- loop
- constructor
Using a truth table can help in analyzing and simplifying complex conditions. By representing all possible input combinations and their results, a truth table provides a clear view of the logic, helping in both implementation and debugging of if-else structures.
The _______ of a recursive function is a condition that does not use recursion to produce an answer.
- base case
- recursive loop
- iteration step
- end case
The base case of a recursive function provides the termination criteria for the recursion. Without a base case, the recursive calls would go on indefinitely, leading to a stack overflow. The base case typically provides a straightforward solution without the need for further recursion.