What does the break statement do in a loop structure?
- Exits the entire program.
- Skips to the next iteration.
- Exits the loop immediately.
- Continues to execute the loop from the start.
The break statement in C++ is used to exit the loop immediately, bypassing the rest of the loop's code and any remaining iterations. This can be especially useful for exiting a loop when a specific condition is met.
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.
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.
Which access specifier in C++ allows a class member to be accessible only within the class and friends of the class?
- public
- private
- protected
- external
The private access specifier in C++ ensures that class members are only accessible within the class and not outside. Additionally, friend functions or classes can access these private members, but they remain hidden from other parts of the program.
What is the effect of the continue statement inside a for loop?
- It pauses the loop for a second
- It terminates the loop
- It jumps to the next iteration
- It reruns the current iteration
Inside a for loop, the continue statement causes the loop to skip the rest of its body and jump immediately to the next iteration. This is particularly useful when certain conditions within the loop need to be avoided for specific iterations.
How does an inline function improve the efficiency of a C++ program?
- By reducing memory usage
- By reducing runtime overhead
- By decreasing code size
- By performing lazy evaluation
Inline functions are a suggestion to the compiler to insert the complete body of the function wherever that function is used in the code. This can improve efficiency by eliminating the overhead of a function call, potentially leading to faster runtime performance.