An infinite loop can be intentionally created for program structures like event listeners using for(;;), which is often referred to as a _______ loop.
- endless
- continuous
- forever
- looping
The term endless loop is often used to describe loops that have no termination condition, such as for(;;), and they continue executing indefinitely until externally terminated.
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.
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.
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.
How does the C++ compiler handle different types of exceptions in a function template?
- It uses dynamic casting to determine the type.
- It uses overloading to handle exceptions.
- Each instantiation gets its own set of exceptions.
- The compiler ignores type-specific exceptions.
In C++, when a function template is instantiated, it gets its own version of the function, complete with its own set of exceptions. This means that if different instantiations of the function template throw different exceptions, each instantiation will have its own set of exceptions to catch. This provides a level of type-safety during exception handling.
You’re maintaining a legacy C++ codebase which has limited comments and documentation. You encounter an erratic bug that only appears in the optimized build. What strategy might be most effective in isolating and fixing the bug?
- Add extensive documentation first
- Compare optimized and unoptimized assembly
- Refactor the entire codebase
- Use printf debugging
Comparing the optimized and unoptimized assembly can provide insights into how the compiler is altering the code, potentially revealing the source of the erratic behavior. It's a meticulous process but can be effective for bugs that only manifest in optimized builds due to compiler transformations.
What is a potential risk of using recursion?
- Always faster than loops.
- Cannot handle large input data.
- Guarantees better readability.
- Uses less memory than loops.
One of the potential risks of using recursion is that it can lead to excessive memory use, especially when dealing with large input data. Every recursive call adds a new layer to the system's call stack, which can eventually result in a stack overflow if unchecked.
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.
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.
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.