Embercode
✕
Pointers and Memory AddressesQuick quiz

Diagrams and aliasing

8 questions. Covers Aliasing.

Questions 1 to 5 of 8

  1. 1. What does this program print? ``cpp #include <iostream> struct Node { int value; Node* next; }; void print_list(const Node* head) { for (const Node* cur = head; cur != nullptr; cur = cur->next) { std::cout << cur->value << " "; } std::cout << "\n"; } int main() { Node c{3, nullptr}; Node b{2, &c}; Node a{1, &b}; Node* head = &a; Node* first = head; Node* second = head->next; first->next = second->next; second->next = first; head = second; print_list(head); return 0; } ``
  2. 2. In a pointer diagram, which arrow does the statement a.next = b.next; move?
  3. 3. What does this program print? Write the two output lines separated by / . ``cpp #include <iostream> #include <vector> int main() { std::vector<int> prices{10, 20, 30}; for (auto p : prices) p += 1; std::cout << prices[0] << ' '; for (auto& p : prices) p += 1; std::cout << prices[0] << ' ' << prices[2] << '\n'; std::vector<int> copy = prices; copy[1] = 0; std::cout << prices[1] << ' ' << copy[1] << '\n'; return 0; } ``
  4. 4. In the declaration std::vector<int>& alias = original;, which symbol makes alias a second name for original instead of a copy? Type the symbol.

    Type your answer.

  5. 5. What does this program print? ``cpp #include <iostream> int main() { int x = 10; int& r = x; int* p = &x; int y = r; r = 20; *p += 5; std::cout << x << ' ' << y << ' ' << r << '\n'; return 0; } ``

5 question(s) left on this page.

Typed answers ignore extra spaces and, for most questions, capital letters.