Quiz: C Language Foundations

Module 1 of 8 · Easy

Quick Quiz

1. A function receives `const double* weights` and `int n`. Inside the function, which of the following is a compile error?

2. Consider this code snippet: ```cpp double* get_rate() { double r = 0.05; return &r; } ``` What is the fundamental problem with this function?

3. What is the output of the following code? ```cpp int n = 5; double* p = new double[n](); p[0] = 1.0; p[1] = 2.0; delete p; ```

4. When a C-style array `double v[10]` is passed to a function declared as `void f(double* p)`, the function can determine the number of elements in `v` using `sizeof(p) / sizeof(double)`.

5. In C++, what is the key performance advantage of allocating a `double path[252]` on the stack rather than via `new double[252]`?

6. The following function is correct and safe: ```cpp void add_position(double& portfolio_value, const double price, const double quantity) { portfolio_value += price * quantity; } ``` Calling it as `add_position(pv, 105.0, 100.0)` modifies `pv` in the caller.