Which of the following is a characteristic of an enum in C++?
- It can store multiple values at once
- It is used for dynamic memory allocation
- It consists of a set of named integer constants
- It defines object behavior
An enum in C++ consists of a set of named integer constants. It allows for creating a new data type where each value is represented by a unique name rather than a numeric value, enhancing the readability of the code. This means that an enum can only hold one of the values that you've defined.
To prevent memory leaks, every call to new should be matched with a call to _______.
- delete
- free
- clear
- remove
When dynamic memory allocation is done using new, it's necessary to release that memory using delete to prevent memory leaks in C++. This ensures that every byte of memory allocated is properly released, maintaining the health and efficiency of the system.
When replacing nested if-else structures with a switch-case, what is a common pitfall to avoid?
- Ignoring the break statement
- Using non-integer values for cases
- Avoiding the default case
- Putting the default case at the beginning
When using switch-case statements, it's essential to remember to include the "break" statement at the end of each case. Without it, the program may execute subsequent cases, leading to unintended behavior known as "fall-through." The "break" statement ensures that once a match is found, no other cases are executed.
The operator used to determine the remainder when one number is divided by another is _______.
- *
- %
- /
- +
In C++, the modulo operator % is used to determine the remainder of a division operation between two numbers. For example, 7 % 3 would yield a result of 1, as when 7 is divided by 3, the remainder is 1. It's a very useful operator, especially in situations that require calculations around loops and array indices.
You are optimizing a recursive algorithm for a real-time system where function call overhead is a critical concern. What strategy might be most effective in reducing the function call overhead while maintaining the logical structure of the algorithm?
- Use inline functions.
- Optimize the algorithm's base cases.
- Introduce parallel processing.
- Rely on external libraries.
Function call overhead is the extra time and resources needed when a function is called. Using inline functions can help reduce this overhead, especially in recursive algorithms, because the function's code gets inserted (or "inlined") at each point where the function is called, eliminating the need for the typical call and return process, which can be resource-intensive.
Which of the following scenarios is most suitable for using a switch-case statement?
- When comparing string values.
- When there are many conditions that can occur in any order.
- When the conditions are based on a range of values.
- When dealing with enumerated values.
The switch-case structure in C++ is best suited for scenarios where a single variable's value needs to be checked against a series of constants, often seen with enumerated values. It's a cleaner and more readable alternative to a long series of if-else statements in such cases.
The loop do { /* code */ } while(_______); will always execute the code block at least once.
- FALSE
- 1
- TRUE
- sometimes
The "do-while" loop in C++ always executes its body once before checking the condition. So even if the condition in the "while" part is "false", the body of the loop will execute once. This differentiates it from the "while" loop which checks the condition before the first execution.
What is the significance of the "default" case in a switch-case statement?
- It handles unspecified cases
- It serves as the primary case to be executed
- It is mandatory for all switch-case structures
- Acts as the else part in if-else structures
The "default" case in a switch-case statement is executed when none of the provided "case" conditions match the switch expression's value. It serves as a fallback and handles unspecified or unexpected values, ensuring that the switch has a response for all potential input. It is similar to the "else" in if-else.
Which of the following is a correct way to declare a function pointer in C++?
- int func()
- int *func()
- int (*func)()
- int &func()
In C++, a function pointer points to the address of a function. The correct syntax for declaring a function pointer is type (*pointer_name) (parameter list). In the given options, int (*func)() is the correct way to declare a function pointer returning an int.
What is the output of a function with a void return type?
- An integer
- A floating point number
- Nothing
- A character
In C++, a function declared with a void return type does not return any value. It is used when the function needs to perform an action but doesn't need to send any data back to the calling function.
Imagine you're working on a large-scale software project involving numerous classes. Some classes are instantiating objects of another, and suddenly an object is accidentally deleted. What techniques or principles might be used to safeguard against accidental deletion in such a situation?
- Use of smart pointers.
- Overloading the delete operator.
- Enabling strict type-checking.
- Always use dynamic allocation.
Smart pointers in C++ are template classes that manage the lifetime of dynamically allocated objects. They ensure that the objects they manage are properly deleted, thus preventing accidental deletions or memory leaks. Overloading the delete operator or enabling strict type-checking wouldn't inherently prevent accidental deletions.
Which of the following smart pointers does not take ownership of the pointed object?
- unique_ptr
- shared_ptr
- weak_ptr
- auto_ptr
weak_ptr is a smart pointer that holds a non-owning reference to an object managed by shared_ptr. It doesn't affect the reference count and won't prevent the object from being deleted.