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.
What is a friend function in C++?
- A function that always returns a friend object.
- A private function of a class.
- A function that can access private members of a class.
- A function that can only be called within its class.
A friend function in C++ is a special function that, although not a member of a class, has access to its private and protected members. This is used to allow non-member functions and other classes to access such members.
Consider a large-scale software project that heavily utilizes templates and generic programming. With an impending deadline, the compilation time is becoming a significant bottleneck. Which C++ feature/technique might be best suited to reduce compilation times without altering runtime performance?
- Decrease template depth
- Explicit template instantiation
- Use dynamic polymorphism
- Use of inline functions
Explicit template instantiation can help reduce compilation times by instructing the compiler to instantiate the templates in a specific source file rather than whenever they're used. This can help reduce redundancy and speed up the compilation of large projects that heavily rely on templates.
Consider enum class in C++11 and above. Which of the following is a true statement about its functionality?
- It allows for automatic type conversion.
- It cannot be forward declared.
- It restricts the scope of the enumerator values to the enumeration.
- It cannot be used with switch statements.
In C++11 and later, enum class (or "strongly typed enumerations") was introduced. Unlike traditional enums, the enumerators of an enum class are not exported to the surrounding scope, thus preventing name clashes. This essentially gives them a "scoped" behavior. This feature enhances type safety and code clarity.
You are tasked with optimizing a piece of code containing nested loops...
- Move calculations outside of the loops.
- Replace inner loop with a lookup table.
- Inline the inner loop calculations.
- Prefetch data before processing in the inner loop.
A lookup table can be used to store pre-computed results of complex calculations. By replacing the inner loop's calculations with lookups from this table, one can often achieve significant speedups, especially if the same calculations are being performed repeatedly.
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.
When passing parameters by reference, which symbol is used to denote reference in C++?
- #
- @
- &
- %
In C++, the ampersand (&) is used to denote that a parameter is being passed by reference. This allows the function to directly access and modify the actual argument's memory location.