The shared_ptr in C++ uses _______ counting to manage the memory of shared objects.
- reference
- object
- allocation
- array
shared_ptr uses reference counting, which means it keeps a count of the number of shared_ptr objects referring to the same memory location. When this count drops to zero (meaning no shared pointers are referring to the object), the memory is automatically deallocated.
What is the relationship between abstraction and interfaces in C++?
- Interfaces are concrete implementations of abstraction.
- Abstraction is achieved using only interfaces.
- Interfaces ensure abstraction but not vice versa.
- C++ does not have interfaces.
In C++, there isn't a direct concept of "interfaces" as in some other languages. However, abstract classes with only pure virtual functions can act similarly to interfaces. These ensure abstraction by forcing derived classes to provide concrete implementations, but not all abstractions need to be interfaces.
The process of transferring the program control from where the exception was thrown to the catch block is known as _______.
- Throwing
- Handling
- Propagation
- Initialization
Propagation refers to the process where after an exception is thrown, it travels up the call stack until it's caught by an appropriate catch block or, if uncaught, the program terminates.
To get the quotient of a division in C++, we use the _______ operator.
- +
- /
- %
- &&
In C++, the forward slash (/) operator is used for division. When used between two integers, it gives the quotient of the division.
Which of the following is true about the complexity of the std::list::remove function?
- O(1)
- O(log n)
- O(n)
- O(n log n)
The std::list::remove function has a linear time complexity O(n). This is because it traverses the list once, removing all elements that match a specified value. As it potentially examines all elements, the complexity is linear.
What is the initial statement in a for loop typically used for?
- Incrementing
- Condition check
- Initialization
- Loop termination
In a for loop, the initial statement is typically used for initialization purposes. This is where variables, often loop counters, are initialized before the loop begins its execution.
What is the main purpose of using friend functions in C++?
- To increase the execution speed of programs
- To enhance object-oriented principles
- To achieve data encapsulation and abstraction
- To provide access to class's private and protected members from outside the class
While C++ uses classes and objects to encapsulate data and behavior, there are cases where you might want an external function (not belonging to the class) to access the private or protected members of the class. This is where friend functions come into play. They provide an exception to the usual encapsulation rules.
The diamond problem occurs due to _______ and can be resolved using _______.
- inheritance, interfaces
- encapsulation, templates
- multiple inheritance, virtual inheritance
- overloading, namespaces
The diamond problem is a complication that arises from multiple inheritance when two classes B and C inherit from A, and class D inherits from both B and C. If there is a method in A that B and C have overridden, and D does not override it, then which version of the method does D inherit: that of B, or that of C? This ambiguity can be resolved in C++ using virtual inheritance, ensuring that only one instance of the base class exists in the derived class.
In C++, a size of long double is at least _______ bytes.
- 8
- 10
- 12
- 16
In C++, the size of long double is compiler-dependent. However, according to the C++ standard, it is at least 8 bytes or the size of a double, whichever is greater. Some compilers may allocate more.
Using templates excessively can lead to an issue known as _______.
- recursion
- template bloat
- overloading
- specialization
Excessive use of templates can result in code that has much larger compiled sizes than equivalent non-template code. This phenomenon, where the binary gets bloated due to templates, is known as template bloat.