Which type of loop is best suited when the number of iterations is known ahead of time?
- do-while loop
- while loop
- for loop
- recursive function
A for loop is best suited when the number of iterations is known ahead of time, as it allows for an initial statement (typically for initialization), a condition check, and an increment/decrement operation in a compact syntax.
How does C++ enforce encapsulation and abstraction in multi-level inheritance scenarios?
- By using abstract classes only.
- By private and protected access specifiers.
- Through use of virtual functions alone.
- By using friend functions.
In C++, encapsulation and abstraction in multi-level inheritance are primarily enforced using private and protected access specifiers. While private members are inaccessible outside the class, protected members can be accessed by derived classes, ensuring a controlled inheritance mechanism.
What is the main purpose of using protected access specifier in base class?
- To restrict all external access
- To allow all classes to access
- To enable inheritance only
- To share methods with friends
The protected access specifier in C++ is mainly used to ensure that members are inaccessible from outside the class (just like private members). However, the difference is that protected members can be accessed by derived classes. This allows for more secure inheritance, giving derived classes access without exposing to the external world.
Which of the following data types can store a non-integer number?
- char
- float
- int
- short
In C++, the float data type is used to store single-precision floating-point numbers, which can represent non-integer values (like 3.14). It provides a way to store decimal numbers with a floating decimal point.
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.