What is a potential risk of using recursion? 

  • Always faster than loops. 
  • Cannot handle large input data. 
  • Guarantees better readability. 
  • Uses less memory than loops.
One of the potential risks of using recursion is that it can lead to excessive memory use, especially when dealing with large input data. Every recursive call adds a new layer to the system's call stack, which can eventually result in a stack overflow if unchecked.

You’re maintaining a legacy C++ codebase which has limited comments and documentation. You encounter an erratic bug that only appears in the optimized build. What strategy might be most effective in isolating and fixing the bug? 

  • Add extensive documentation first
  • Compare optimized and unoptimized assembly 
  • Refactor the entire codebase 
  • Use printf debugging 
Comparing the optimized and unoptimized assembly can provide insights into how the compiler is altering the code, potentially revealing the source of the erratic behavior. It's a meticulous process but can be effective for bugs that only manifest in optimized builds due to compiler transformations.

How can you access the last element of a C++ STL vector named "myVector"? 

  • myVector.end() 
  • myVector.last() 
  • myVector.back() 
  • myVector[myVector.size()]
The member function back() provides direct access to the last element in a vector. On the other hand, myVector.end() returns an iterator pointing to the position after the last element, and myVector[myVector.size()] would result in undefined behavior since it tries to access out-of-bounds memory. There's no last() function in vector.

What does the -> operator do in the context of pointers in C++? 

  • Duplicates the pointer 
  • Adds values 
  • Dereferences pointer and accesses member 
  • Multiplies pointer values
The -> operator in C++ is used with pointers to objects. It dereferences the pointer and accesses the member of the object. For instance, if p is a pointer to an object, p->member accesses the member of the object.

How does friendship relation in C++ affect inheritance hierarchies, if at all? 

  • It overrides private inheritance. 
  • It allows base classes to access derived class members. 
  • It doesn't directly affect inheritance hierarchies. 
  • It breaks the "is-a" relationship in OOP.
Friendship is orthogonal to inheritance in C++. Declaring a function or class as a friend of another class doesn't have a direct impact on the inheritance hierarchy of classes. A friend function/class of a base class isn't automatically a friend of its derived classes.

You are developing a multi-threaded application where multiple clients are sending data to a server. Each client is handled in a separate thread and communicates with the server in a loop. If a client sends an incorrect data format, the server should ignore it and wait for the next data. How might continue be used in this case? 

  • To terminate the client's thread. 
  • To send an error message back to the client. 
  • To disconnect the client. 
  • To skip the processing of the incorrect data and wait for the next input.
In a multi-threaded context, when a client sends data in an incorrect format, the server can utilize the continue statement to skip any further processing of that particular piece of data and return to the beginning of the loop to wait for the next data. This ensures that the server doesn't waste resources processing invalid data but remains ready to handle valid inputs.

When dynamically allocating an array of integers using new, which of the following syntax is correct? 

  • int* arr = new int; 
  • int arr = new int[10]; 
  • int arr[10] = new int; 
  • int* arr = new int[10];
The correct way to dynamically allocate an array of integers in C++ using the new operator is int* arr = new int[10];. This syntax allocates memory for 10 integers on the heap and returns a pointer to the first element.

Which of the following data types has the smallest size in C++? 

  • int 
  • float 
  • char 
  • double
In C++, the char data type typically takes up 1 byte of memory, which is smaller than int, float, or double. The exact size of int can vary depending on the platform, but char is defined by the standard to always be at least 1 byte, making it the smallest of the given options.

Which of the following scenarios is the most suitable for applying recursion? 

  • Iterating through elements in an array. 
  • Calculating the total sales of a company. 
  • Solving problems that have repetitive structures. 
  • Creating graphical user interfaces.
Recursion is particularly suitable for problems that exhibit repetitive or nested structures. Examples include problems like calculating factorial, traversing trees, or solving the Towers of Hanoi. Such problems can be broken down into smaller instances of the same problem, making them perfect candidates for a recursive approach.

The friendship granted by a class A to a function or class B is _______ reciprocal, meaning [choose the correct statement]. 

  • always 
  • occasionally 
  • one-way 
  • not
The friendship in C++ is one-way. If class A declares class B or a function as its friend, it doesn't mean that class A automatically becomes a friend of class B or that function. Friendship must be explicitly granted. This means the granting of friendship is not reciprocal by default. Class B or the function would need to separately declare class A as a friend for the reverse to be true.

In professional C++ coding, it is often recommended to avoid using break and continue in favor of _______. 

  • structured logic 
  • recursive functions 
  • exception handling 
  • inline functions
While break and continue have their uses, over-reliance on them can make code harder to read and debug. Instead, it's often recommended to use clear and structured logic that doesn't rely on abrupt jumps within loops.

When a float is converted to an int, the value is _______. 

  • truncated 
  • rounded 
  • incremented 
  • exponentiated
When a floating-point number is converted to an integer in C++, any fractional part is discarded (i.e., truncated). This does not involve rounding, just a simple removal of the decimal and beyond.