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.
Consider a class hierarchy with Base and Derived classes. An object of Derived throws an exception that is caught in a catch block for Base exceptions. What considerations might be relevant to the use of dynamic_cast inside the catch block?
- Checking the type of the exception.
- Re-throwing the exception.
- Modifying the caught object in the catch block.
- Use of multiple inheritance.
When an exception of Derived type is caught as its base type, using dynamic_cast can help determine the exact type of the caught exception. This can be useful if you want to take specific actions based on the exact type of the exception. Remember, dynamic_cast will return a nullptr if the cast is not valid for pointers or throw a std::bad_cast exception for references, so it's essential to check for these cases.
What would be the result of instantiating a class template with a user-defined type that does not meet the template’s expected type requirements?
- A runtime error
- A compilation error
- The template would adapt automatically
- The system would crash
Instantiating a class template with a type that doesn't meet the template's requirements would result in a compilation error. Templates rely on the types they are instantiated with to adhere to specific criteria. If those criteria aren't met, the compiler will produce an error message indicating the discrepancies.
When using the logical AND operator, if the left operand is false, the right operand is _______ evaluated.
- always
- sometimes
- never
- conditionally
In C++, the logical AND (&&) operator employs short-circuit evaluation. If the left operand is false, the right operand isn't evaluated because the overall result will already be false.
Which function is used to read a single character from a file in C++?
- extract()
- get()
- fetch()
- pull()
The get() method is used to read a single character from a file in C++. While extract() is not a standard method for this purpose, fetch() and pull() are not associated with file reading in the standard C++ library.
In C++, if a class makes a non-member function its friend, that function gets the ability to access the _______ and _______ members of the class.
- public, private
- private, protected
- public, static
- static, protected
In C++, when a non-member function is declared as a friend of a class, it can access both private and protected members of that class. Public members are accessible to all functions, so the unique ability granted by the friendship is access to the private and protected members.
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".