What will happen if a function does not return a value but has a return type other than void?
- It will cause a runtime error.
- It will return a random value.
- The program will not compile.
- The function will return zero.
When a function in C++ is declared with a return type other than void, it's a promise that the function will return a value of that type. If the function does not return any value, it will lead to a compilation error because the contract of returning a value (as per its declaration) is violated.
Which type of inheritance in C++ restricts a derived class from inheriting from more than one base class?
- Single inheritance
- Multiple inheritance
- Hierarchical inheritance
- Hybrid inheritance
Single inheritance restricts a derived class to inherit from only one base class. While C++ supports multiple inheritance, where a class can inherit from more than one class, single inheritance ensures there's a straightforward lineage from the base to the derived class, avoiding complexities and potential ambiguities.
In C++, using goto to jump over the initialization of a variable will result in _______.
- compilation error
- uninitialized variable
- runtime exception
- optimized behavior
Jumping over the initialization of a variable using the goto statement will result in a compilation error because the compiler will identify it as an attempt to bypass variable initialization.
When implementing a finite state machine (FSM) in C++, which control structure might be more advantageous?
- Use if-else exclusively
- Use switch-case exclusively
- Use loops
- Depend on external libraries
A switch-case is generally more advantageous for implementing FSM in C++ because it improves readability when dealing with a set of known, discrete values (like states). It's also more performant than a chain of if-else statements in scenarios where there are multiple states.
The _______ smart pointer in C++ does not allow the pointer to be shared among objects.
- unique_ptr
- weak_ptr
- shared_ptr
- custom_ptr
unique_ptr is a smart pointer that owns a dynamically allocated object exclusively. It ensures that only one unique_ptr can point to the object at a time. If another unique_ptr tries to take ownership, the original will relinquish its hold, ensuring uniqueness.
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.
Which of the following is not a legitimate reason to use function templates?
- To support multiple types with the same function logic
- To increase execution speed
- To reduce code size
- To create a list of unrelated types
Function templates are utilized primarily to enable generic programming, meaning writing code that works for multiple data types without repetition. While they may have indirect effects on execution speed or code size, using them to group unrelated types doesn't align with their primary purpose.
A class whose objects cannot be created is known as a _______ class.
- abstract
- virtual
- static
- inline
An abstract class in C++ is a class that cannot be instantiated, meaning objects of the class cannot be created directly. Abstract classes are intended to be inherited by other classes.
The maximum value that can be stored in an unsigned char is _______.
- 127
- 1024
- 256
- 255
The unsigned char data type in C++ uses 8 bits (1 byte) for storage. This means it can store values ranging from 0 to 2^8-1, which is 0 to 255.
Unlike if-else, switch-case in C++ does not support _______ type for case values.
- floating-point
- integer
- character
- boolean
In C++, switch-case constructs don't support floating-point types for case values. This means you can't use float or double values as cases. On the other hand, if-else structures can easily handle conditions based on floating-point values.
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.
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.