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.
The expression a×b can be represented as a _______ b in C++.
- *
- %
- +
- -
In C++, the asterisk () is used as the multiplication operator. So, "a×b" is represented as "ab" in C++ programming.
Which method will you use to write a single character to the file stream in C++?
- put()
- write()
- insert()
- push()
The put() method is used to write a single character to a file stream in C++. The write() method is typically used to write blocks of binary data, while insert() and push() aren't standard methods for this purpose in C++ streams.
The _______ keyword is used when we want to explicitly pass by reference but prevent the function from modifying the argument.
- const
- static
- virtual
- inline
The const keyword is used in C++ to indicate that a value shouldn't be modified. When passing an argument by reference, using const before the parameter type ensures that the function does not alter the original data, providing a level of safety.