Creating user-defined exceptions in C++ can be achieved by inheriting the _______ class.
- std::exception
- std::error
- std::runtime_error
- std::catchable
In C++, user-defined exceptions are often derived from the std::exception class or its sub-classes. This allows them to be caught using catch blocks designed for standard exceptions, while also adding custom behavior.
Consider a large-scale application where multiple threads often access shared objects concurrently. Which smart pointer can be used to ensure that an object is deleted only when the last pointer to it is out of scope, considering thread safety?
- raw pointer
- shared_ptr
- unique_ptr
- weak_ptr
shared_ptr is thread-safe in terms of its reference counting mechanism, ensuring that the object is safely managed across multiple threads and deleted only when the last pointer to it goes out of scope.
Which of the following stream classes is suitable for both reading and writing operations?
- ifstream
- ofstream
- fstream
- sstream
The "fstream" class in C++ is suitable for both input and output operations. This means you can use it for both reading from and writing to files. In contrast, "ifstream" is specifically for input (reading) and "ofstream" is for output (writing).
Which function is used to get the position of the file pointer in a file?
- getpos()
- seekg()
- tellg()
- seekp()
The "tellg()" function in C++ is used to get the current position of the file pointer in a file when working with input operations. It returns the current position of the reading cursor within the file, which is useful for various file handling tasks.
In C++, if a programmer forgets to deallocate memory using delete, it can result in a _______.
- segmentation fault
- memory leak
- compilation error
- type error
A memory leak in C++ occurs when programmers allocate memory (typically using the new operator) but forget to deallocate it (using the delete operator). This causes the program to consume memory resources continuously, which might lead to system slowdowns or crashes.
Which of the following is a correct file extension for a C++ source file?
- .cpp
- .html
- .java
- .py
C++ source files typically have the extension ".cpp", although other extensions like ".cc" or ".cxx" are also used sometimes.
What could be a reason for choosing pass by pointer over pass by reference in a function?
- Allow null arguments
- To increase execution speed
- To reduce code complexity
- For automatic memory management
Pass by pointer allows for the possibility of passing a null argument. In contrast, references in C++ must always alias some object, meaning you can't have a null reference. When a function needs to accept a "no-object" situation, using a pointer can be preferable since it can be checked for nullity.
A _______ is a smart pointer that owns and manages another object through a pointer and disposes of that object when the _______ goes out of scope.
- auto_ptr
- unique_ptr
- shared_ptr
- raw_ptr
A unique_ptr in C++ is a type of smart pointer that owns a dynamically allocated object exclusively. It ensures that the owned object is properly deallocated once the unique_ptr that owns it goes out of scope, preventing memory leaks.
The _______ statement is used to prematurely exit a switch-case block.
- return
- break
- continue
- exit
The "break" statement is used to exit a switch-case block prematurely. Once a "break" is encountered, the control jumps out of the switch-case structure.
Imagine you are implementing a high-frequency trading system...
- std::fstream with buffering
- mmap based I/O
- std::cout
- std::ofstream with std::ios::sync_with_stdio(false)
mmap based I/O, which maps a file into memory, allows for very fast file operations since it avoids certain system calls and can result in fewer memory-copy operations than traditional read/write functions. This is crucial for high-frequency systems where latency is critical.