Which of the following is a correct statement about friend functions in C++?
- They are member functions of a class
- They can be inherited by derived classes
- They always improve performance of the program
- They do not have access to the 'this' pointer of the class
In C++, the 'this' pointer is an implicit pointer to the object on which a member function is called. Since friend functions are not member functions of a class, they do not have access to this pointer. They operate as external functions with special access permissions.
Which parameter-passing method does not allow the function to modify the actual argument?
- By value
- By reference
- By pointer
- By name
When parameters are passed by value, a copy of the argument is made for the function. Any modifications to this copy do not affect the original argument outside the function.
How does a range-based for loop work in C++11 and above?
- It works only on arrays and not on containers.
- It iterates over elements using indices.
- It allows iterating over all elements in a container or array directly.
- It requires manual initialization of iterators.
In C++11 and newer versions, the range-based for loop provides a more readable syntax to iterate over all elements of a container or an array. The loop automatically gets the begin and end iterators of the range and iterates over each element without needing manual indexing.
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.
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.
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.
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.
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.
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().
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.
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.
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.