Pointers quiz

Answer each question to see whether you got it right and why. Your answers are saved in this browser.

0 correct · 0 / 31 answered
  1. Q1Reading and writing

    What does this print?

    int x = 10;
    int* p = &x;
    *p += 5;
    x *= 2;
    std::cout << *p;
  2. Q2Declarations

    After int* p, q;, what is the type of q?

  3. Q3Declarations

    In int* p = &x; the * dereferences p.

  4. Q4Aliasing

    What are a and b at the end?

    int a = 10, b = 20;
    int* p = &a;
    int* q = p;
    p = &b;
    *p = 99;
  5. Q5Aliasing

    Given int a = 5, b = 5; int* p = &a; int* q = &b;, what do p == q and *p == *q evaluate to?

  6. Q6Aliasing

    After line 3, what is the state (a, b, p target, q target)? (Practice sheet Q1)

    int a = 4, b = 9;
    int* p = &a;
    int* q = p;
    *q += 3;      // 1
    q = &b;       // 2
    *p = *q - 2;  // 3
  7. Q7Aliasing

    What are a and b after line 5? (Practice sheet Q1)

    int a = 4, b = 9;
    int* p = &a;
    int* q = p;
    *q += 3;      // 1
    q = &b;       // 2
    *p = *q - 2;  // 3
    p = q;        // 4
    *p += a;      // 5
  8. Q8Pointer states

    An uninitialised local pointer like int* p; is automatically nullptr.

  9. Q9Pointer states

    Checking if (p != nullptr) before dereferencing protects you from dangling pointers.

  10. Q10Pointer arithmetic

    What does this print?

    int a[] = {10, 20, 30, 40};
    int* p = a;
    ++p;
    p += 2;
    std::cout << *p;
  11. Q11Pointer arithmetic

    int a[4] starts at address 0x1000 and sizeof(int) == 4. What address does a + 3 hold?

  12. Q12Pointer arithmetic

    With int a[] = {10, 20, 30, 40}; int* first = a; int* last = a + 3;, what is last - first?

  13. Q13Pointer arithmetic

    For int a[4];, which of these is legal?

  14. Q14Pointer arithmetic

    What is *p + *q at the end? (Practice sheet Q2)

    int a[] = {3, 5, 7, 9, 11};
    int* p = a + 1;
    int* q = a + 4;
    *p = *q - *(p + 1);
    ++p;
    --q;
  15. Q15Pointer arithmetic

    Same code as Practice sheet Q2. What is q - p at the end?

    int a[] = {3, 5, 7, 9, 11};
    int* p = a + 1;
    int* q = a + 4;
    *p = *q - *(p + 1);
    ++p;
    --q;
  16. Q16Pointer arithmetic

    Which pointer-only loop sums the elements from p through q, inclusive? (Practice sheet Q2)

  17. Q17Functions

    Calling void reset(int* p) { p = nullptr; } sets the caller's pointer to nullptr.

  18. Q18Functions

    What is the final state? (Practice sheet Q3)

    void move(int* p, int* target) { *p += 3; p = target; }
    
    int x = 6, y = 10;
    int* p = &x;
    move(p, &y);
    *p += 2;
  19. Q19Functions

    The first parameter of move is changed to int*& p. What is the final state now? (Practice sheet Q3)

    void move(int*& p, int* target) { *p += 3; p = target; }
    
    int x = 6, y = 10;
    int* p = &x;
    move(p, &y);
    *p += 2;
  20. Q20Functions

    Which signature lets a function make the caller's pointer point somewhere else?

  21. Q21Functions

    What is wrong with this function?

    double* bad() {
        double s = 26.48;
        return &s;
    }
  22. Q22Functions

    findMax returns a pointer to the first maximum element. The caller runs *findMax(a, 4) = 0; on {4, 9, 2, 9}. What is the array now? (Practice sheet Q4)

  23. Q23Functions

    A findMax that copies the largest value into a local int best and returns &best lets the caller change the original array through the result. (Practice sheet Q4)

  24. Q24Functions

    What should int* findMax(int* first, int n) return when n == 0? (Practice sheet Q4)

  25. Q25Structs and classes

    p is a Student*. What does *p.gpa do?

  26. Q26Structs and classes

    void transfer(Counter* from, Counter* to, int amount) takes amount out of from->value and adds it to to->value. It does this only if both pointers are non-null, point to different objects, and the positive amount is available in from. What is items after both calls? (Practice sheet Q5)

    struct Counter { int value; };
    Counter items[] = {{3}, {7}, {5}};
    
    transfer(&items[1], &items[0], 4);   // from items[1], to items[0]
    transfer(&items[1], &items[2], 6);   // from items[1], to items[2]
  27. Q27Structs and classes

    With the rules of Practice sheet Q5, transfer(&items[0], &items[0], 2) succeeds because the amount is available.

  28. Q28Structs and classes

    What is the only difference between struct and class in C++?

  29. Q29Templates

    Calculator<T>::divide() returns a / b. What does Calculator<int>(5, 2).divide() return?

  30. Q30Templates

    Given Number<int> n(7);, the line Number<double>* q = &n; is a compile error.

  31. Q31Ownership

    Which statement about std::unique_ptr is true?