Brownian Bridge™
Learn
Practice
Pricing
For employers
About
Contact
Log in
Start practicing
Courses
/
C++ for Quantitative Finance
/
01 — Article
/
Quiz
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?
`const double* p = weights + 1;`
`int len = n;`
`double w = weights[2];`
`weights[0] = 1.0;`
2.
Consider this code snippet:
```cpp
double* get_rate() {
double r = 0.05;
return &r;
}
```
What is the fundamental problem with this function?
It should return `const double*` instead of `double*`
`double` variables cannot be declared inside functions
It compiles only in C++20, not C++17
Returns a pointer to a local variable, destroyed on return — it dangles.
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;
```
Compile error: `new double[n]` requires a constant expression for `n`
The code leaks only `p[2]` through `p[4]`; `p[0]` and `p[1]` are correctly freed
Undefined behaviour: heap arrays need delete[], not scalar delete.
The code is well-formed; the array is correctly freed by delete.
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)`.
True
False
5.
In C++, what is the key performance advantage of allocating a `double path[252]` on the stack rather than via `new double[252]`?
Stack memory is in a faster CPU cache tier than heap memory
The compiler always vectorises stack arrays but not heap arrays
Stack arrays use 32-bit floats internally, halving memory bandwidth
A single stack-pointer bump (O(1), no allocator, no syscall); heap needs bookkeeping.
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.
True
False
Submit
← Back to article
Next: C++ Classes and Special Member Functions →