How does the C++ compiler handle different types of exceptions in a function template?
- It uses dynamic casting to determine the type.
- It uses overloading to handle exceptions.
- Each instantiation gets its own set of exceptions.
- The compiler ignores type-specific exceptions.
In C++, when a function template is instantiated, it gets its own version of the function, complete with its own set of exceptions. This means that if different instantiations of the function template throw different exceptions, each instantiation will have its own set of exceptions to catch. This provides a level of type-safety during exception handling.
The _______ function is used to move the file pointer to a specified position in the file.
- tellp
- seekp
- get
- put
The seekp function is used with output file streams to set the position of the next character to be inserted into the file. It essentially moves the file pointer to a specified position, allowing for efficient and targeted writing or modifying of file contents.
Which access specifier allows a class member to be accessible only within its own class and friends?
- public
- protected
- private
- global
The private access specifier in C++ ensures that class members are accessible only within the class they are defined and by friend functions or classes. This helps in the principle of encapsulation, keeping data and methods secure from unintended access.
When might using a table of function pointers be preferable over a switch-case statement for handling various cases/conditions?
- When handling a static set of conditions that seldom change.
- When trying to make the code more object-oriented.
- When handling a very large number of cases that might change dynamically or are loaded from an external source.
- When the conditions are based on string values.
Using a table of function pointers can be highly beneficial when there's a need to handle a dynamic set of conditions, especially if these conditions might be loaded from an external source or change during runtime. It allows for a more flexible and extensible approach than hard-coding numerous cases in a switch-case statement. Furthermore, it can lead to cleaner and more maintainable code in some scenarios.
In a complex software project where multiple classes are interacting with one another, you discover that there is a significant performance bottleneck during the creation and deletion of objects, which is causing inefficiency. What strategy or principle might be applied to manage object creation and deletion more efficiently?
- Use of object pooling.
- Reduce the use of polymorphism.
- Always use inline functions.
- Implement multi-threading.
Object pooling is a design pattern where a set of initialized objects are kept ready to use, rather than allocating and deallocating them on the fly. This can greatly reduce the overhead of object creation and deletion in scenarios where such operations are frequent. Other options, while useful in specific contexts, don't directly address the efficient management of object creation and deletion.
Unlike if-else, switch-case in C++ does not support _______ type for case values.
- floating-point
- integer
- character
- boolean
In C++, switch-case constructs don't support floating-point types for case values. This means you can't use float or double values as cases. On the other hand, if-else structures can easily handle conditions based on floating-point values.
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.