C/C++ Performance Optimization and Low-Level Concepts Quiz Quiz

Explore essential concepts in C and C++ performance optimization, including memory usage, compiler behavior, and efficient programming techniques. This quiz is designed to reinforce understanding of low-level language features and practical strategies that impact program efficiency.

  1. Inlining Functions

    What is one typical benefit of using the 'inline' keyword for a small function in C++?

    1. It may reduce the overhead of function calls
    2. It converts the function to assembly code
    3. It forces the compiler to ignore the function
    4. It disables recursion in the function

    Explanation: Using 'inline' suggests to the compiler to replace the function call with the function’s code, reducing the overhead of a call. It does not disable recursion by itself, nor does it convert code to assembly directly. The keyword is only a hint and does not force the compiler to ignore the function definition.

  2. Loop Unrolling Purpose

    Why is loop unrolling sometimes used as a performance optimization technique in C/C++?

    1. To decrease the number of loop control instructions
    2. To improve code readability
    3. To increase the recursion depth
    4. To prevent compiler warnings

    Explanation: Loop unrolling reduces the overhead of loop control instructions by performing more work per iteration. It does not affect recursion or necessarily improve code readability, and it's not primarily aimed at suppressing warnings. The main goal is faster execution by reducing the number of jumps and comparisons.

  3. Cache-Friendly Data Layout

    For optimal cache performance when processing large arrays in C, which data access pattern is preferred?

    1. Skipping every other element when accessing
    2. Accessing elements sequentially by increasing index
    3. Accessing random elements in the array
    4. Accessing only the first element repeatedly

    Explanation: Sequential access ensures that data is loaded into the cache efficiently due to spatial locality. Random or skipping patterns do not make good use of cache lines, and focusing on just one element doesn't utilize cache bandwidth. Accessing sequentially benefits most from the memory subsystem.

  4. Static vs Automatic Variables

    How does a 'static' local variable differ from an automatic local variable in C?

    1. An automatic variable is visible outside the function
    2. An automatic local variable is initialized only once
    3. A static local variable retains its value between function calls
    4. A static local variable is stored on the stack

    Explanation: Static local variables keep their value between invocations because they are stored in static storage. They are not kept on the stack, unlike automatic variables. Automatic locals are initialized every time the function is called and are not visible outside the function.

  5. Pointer Arithmetic

    Given 'int arr[10];', what is a valid result of the expression 'arr + 3' in C?

    1. It gives the number 3
    2. It gives the address of the fourth element in arr
    3. It dereferences the array
    4. It results in a compilation error

    Explanation: In pointer arithmetic, 'arr + 3' advances the pointer to the address of the fourth element, since arrays are zero-indexed. It does not yield the number 3, nor does it directly dereference or cause a compilation error if used correctly. Pointer addition is a valid low-level operation in C.

  6. Why Use 'restrict' Keyword

    What is the purpose of the 'restrict' keyword in C function parameters?

    1. To inform the compiler that the pointer is the sole initial means of accessing the object
    2. To mark a variable as static
    3. To allocate memory dynamically
    4. To make a variable constant

    Explanation: 'restrict' tells the compiler no other pointer will access the memory object, allowing better optimization. It does not make variables constant or allocate memory, nor does it relate to static storage. The keyword is specifically for optimizing pointer usage.

  7. Avoiding Memory Leaks

    Which practice helps prevent memory leaks when using dynamic memory allocation with 'malloc' in C?

    1. Avoiding pointer arithmetic
    2. Declaring pointers as automatic variables only
    3. Defining arrays statically
    4. Always freeing memory with 'free' after use

    Explanation: Memory allocated with 'malloc' must be released with 'free' to avoid leaks. Using automatic variables or static arrays can avoid allocation altogether, but do not address leaks if dynamic allocation is used. Avoiding pointer arithmetic alone does not prevent leaks.

  8. Short-Circuit Evaluation

    What optimization benefit does short-circuit evaluation provide in C logical expressions such as 'if(a u0026u0026 b)'?

    1. It requires all conditions to be checked always
    2. It avoids unnecessary evaluation of the second operand if the first is false
    3. It disables compiler optimizations
    4. It increases the size of the generated code

    Explanation: Short-circuiting means that if the first operand of 'u0026u0026' is false, the second is not evaluated, saving processing time. This reduces code execution, not increases it. All conditions are not always checked, and it actually enables, not disables, further optimization.

  9. Zero-Cost Abstraction

    Which statement accurately describes a zero-cost abstraction in C++?

    1. An abstraction that does not add performance overhead compared to manual code
    2. An abstraction that doubles the code size
    3. An abstraction that can only be used with macros
    4. An abstraction that must be interpreted at runtime

    Explanation: A zero-cost abstraction strives to be as efficient as hand-written code, with no extra overhead. It is not related to runtime interpretation, increased code size, or macro-only usage. The goal is to provide both expressiveness and speed.

  10. Effect of 'const' in Functions

    Why should function parameters be declared as 'const' when possible in C/C++?

    1. To disable usage of inline functions inside the function
    2. To force the compiler to allocate memory on the heap
    3. To allow the compiler to make additional optimizations
    4. To prevent the function from being called

    Explanation: 'const' parameters signal that values will not change, allowing both the compiler and the reader to assume immutability, which can lead to better optimization. It does not affect function calls, force heap usage, or prevent inlining. Using 'const' is a best practice for clarity and performance.