The maximum value that can be stored in an unsigned char is _______.
- 127
- 1024
- 256
- 255
The unsigned char data type in C++ uses 8 bits (1 byte) for storage. This means it can store values ranging from 0 to 2^8-1, which is 0 to 255.
A class whose objects cannot be created is known as a _______ class.
- abstract
- virtual
- static
- inline
An abstract class in C++ is a class that cannot be instantiated, meaning objects of the class cannot be created directly. Abstract classes are intended to be inherited by other classes.
Which of the following is not a legitimate reason to use function templates?
- To support multiple types with the same function logic
- To increase execution speed
- To reduce code size
- To create a list of unrelated types
Function templates are utilized primarily to enable generic programming, meaning writing code that works for multiple data types without repetition. While they may have indirect effects on execution speed or code size, using them to group unrelated types doesn't align with their primary purpose.
Which of the following data types has the smallest size in C++?
- int
- float
- char
- double
In C++, the char data type typically takes up 1 byte of memory, which is smaller than int, float, or double. The exact size of int can vary depending on the platform, but char is defined by the standard to always be at least 1 byte, making it the smallest of the given options.
What does the new operator do in C++?
- Deletes memory
- Compares memory
- Allocates memory
- Modifies memory
The new operator in C++ is used to allocate memory on the heap at runtime. When you use new, it returns a pointer to the beginning of the block of memory it allocated. This is essential for dynamic memory management, as it allows more flexible storage allocation during program execution.
Which of the following scenarios is the most suitable for applying recursion?
- Iterating through elements in an array.
- Calculating the total sales of a company.
- Solving problems that have repetitive structures.
- Creating graphical user interfaces.
Recursion is particularly suitable for problems that exhibit repetitive or nested structures. Examples include problems like calculating factorial, traversing trees, or solving the Towers of Hanoi. Such problems can be broken down into smaller instances of the same problem, making them perfect candidates for a recursive approach.
What is the impact of a function having a default argument?
- It allows function overloading.
- It allows multiple return types for the function.
- It can be called with fewer arguments than defined.
- It enhances runtime performance.
When a function has a default argument, it can be called with fewer arguments than it defines. The compiler will use the default value for any arguments not provided by the caller. This feature aids in providing flexibility while calling functions but doesn't directly enhance runtime performance.
Imagine you are developing an e-commerce system where different types of users (Admin, Buyer, and Seller) exist. Which inheritance model might be beneficial to maintain, expand, and utilize polymorphic behavior?
- Single Inheritance
- Multiple Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
Hierarchical inheritance is where a single class serves as a base class (superclass) for more than one derived class (subclass). In this scenario, the common attributes and methods of users can be part of the base class, while unique functionalities for Admin, Buyer, and Seller can be defined in their respective derived classes.
When is a copy constructor called in a C++ program?
- When an object is returned by value from a function.
- When we assign values from one object to another without the use of pointers.
- When an array of objects is created.
- When a function is called by value.
A copy constructor in C++ is called in several scenarios: 1) when an object is passed (by value) to a function, 2) when an object is returned (by value) from a function, 3) when an object is constructed based on another object of the same class, and 4) during the initialization of an object from another.
The enum class introduces _______ scope to prevent enumerators from polluting the namespace.
- global
- restricted
- local
- strong
The enum class introduces "strong" scoping. This ensures that the enumerator names are not directly accessible without the name of the enumeration, thus preventing name clashes and polluting the global namespace.