Considering cache efficiency, which STL container will provide the fastest element access and iteration in most cases?
- std::list
- std::deque
- std::vector
- std::set
std::vector is a dynamic array implementation. Due to its contiguous memory storage, it provides the best cache locality among the listed STL containers. This leads to faster element access and iteration, especially when compared to non-contiguous storage containers like std::list or node-based containers like std::set.
Which operator will give the remainder of a division operation?
- /
- *
- +
- %
The '%' operator in C++ is called the modulus operator. It is used to determine the remainder of a division operation between two integers. For example, '7 % 3' will return 1 as the remainder.
When an array is passed to a function, it is always passed by _______.
- value
- reference
- name
- address
When an array is passed to a function, it is passed by name, which essentially means the address of the array is passed. This allows functions to modify the original array. The behavior is similar to passing by reference.
A class that contains at least one pure virtual function is called a(n) _______.
- subclass
- instantiated class
- abstract class
- inherited class
An abstract class in C++ is a class that contains at least one pure virtual function. This means the class cannot be instantiated and is meant to be inherited by other classes that provide implementations for the pure virtual functions.
What potential issue might arise when using switch-case statements with enumerated types?
- Memory leaks.
- Switching to an incorrect case accidentally.
- Skipping the default case.
- Not handling newly added enumerators in the switch.
When using switch-case statements with enumerated types, if a new enumerator is added to the enumeration and not correspondingly handled in the switch statement, it can lead to potential issues or bugs. This is because the switch might not have a case for the new enumerator, and if there's no default case or if the default case doesn't handle it appropriately, the program can behave unpredictably.
If an exception is thrown and not caught anywhere in the program, function _______ is called.
- exit()
- main()
- terminate()
- throw()
If an exception is thrown but not caught anywhere in the program, the special function terminate() is called in C++. By default, terminate() ends the program. However, a programmer can change the function that gets called by using set_terminate().
In what scenario might the compiler ignore the inline keyword for a function?
- If the function has recursion.
- If the function is a template.
- If the function is defined outside the class.
- If the function uses a single return statement.
The inline keyword is a suggestion to the compiler, not a command. While compilers use their heuristics to decide, functions with recursion are typically not inlined because inlining recursive functions can lead to significant code bloat and possible stack overflow issues.
Which arithmetic operator is used to perform division in C++?
- *
- /
- +
- -
In C++, the '/' operator is used for division. If used between two integers, it will perform integer division; with floats or doubles, it performs standard division.
When defining a struct in C++, what does the compiler implicitly provide?
- Default constructor
- Virtual destructor
- Overloaded operators
- Private members
When defining a struct in C++, the compiler will implicitly provide a default constructor if no constructors are explicitly defined. However, it won't automatically provide virtual destructors, overloaded operators, or private members without explicit definitions.
The goto statement can lead to _______ if used indiscriminately.
- spaghetti code
- infinite loop
- syntax error
- recursion
Using the goto statement without careful consideration can lead to "spaghetti code", which is a term for code that is tangled and difficult to follow or maintain due to excessive jumps.