John is creating a game and needs a variable to store whether a player is alive or dead. Which data type is most appropriate for this purpose?
- bool
- char
- decimal
- double
John should use the 'bool' (boolean) data type. Booleans are binary, representing either 'true' or 'false'. They are perfect for storing states like 'alive' or 'dead' in a game, allowing for simple condition checks.
In C++, passing large structures or classes to functions is typically done by _______ to avoid expensive copy operations.
- pointers
- value
- integers
- operators
Passing large structures or classes by value results in copy operations, which can be expensive in terms of performance. To mitigate this, C++ developers often pass large data structures using pointers or references, allowing the function to access the original data without copying.
The function _______ is used to merge two sorted ranges in the C++ STL.
- combine
- concatenate
- merge
- join
The merge algorithm in C++ STL is used to merge two sorted ranges into a single, sorted range. Both input ranges should be sorted for the merge algorithm to work correctly.
The result of 5.5 + 2.5 in C++ is _______.
- 8
- 7.5
- 8
- 7
In C++, the addition of two floating-point numbers (like 5.5 and 2.5) will result in another floating-point number, which is 8.0 in this case.
Which of the following is not a type of iterator in C++ STL?
- Random access
- Bidirectional
- Dynamic
- Forward
The C++ STL provides several types of iterators like random access, bidirectional, and forward iterators. However, there is no iterator type named "Dynamic." Iterators are powerful tools in STL, allowing operations like traversal, reading, and writing to elements of container objects.
The function signature in C++ includes the function name and the _______.
- return type
- class name
- library
- parameters
A function signature in C++ is determined by its name and its parameter list. The return type is not considered a part of the function's signature.
What is the primary difference between abstraction and encapsulation in C++?
- Abstraction hides details; encapsulation bundles them.
- Abstraction uses inheritance; encapsulation doesn't.
- Abstraction is a data type; encapsulation is a method.
- Encapsulation is a principle; abstraction is an attribute.
The primary difference between abstraction and encapsulation in C++ lies in their purposes and implementations. Abstraction focuses on hiding the complex reality of an object and showing only the necessary features, essentially giving a high-level view. On the other hand, encapsulation is about bundling data and the methods, shielding them from outside interference.
Can a friend function be a member function of another class?
- Always
- Never
- Only if it's a static member function.
- Yes, if it's declared as a friend in the original class.
A friend function can indeed be a member function of another class. In this context, one class allows a member function of another class to access its private and protected members by declaring that member function as a friend.
Imagine you are developing a system where you need to represent a fixed set of constant integral values that represent different states, and you want to restrict the variables to only allow these states. Which C++ feature would be most appropriate to use?
- Class
- Enum
- Union
- Template
Enums, short for enumerations, allow the definition of a type that can hold a set of integral constants. They provide a way to assign names to the integral constants which makes the code more readable and maintainable.
To insert an element into a C++ list at a specified position, you should use the _______ function.
- push
- emplace
- put
- insert
The insert function in C++ STL is used to insert elements at a specific position in a list. emplace is also used for insertion, but it constructs the object in-place, while insert just places a copy or a move of the object.