Question: Answer these multiple Choice Questions; If you've been given a pointer (e.g. string *s), and you want to use the thing it points to, which

Answer these multiple Choice Questions;

If you've been given a pointer (e.g. string *s), and you want to use the thing it points to, which of these operators should you use?

Question 1 options:

1.

new

2.

<<

3.

*

4.

&

Question 2:

What's wrong with this code?

int priceOrange() { Orange *o = new Orange; o->radius = 7; return o->getPrice(); }

Question 2 options:

1.

It has a memory leak

2.

It dereferences a null pointer

3.

It dereferences an uninitialised pointer

4.

It has a use-after-free error

Question 3:

What's wrong with this code when likeCitrus is false? (Orange is a subclass of Fruit.)

void eatFruit(bool likeCitrus) { Fruit *f = nullptr; if (likeCitrus) { f = new Orange(); } f->eat(); delete f; }

Question 3 options:

1.

It has a memory leak

2.

It dereferences a null pointer

3.

It dereferences an uninitialised pointer

4.

It has a use-after-free error

Question 4:

What's wrong with this code? (Apple, Melon and Orange are subclasses of Fruit.)

void makeFruitSalad() { // An array of pointers to Fruit -- each element is a Fruit * Fruit *chunks[3]; chunks[0] = new Apple; chunks[1] = new Melon; chunks[2] = new Orange; for (int i = 0; i < 3; i++) { delete chunks[i]; } Fork *f = new Fork; for (int i = 0; i < 3; i++) { cout << "Eating " << chunks[i]->getDescription() << " "; f->pickUp(chunks[i]); f->eat(); } delete f; }

Question 4 options:

1.

It has a memory leak

2.

It dereferences a null pointer

3.

It dereferences an uninitialised pointer

4.

It has a use-after-free error

Question 5 (1 point)

Which of the following is not a good way to improve the speed of a performance-critical piece of code?

Question 5:

1.

Reduce the number of memory accesses it performs

2.

Run it on a machine with a smaller L1 cache

3.

Rearrange the data it's using to be stored in consecutive memory locations

4.

Reduce the number of instructions it executes

Question 6:

You are working on a machine where the double type is 8 bytes long, and cachelines are 64 bytes. You've written the following code:

double values[1024]; values[5] = 42;

Assuming that values[0] is located at the start of a cacheline (the array is "cacheline-aligned"), which elements of values should you expect to be in the cache after this code has executed?

Question 6 options:

1.

0 to 7

2.

5 to 12

3.

0 to 5

4.

Only 5

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!