In a function that needs to return multiple values, which method of parameter passing might be employed to facilitate this without using data structures like tuples or pairs?
- Using global variables
- Using static variables
- Passing by reference
- Using std::vector
When parameters are passed by reference to a function, the function can modify the original values of those parameters. By passing additional parameters by reference, a function can effectively "return" more than one value without relying on composite data structures like tuples or pairs. This method is often used for out parameters in functions.
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.
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.