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.
How can you prevent users from mistakenly instantiating your template classes with non-numerical types in a C++ library of numerical methods, leading to confusing compiler errors?
- Provide runtime type checking
- Use static_assert with type traits
- Offer detailed documentation
- Implement virtual functions
The static_assert mechanism, combined with type traits, allows for compile-time checks. By using static_assert along with type traits like std::is_arithmetic, it's possible to generate clear, intentional compiler errors if a user tries to instantiate a template with a non-numeric type, thus providing instant feedback to the user.
Which data type would be most appropriate for storing a boolean value?
- int
- char
- double
- bool
The bool data type is specifically designed in C++ to store boolean values, which can be either true or false. Using other data types like int, char, or double for boolean values would be inefficient and not semantically clear. Thus, bool is the most appropriate choice for this purpose.
What is the purpose of the this pointer in C++?
- To return the current object's address.
- To point to global variables.
- To create a new object.
- To delete the current object.
In C++, the this pointer is used within a class's member function to refer to the invoking object of that function. It's an implicit parameter to all member functions and contains the memory address of the current object, allowing for operations on the calling object itself.