What is the size of the 'int' data type on a typical 32-bit system?
- 16 bytes
- 2 bytes
- 4 bytes
- 8 bytes
On a typical 32-bit system, the 'int' data type in C++ has a size of 4 bytes. This means it can store integer values within the range of approximately -2 billion to 2 billion.
Alex wants to create a menu-driven program that keeps displaying the menu until the user selects the 'Exit' option. Which loop is best suited for this task?
- do-while loop
- for loop
- switch loop
- while loop
Alex should use a do-while loop for this purpose. A do-while loop executes a block of code at least once and then repeats it based on a condition. It's ideal for menu-driven programs because it ensures that the menu is displayed at least once, even if the user chooses to exit immediately.
Which loop structure is best suited for scenarios where the number of iterations is known beforehand?
- do-while loop
- for loop
- if-else loop
- while loop
The for loop is best suited for scenarios where the number of iterations is known beforehand. It's a compact loop structure that allows you to initialize a variable, define a condition, and specify an increment or decrement in a single line. This makes it ideal for loops with a fixed number of iterations, such as iterating through arrays or performing a set number of calculations.
In C++, if a floating-point number is assigned to an integer type, the value will be ______.
- Rounded Up
- Truncated
- Type Error
- Undefined
When a floating-point number is assigned to an integer type in C++, the value will be truncated. This means that the fractional part of the floating-point number is discarded, and only the whole number part is stored in the integer variable. It's essential to be aware of this behavior to prevent unexpected results in your code.
Ethan is writing a large C++ project. To maintain clarity, he wants to keep function declarations separate from their definitions. How might he structure his program to achieve this?
- Combine declarations and definitions in a single source file
- Use header files (.h) for declarations and source files (.cpp) for definitions
- Use inline functions for both declarations and definitions
- Use namespaces to separate declarations from definitions
To keep function declarations separate from their definitions in C++, Ethan can use header files (.h) for declarations and source files (.cpp) for definitions. This practice enhances code organization, reusability, and maintainability in large projects.
Bob is noticing that his compiled program size is much larger than expected after he marked several lengthy functions as inline. What could be a reason for this?
- Code Duplication
- Compiler Optimization
- Linker Error
- Memory Leaks
One reason for Bob's compiled program size to increase after marking lengthy functions as inline is code duplication. When functions are marked as inline, their code is replicated at each call site. If the function is lengthy and called from multiple places, this can lead to increased code size. It's essential to strike a balance between inlining for performance and avoiding excessive code duplication.
What will be the flow of control in a switch statement if there is no break keyword at the end of a case?
- It will continue to execute the next case
- It will return to the main function
- It will terminate the switch statement
- It will throw an error
In C/C++, if there's no break statement at the end of a case in a switch statement, the control will continue to execute the next case statements without any regard for the case conditions. This behavior is known as "fall-through." It's important to use break statements to avoid unintended fall-through.
In a switch statement, each case ends with the ______ keyword to prevent fall-through.
- break
- continue
- goto
- return
In a switch statement in C++, each "case" ends with the "break" keyword. The "break" statement is crucial in switch statements to prevent fall-through, meaning that once a case is matched and its code block is executed, the program exits the switch block.
The main function's standard signature returns an integer and accepts ______ arguments
- four
- one
- three
- two
The standard signature for the main function in C# and many other programming languages returns an integer and accepts two arguments, typically string[] args for command-line arguments.
In C++, the conditional operator ______ is a ternary operator.
- ?:
- if-else
- switch
- while
In C++, the conditional operator "?:" is commonly known as the ternary operator. It is a shorthand way to write conditional expressions and is often used to assign values based on a condition. The operator takes three operands: a condition, a value if the condition is true, and a value if the condition is false.
How would you declare a variable pi that holds the value of pi up to several decimal places?
- decimal pi = 3.14159265359;
- double pi = 3.14159265359;
- float pi = 3.14;
- int pi = 3;
In C++, to store the value of pi up to several decimal places, you would typically use the double data type. The double data type provides greater precision compared to float or int, making it suitable for representing the value of pi with more decimal places.
For a function whose return type is deduced at the time of its invocation, the keyword ______ is used.
- deduce
- infer
- decltype
- auto
For a function whose return type is deduced at the time of its invocation, the keyword 'auto' is used. This is part of the C++11 feature where the compiler determines the return type based on the function's implementation. It allows for more flexible and concise code.