Question
Answer The Following Multiple Choice Questions And Choose the Correct Answer (A-D). Question 1: If you've been given a pointer (e.g. string *s), and you
Answer The Following Multiple Choice Questions And Choose the Correct Answer (A-D).
Question 1:
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:
A. | new |
B. | << |
C. | * |
D. | & |
Question 2:
What's wrong with this code?
int priceOrange() { Orange *o = new Orange; o->radius = 7; return o->getPrice(); }
Question 2 options:
A. | It has a memory leak |
B. | It dereferences a null pointer |
C. | It dereferences an uninitialised pointer |
D. | 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:
A. | It has a memory leak |
B. | It dereferences a null pointer |
C. | It dereferences an uninitialised pointer |
D. | It has a use-after-free error |
Question 4 (1 point)
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:
A. | It has a memory leak |
B. | It dereferences a null pointer |
C. | It dereferences an uninitialised pointer |
D. | It has a use-after-free error |
Question 5:
Which of the following is not a good way to improve the speed of a performance-critical piece of code?
Question 5 options:
A. | Reduce the number of memory accesses it performs |
B. | Run it on a machine with a smaller L1 cache |
C. | Rearrange the data it's using to be stored in consecutive memory locations |
D. | 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:
A. | 0 to 7 |
B. | 5 to 12 |
C. | 0 to 5 |
D. | Only 5 |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started