In C++, the method used to allocate memory dynamically during runtime within the object is called _______.
- malloc
- new
- alloc
- reserve
In C++, dynamic memory allocation for objects is achieved using the new operator. Unlike malloc in C, new in C++ initializes the allocated memory.
The problem in which a derived class may inherit the same member more than once from a base class hierarchy is known as the _______ problem.
- Replication
- Duplication
- Diamond
- Looping
The "Diamond Problem" arises in object-oriented languages that support multiple inheritance. If a class is derived from two classes that have a common base class, the derived class may inherit the same member more than once.
Which of the following is true about function overloading in C++?
- Functions must be in the same scope
- Return type can be used to differentiate overloaded functions
- Overloaded functions must have a different number of parameters
- Only member functions can be overloaded
Function overloading in C++ allows multiple functions with the same name but different parameters (types and/or number). They must be in the same scope. Return type alone cannot be used to distinguish overloaded functions, and both member and non-member functions can be overloaded.
Which of the following is true regarding stack unwinding in C++ exceptions?
- It is a process of releasing memory.
- It relates to multi-threading.
- It is the process of calling destructor functions.
- It is the process of rolling back a database transaction.
Stack unwinding in C++ refers to the process that takes place when an exception is thrown, and control is transferred from the point of the throw statement to the handler. During this, all the local objects in the intervening frames are properly destroyed.
How can the return statement be used in a function that returns void?
- To exit the function immediately.
- To return a value of type int.
- To pause the function execution.
- To skip to the next function.
In a void function, the return statement doesn't return a value but can be used to exit the function prematurely. For instance, in certain conditions, if there's no need to execute the rest of the function, a return statement can be used to exit out, improving efficiency and logic clarity.
Which of the following data structures is not implemented as a container in C++ STL?
- Array
- Linked List
- Hash Map
- Binary Tree
While the C++ Standard Template Library (STL) provides containers for dynamic arrays (vector), linked lists (list), and hash maps (unordered_map), there's no direct implementation for a binary tree. Trees are represented using set, map, multiset, and multimap, but they are based on balanced binary search trees (like Red-Black Trees) rather than generic binary trees.
A C++ project is exhibiting memory leaks, but it is unclear where they originate. Considering the tools and features available in modern C++, which approach might be most efficient in tracking down and resolving the memory leaks?
- Re-writing the entire project from scratch
- Use new and delete extensively
- Use std::shared_ptr and std::weak_ptr
- Utilize memory profiling tools like Valgrind
Memory profiling tools, like Valgrind, inspect a program's memory usage in real-time and can identify where memory is being allocated but not released. By pinpointing the exact location of leaks, developers can focus their efforts on specific parts of the code, rather than resorting to broad changes or extensive manual checks.
A game has a scoring system where players earn points. The game needs a feature to evenly distribute bonus points among players and also determine any remaining points that can't be evenly distributed. Which arithmetic operator is essential to determine the number of leftover points?
- *
- +
- -
- %
In this scenario, the modulus operator (%) is essential. When dividing the bonus points by the number of players, the modulus operator gives the remainder, which represents the number of leftover points that can't be evenly distributed among the players. This allows the game to allocate bonus points fairly and determine if there are any undistributed points.
What value does a function with return type void return?
- 0
- Null
- It doesn't return a value
- An undefined value
A function with a void return type doesn't return any value. It is often used for functions that perform an action but do not need to send a result back to the caller.
What is the primary purpose of encapsulation in object-oriented programming in C++?
- Code compression
- Memory saving
- Hide complexity
- Code sharing
Encapsulation is a core concept in object-oriented programming. Its primary purpose in C++ and other OOP languages is to hide the internal representation, or state, of an object from the outside and to bundle the data (attributes) and the methods (functions) that operate on the data into a single unit, thus hiding complexity.