What is the impact on performance when using float versus double in mathematical calculations in C++?
- float is faster
- double is faster
- No difference
- It depends on context
While "float" may use less memory, "double" provides higher precision. The performance impact depends on the architecture and context. Some architectures are optimized for double precision, making operations as fast, if not faster, than float operations.
How does a member function differ from a regular function in C++?
- It can only be called by an object.
- It does not have a return type.
- It always has parameters.
- It cannot be defined inside a class.
A member function in C++ is associated with an object of the class and has access to the object's data members. Unlike regular functions, member functions can be called using the object of the class and can manipulate the class's attributes directly. Regular functions don't have this implicit access.
In C++, function overloading is resolved at _______.
- runtime
- declaration time
- compile-time
- execution-time
Function overloading allows multiple functions in the same scope to have the same name as long as they have different parameter lists. In C++, the appropriate overloaded function is chosen at compile-time based on the function call's arguments.
Which keyword is used in C++ to make a function virtual?
- static
- const
- virtual
- override
The keyword virtual is used in C++ to make a function virtual. When a function is declared as virtual, it can be overridden in any derived class. This allows for dynamic polymorphism, where the decision on which method version to execute is made at runtime based on the object's actual type.
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.
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.
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.