What is the primary reason behind certain languages optimizing tail recursion?
- To improve memory efficiency
- To make code look cleaner
- To speed up compilation time
- To allow deeper recursion depth
Tail recursion optimization, often referred to as Tail Call Optimization (TCO), primarily serves to improve memory efficiency. When a recursive call is the last thing a function does (tail recursion), some languages can optimize it to reuse the current function's stack frame for the next function call. This can lead to significant savings in stack space, especially for deep recursive calls. It allows recursion-intensive algorithms to run without consuming as much memory or leading to a stack overflow.
Which operator is used to access the memory address of a variable in C++?
- * (Asterisk)
- & (Ampersand)
- % (Percent)
- + (Plus)
The "&" (ampersand) operator is used to get the memory address of a variable in C++. For instance, if int x = 10;, then &x will give the memory address where the integer x is stored. The asterisk (*), on the other hand, is used for dereferencing.
Which of the following statements about the struct and class keywords is true?
- Only structs can have member functions
- Structs are always public, and classes are always private
- Both can have member functions, but default access is different
- Classes support inheritance, structs don't
Both struct and class in C++ can have member functions, data members, and can support inheritance. The primary difference is the default access level: members of a struct are public by default, whereas members of a class are private. Other functionalities between them are essentially identical.
To remove all instances of a particular value from a C++ STL vector, you should use the _______ algorithm.
- find
- sort
- erase
- remove
While erase can remove elements from a vector, to efficiently remove all instances of a value, you'd first use remove to move those elements to the end and then use erase. This is known as the "erase-remove" idiom in C++.
The keyword _______ is used to access the base class’s members in the derived class.
- super
- this
- parent
- base
In C++, the "base" keyword is not actually used. The correct way to access base class members is through the use of the base class name or the scope resolution operator. However, "base" is a term from C#.
The concept of encapsulation involves bundling the data () and the methods () that operate on the data into a single unit.
- objects, functions
- variables, operators
- members, methods
- attributes, functions
Encapsulation is a key concept in object-oriented programming where the data (members) and the functions (methods) that operate on this data are bundled together as a single unit known as a class.
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.
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 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.
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.