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.
How does C++ handle the virtual table in a multiple-inheritance scenario?
- Single table for all classes
- Separate tables for each base class
- A combination of both depending on visibility
- Virtual tables are not used
In C++, when a class inherits from multiple base classes, each base class will have its own virtual table. Thus, an object of the derived class will have separate virtual tables for each inherited class to handle dynamic dispatch for function calls, ensuring correct method resolution.
The members of a struct are _______ by default.
- private
- public
- protected
- static
In C++, the members of a structure (struct) are public by default. This means they can be accessed from functions outside the structure. This is in contrast to the members of a class, which are private by default and cannot be accessed or viewed outside of the class.
In C++, the data type _______ is commonly used to store large integers.
- float
- char
- long long int
- double
In C++, the long long int data type is designed to store very large integers. It can typically store at least 64 bits, allowing for a wide range of integer values far beyond the standard int.
How can a function indicate that it will not throw any exceptions?
- By using the noexcept specifier
- By returning a null pointer
- By using the try block without catch
- By marking it as static
The noexcept specifier in C++11 and later indicates that a function does not throw exceptions. If such a function does throw an exception, the program will terminate (usually by calling std::terminate). This can be a useful optimization hint for the compiler.
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.
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.
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.
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.
If a break statement is encountered in a nested loop, it will exit _______.
- the outermost loop
- the innermost loop
- the function
- the program
When a break statement is encountered within a nested loop, it only exits the loop in which it's directly contained, i.e., the innermost loop. It does not affect any outer enclosing loops.
How does C++ treat an enum regarding its type and size?
- As a float type with fixed size
- As an integer type with variable size
- As a class with member functions
- As a boolean type
Enums in C++ are treated as integer types. Their actual size can vary based on the compiler and the number of values they hold, but typically, they have the size of an int. C++11 introduced scoped enumerations (enum class) that offer better type safety, but fundamentally, they're still integers.