What is the primary benefit of using class templates in C++?
- Faster execution
- Reduced memory usage
- Code optimization
- Genericity
Class templates in C++ provide the ability to create classes that can work with a variety of data types without the need to redefine the class for each type, offering a level of genericity and flexibility in code design.
The minimum possible value of a variable of type short int is _______.
- -32768
- 0
- -1024
- -2147483648
The short int type in C++ is typically 16 bits in size. Given that it's signed, half of the 2^16 possible values are negative. The range thus typically starts at -32768 and goes up to 32767.
How does the virtual base class resolve the diamond problem in C++?
- By preventing multiple copies
- By duplicating base class
- By altering access specifiers
- By overloading functions
The diamond problem arises in C++ multiple inheritance when one class inherits from two classes that have a common base class. This can lead to ambiguity regarding which base class member should be accessed. The "virtual" keyword before the base class prevents the compiler from creating multiple instances of the base class, thus resolving the diamond problem.
What implications does the "Rule of Three" have in C++ class design?
- Always implement three constructors
- Always use three public members in a class
- Always create three instances of a class
- Manage the copy constructor, assignment operator, and destructor
The "Rule of Three" in C++ suggests that if a class requires a user-defined destructor, copy constructor, or assignment operator, it likely requires all three. This rule arises due to the intricacies of dynamic resource management (like memory allocation) in C++. Implementing or omitting one without the others can lead to unexpected behavior.
You are tasked with designing a lightweight 3D point class to be used in a performance-critical graphics application. The class should store three float values and should allow public access to these without using getters and setters. Which user-defined data type would be best suited for this purpose?
- Class
- Struct
- Union
- Array
In C++, structs are typically used for lightweight user-defined data types. They default to public member access, making it easier to access the members directly without the need for getters and setters. Classes default to private access and are generally used when encapsulation is a primary concern.
Which of the following scenarios is most suitable for using a struct instead of a class?
- When needing multiple inheritance.
- When encapsulation and private members are vital.
- When only simple data storage without methods is required.
- When polymorphism is a primary concern.
In C++, both structs and classes can have member functions, inherit from others, and serve many of the same roles. The primary difference is the default access level (structs default to public; classes default to private). However, structs are conventionally used for simple data storage without associated behavior or methods.
To define an abstract class in C++, at least one member function must be declared as _______.
- inline
- pure virtual
- private
- constant
In C++, an abstract class is defined by having at least one pure virtual function. This means the function is declared but not defined within the class, ensuring that derived classes must implement it.
In which type of inheritance do all derived classes share a common base class?
- Multiple Inheritance
- Hierarchical Inheritance
- Multilevel Inheritance
- Hybrid Inheritance
Hierarchical Inheritance is when multiple derived classes inherit from a single base class. All the derived classes share the features of that one common base class. This kind of inheritance structure resembles a hierarchy or tree structure.
The concept of encapsulation involves bundling the data () and the methods () that operate on the data into a single unit.
- objects, functions
- variables, operators
- members, methods
- attributes, functions
Encapsulation is a key concept in object-oriented programming where the data (members) and the functions (methods) that operate on this data are bundled together as a single unit known as a class.
The keyword _______ is used to access the base class’s members in the derived class.
- super
- this
- parent
- base
In C++, the "base" keyword is not actually used. The correct way to access base class members is through the use of the base class name or the scope resolution operator. However, "base" is a term from C#.
To remove all instances of a particular value from a C++ STL vector, you should use the _______ algorithm.
- find
- sort
- erase
- remove
While erase can remove elements from a vector, to efficiently remove all instances of a value, you'd first use remove to move those elements to the end and then use erase. This is known as the "erase-remove" idiom in C++.
Which of the following statements about the struct and class keywords is true?
- Only structs can have member functions
- Structs are always public, and classes are always private
- Both can have member functions, but default access is different
- Classes support inheritance, structs don't
Both struct and class in C++ can have member functions, data members, and can support inheritance. The primary difference is the default access level: members of a struct are public by default, whereas members of a class are private. Other functionalities between them are essentially identical.