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.
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.
A pointer passed by value to a function allows the function to change the _______ to which the pointer points.
- value
- address
- direction
- data type
When a pointer is passed by value to a function, the function gets a copy of the pointer. This means that the function can modify the data at the address the pointer points to, but it cannot change the address of the original pointer itself.