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.
The _______ of a recursive function is a condition that does not use recursion to produce an answer.
- base case
- recursive loop
- iteration step
- end case
The base case of a recursive function provides the termination criteria for the recursion. Without a base case, the recursive calls would go on indefinitely, leading to a stack overflow. The base case typically provides a straightforward solution without the need for further recursion.
When dealing with complex conditions, it might be beneficial to use a _______ in conjunction with if-else structures to improve code clarity and maintainability.
- truth table
- destructor
- loop
- constructor
Using a truth table can help in analyzing and simplifying complex conditions. By representing all possible input combinations and their results, a truth table provides a clear view of the logic, helping in both implementation and debugging of if-else structures.
The bitwise NOT operator (~) performs a _______ operation on each bit of a number.
- negation
- AND
- OR
- XOR
The bitwise NOT operator (~) inverts each bit of a number. If the bit is 0, it becomes 1, and if it's 1, it becomes 0. This is essentially a negation operation on individual bits.
The return type of a function that does not return any value is specified as _______.
- char
- int
- double
- void
The "void" return type specifies that a function doesn't return a value. It's used when the primary purpose of the function is to perform an action, not produce data.
What happens if you try to delete memory using the delete operator more than once?
- It gets deleted twice
- No effect
- Program crashes
- Memory gets duplicated
Deleting a memory location more than once leads to undefined behavior, most commonly resulting in program crashes. Always ensure memory is deleted once and pointers are set to nullptr afterwards.
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.
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.
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 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.