What does the new operator do in C++?
- Deletes memory
- Compares memory
- Allocates memory
- Modifies memory
The new operator in C++ is used to allocate memory on the heap at runtime. When you use new, it returns a pointer to the beginning of the block of memory it allocated. This is essential for dynamic memory management, as it allows more flexible storage allocation during program execution.
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.
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.
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.
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 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.
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.
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.
When performing a bitwise AND operation with a number and 0, the result is always _______.
- positive
- zero
- negative
- unchanged
When any number is bitwise AND-ed with 0, the result is always 0. This is because the AND operation yields 1 only if both bits being compared are 1; otherwise, it yields 0.
What is the output of the logical NOT (!) operator when applied to a non-zero operand?
- 0
- 1
- Undefined
- None
In C++, any non-zero value is considered as true. The logical NOT (!) operator inverts the truthiness of its operand. So, if applied to a non-zero (true) operand, the output will be false, which is represented as 0.