How does the conditional (ternary) operator ? : associate in terms of precedence?
- It Depends on Compiler
- It Doesn't Associate
- Left-to-Right
- Right-to-Left
The conditional operator ?: associates left-to-right, meaning that expressions are evaluated in the order they appear. This is important when chaining multiple ternary operators together.
How does function overloading relate to the concept of polymorphism in C++?
- Overloading simplifies polymorphism
- Polymorphism replaces overloading
- Polymorphism simplifies overloading
- They are unrelated concepts
Polymorphism is a key concept in C++ that allows objects of different classes to be treated as objects of a common base class. Function overloading is a technique that enables polymorphism by providing multiple ways to call functions with different arguments, contributing to the versatility and flexibility of polymorphic behavior in C++.
Consider a for loop in C++ with an empty condition (i.e., for( ; ; )). What will be the behavior of this loop?
- It will not compile
- It will run indefinitely
- It will run once and exit
- It will throw an exception
A for loop with an empty condition (for( ; ; )) in C/C++ will run indefinitely until it is explicitly exited using a break statement or until an exit condition is encountered within the loop. It's often used for creating infinite loops, but caution is required to avoid infinite execution.
Which loop can potentially become an infinite loop if no increment/decrement operation is performed on the loop control variable?
- do-while loop
- for loop
- foreach loop
- while loop
A while loop can potentially become an infinite loop if no increment or decrement operation is performed on the loop control variable within the loop body. This happens when the loop condition always evaluates to true.
Which of the following is NOT a primitive data type in C++?
- Array
- Boolean
- Float
- Integer
In C++, an array is not a primitive data type; it's a user-defined data type used to store a collection of elements of the same data type. Primitive data types include integers, floats, and booleans, among others.
How does operator precedence affect the evaluation of complex expressions in C++?
- Operator precedence affects only mathematical operators.
- Operator precedence determines the order of evaluation in complex expressions.
- Operator precedence has no impact on the evaluation of expressions.
- Operator precedence is relevant only in assembly language.
Operator precedence is crucial in C++ for determining the order of evaluation in complex expressions. It ensures that operators with higher precedence are evaluated before those with lower precedence. Understanding operator precedence is essential for writing correct and predictable code.
To avoid ambiguities while overloading functions, always ensure clear differentiation in the ______.
- Access Modifier
- Function Name
- Parameter List
- Return Type
When overloading functions, the key is to differentiate them by their parameter lists. This clarity prevents ambiguity and helps the compiler determine which function to call based on the arguments provided. While return types and access modifiers are important, they are not the primary means of differentiation.
Emma is writing her first C++ program. She wants to use the cout and cin objects without qualifying them with std::. Which directive should she include at the beginning of her program?
- #include
- #include
- #include
- #include
Emma should include the directive #include at the beginning of her program to access the cout and cin objects without qualifying them with std::. This directive provides access to the standard I/O stream objects.
When would you use a do-while loop over a while loop?
- When you want to create an infinite loop.
- When you want to execute the loop body a fixed number of times.
- When you want to execute the loop body at least once.
- When you want to loop through a collection.
You would use a do-while loop over a while loop when you want to ensure that the loop body is executed at least once, regardless of whether the initial condition is true or false. The condition is checked after the first execution in a do-while loop.
If a function is declared but not defined in a program, what will be the outcome during the linking phase?
- Compilation error
- Linker error
- No error, it will use a default implementation
- Runtime error
If a function is declared but not defined in a program, it will result in a linker error during the linking phase. This error occurs because the linker cannot find the actual implementation of the function, and therefore, it cannot resolve references to that function.