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.
Which type of function cannot be virtual in C++?
- Inline functions
- Constructor functions
- Friend functions
- Static member functions
Constructors in C++ cannot be virtual. When creating an instance of a derived class, it's essential to know the exact type of the object being created, so the correct constructor gets called. Making constructors virtual doesn't make sense in the context of object-oriented design, as it would introduce ambiguity in the object creation process.
Which file opening mode in C++ will allow you to append data at the end of the file’s content?
- ios::binary
- ios::in
- ios::app
- ios::trunc
The "ios::app" mode in C++ stands for "append". When a file is opened in this mode, data will be appended to the end of the file's content if it already exists. If the file does not exist, a new one is created.
To check for possible errors or failures in file operations, you should check the _______.
- failbit
- seekg
- open
- tellg
The failbit is a status flag in C++ streams that indicates a file operation failed. When working with file I/O in C++, it's crucial to check this flag (or use functions like fail()) to determine if a particular operation has encountered an error.
How does C++ handle template instantiation when the same instantiation is required in multiple translation units?
- Via separate instantiation
- Via shared instantiation
- Via static instantiation
- Via dynamic instantiation
C++ typically handles template instantiation on a per-translation unit basis. This means each translation unit will have its own instance of the template. Linkers are smart enough to discard duplicate instances, ensuring that only one instance remains in the final executable.
Which of the following C++ versions introduced the auto keyword for automatic type deduction?
- C++03
- C++11
- C++14
- C++98
The auto keyword for automatic type deduction was introduced in C++11. It allows the compiler to automatically deduce the type of a variable based on its initializer, enhancing code readability and maintainability.