How does a member function differ from a regular function in C++?
- It can only be called by an object.
- It does not have a return type.
- It always has parameters.
- It cannot be defined inside a class.
A member function in C++ is associated with an object of the class and has access to the object's data members. Unlike regular functions, member functions can be called using the object of the class and can manipulate the class's attributes directly. Regular functions don't have this implicit access.
What is the impact on performance when using float versus double in mathematical calculations in C++?
- float is faster
- double is faster
- No difference
- It depends on context
While "float" may use less memory, "double" provides higher precision. The performance impact depends on the architecture and context. Some architectures are optimized for double precision, making operations as fast, if not faster, than float operations.
In a real-time graphics application where performance is critical, you need to manage a dynamically-allocated array that can change size. Which of the following STL containers would be the most appropriate choice considering the balance between dynamic size management and performance?
- vector
- list
- deque
- forward_list
The STL vector is a dynamic array that provides O(1) access time for its elements. It's efficient when resizing (amortized constant time for insertions/deletions at the end), making it suitable for scenarios requiring a balance between dynamic size management and performance.
You are debugging a C++ application and find that a goto statement is causing erratic jumps and consequently, unexpected behavior. Which refactoring approach might be the most beneficial to enhance code readability and maintainability?
- Replace goto with structured loops.
- Encapsulate the goto in a function.
- Add comments explaining the goto.
- Ignore it and move on.
Using goto statements can lead to spaghetti code that's hard to maintain and debug. It's considered a best practice to avoid using goto in C++. Replacing goto with structured loops or conditionals can often make the code clearer and more maintainable. While encapsulation is a good practice in general, it doesn't address the fundamental problems associated with goto. Comments can help but don't solve the root issue.
The process of transferring the program control from where the exception was thrown to the catch block is known as _______.
- Throwing
- Handling
- Propagation
- Initialization
Propagation refers to the process where after an exception is thrown, it travels up the call stack until it's caught by an appropriate catch block or, if uncaught, the program terminates.
Which of the following is true about the complexity of the std::list::remove function?
- O(1)
- O(log n)
- O(n)
- O(n log n)
The std::list::remove function has a linear time complexity O(n). This is because it traverses the list once, removing all elements that match a specified value. As it potentially examines all elements, the complexity is linear.
To get the quotient of a division in C++, we use the _______ operator.
- +
- /
- %
- &&
In C++, the forward slash (/) operator is used for division. When used between two integers, it gives the quotient of the division.
What is the size (in bytes) of an int data type in most C++ implementations?
- 1
- 2
- 4
- 8
In most C++ implementations, particularly those that follow the x86 architecture and many 32-bit systems, an int data type occupies 4 bytes in memory. However, it's essential to note that the size can vary based on the platform and compiler.
Which smart pointer in C++ retains shared ownership of an object through a pointer?
- auto_ptr
- unique_ptr
- weak_ptr
- shared_ptr
shared_ptr is a type of smart pointer in C++ that retains shared ownership of an object. This means multiple shared_ptr objects can own the same object, and the object will only be destroyed when the last shared_ptr owning it is destroyed or reset. This helps in preventing memory leaks and ensuring safer memory management.
When an enum is declared, it creates a new _______.
- function
- data type
- variable
- class
When an enumeration (enum) is declared in C++, it essentially creates a new user-defined data type. This allows the programmer to define variables of this enumeration type and assign any of its enumerator values to these variables.
Using templates excessively can lead to an issue known as _______.
- recursion
- template bloat
- overloading
- specialization
Excessive use of templates can result in code that has much larger compiled sizes than equivalent non-template code. This phenomenon, where the binary gets bloated due to templates, is known as template bloat.
In C++, a size of long double is at least _______ bytes.
- 8
- 10
- 12
- 16
In C++, the size of long double is compiler-dependent. However, according to the C++ standard, it is at least 8 bytes or the size of a double, whichever is greater. Some compilers may allocate more.