Which operator is used to compare if two values are equal in C++?
- ==
- =
- ===
- !=
In C++, the == operator is used for equality comparison. It checks if the values of two operands are equal. On the other hand, = is an assignment operator, === is not valid in C++, and != is the not-equal operator.
What is the primary purpose of the ofstream class in file handling?
- Reading files
- Writing to files
- Deleting files
- Copying files
The ofstream class stands for output file stream. It is primarily used for performing output operations on files, meaning it's used for writing data to files. This class provides methods and properties needed to write data.
Which of the following data types is suitable for storing a character in C++?
- boolean
- char
- float
- int
In C++, the char data type is specifically designed to store characters. It typically occupies 1 byte in memory and can represent a single character from the ASCII table.
To prevent a class from being inherited, we declare it as _______.
- final
- static
- private
- mutable
In C++, the "final" keyword is used to prevent a class from being inherited. This means that no other class can be derived from a class declared as "final".
When class members are labeled as private, they are inaccessible outside the class, enforcing _______.
- encapsulation
- abstraction
- polymorphism
- inheritance
Encapsulation is a key concept in object-oriented programming where the internal details of a class are hidden and only specific functionalities are exposed. Making class members private enforces this encapsulation.
Regarding memory alignment and data packing, which of the following is true for structs in C++?
- Struct members are always packed tightly with no padding.
- Structs cannot be aligned in memory.
- Struct members have a defined order, but might have padding.
- Structs use dynamic memory for data storage.
Memory alignment and data packing are important considerations in C++ for optimizing memory usage and performance. In a struct, the order of declaration of members matters because the compiler might introduce padding between members to align data appropriately for the target architecture. This can affect the overall size of the struct.
How does the return statement interact with constructors or destructors in C++?
- It can exit a constructor prematurely.
- It causes the destructor to be called immediately.
- It can be used to return a value from the constructor.
- It has no use in constructors and destructors.
Constructors don't return values, so the return statement isn't used to return a value. However, it can be used to exit a constructor prematurely under certain conditions. Destructors also don't return values, and the use of a return statement in them would be to exit early, which is very rare.
In binary file operations, to write data of various data types, you use the _______ function.
- write
- fwrite
- push
- output
The write function is used in C++ for binary file operations. It allows the user to write data of various data types to a file. Unlike formatted output, write writes raw data bytes to the file.
What is the primary purpose of function templates in C++?
- Code obfuscation
- Memory conservation
- Code reusability
- Exception handling
Function templates in C++ enable the creation of functions that can operate on different data types without having to rewrite the entire function for each type. This promotes code reusability and reduces redundancy.
Which STL container provides constant time access to elements but may take linear time to insert a new element?
- std::vector
- std::queue
- std::array
- std::list
The std::vector container provides constant time (O(1)) access to elements using random access iterators. However, insertions (especially in the middle or at the beginning) might take linear time (O(n)) as elements need to be shifted to make space for the new element.