Object Oriented Data Types quiz

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

0 correct · 0 / 42 answered
  1. Q1ADTs and algorithms

    Which statement describes an algorithm?

  2. Q2ADTs and algorithms

    An ADT specifies exactly how its data is stored in memory.

  3. Q3ADTs and algorithms

    Which is an ADT operation?

  4. Q4ADTs and algorithms

    Start with an empty stack and run push(3), push(7), pop(). What value does pop() return?

  5. Q5ADTs and algorithms

    Which structure removes items in first-in, first-out order?

  6. Q6Find the maximum

    What is the final value of maximum?

    int a[] = {3, 1, 6, 4};
    int maximum = a[0];
    for (int i = 1; i < 4; ++i) {
        if (a[i] > maximum)
            maximum = a[i];
    }
    std::cout << maximum;
  7. Q7Programming process

    A program compiles but prints 11 instead of 10. What kind of error is this?

  8. Q8Static vs dynamic memory

    Which expression allocates an int during execution?

  9. Q9Static vs dynamic memory

    The size of int scores[5]; can be changed while the program runs.

  10. Q10Static arrays

    With int a[3] = {7, 4, 9};, what is a[1]?

  11. Q11Static arrays

    What does this print?

    int a[4] = {2, 3, 5, 7};
    std::cout << a[0] + a[3];
  12. Q12Static arrays

    What does this print?

    int a[4] = {5, 8, 3, 1};
    a[2] = a[0] + a[1];
    std::cout << a[2];
  13. Q13Arrays and functions

    Inside int f(const int a[], int n), what does const forbid?

  14. Q14Arrays and functions

    A function receiving int a[] can tell how many elements the caller's array has.

  15. Q152D arrays

    What does this print?

    int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
    std::cout << a[1][0];
  16. Q162D arrays

    Which is a valid parameter for int a[2][3]?

  17. Q17Addresses and pointers

    With int x = 5;, what does &x mean?

  18. Q18Addresses and pointers

    What does this print?

    int a = 4;
    int* p = &a;
    *p = *p + 3;
    std::cout << a;
  19. Q19Addresses and pointers

    In *p = 20; the * means "pointer to".

  20. Q20The heap

    Which memory must eventually be released by you?

  21. Q21The heap

    What does this print?

    int* p = new int(8);
    std::cout << *p;
    delete p;
    p = nullptr;
  22. Q22The heap

    After delete p; (and no reassignment), is std::cout << *p; valid?

  23. Q23The heap

    After moving the cout before delete z, what does this print?

    int* h = new int(9);
    int* z = new int(5);
    *z = *h + *z;
    std::cout << *z;
    delete z;
    delete h;
  24. Q24Dynamic arrays

    Which statement correctly releases int* p = new int[3];?

  25. Q25Dynamic arrays

    Which allocates n integers at runtime?

  26. Q26Dynamic arrays

    What does this print?

    int* a = new int[3]{1, 2, 3};
    int sum = 0;
    for (int i = 0; i < 3; ++i)
        sum += a[i];
    std::cout << sum;
    delete[] a;
  27. Q27Resizing

    Which step must come before delete[] of the old array when resizing?

  28. Q28Resizing

    void extendArray(int* a, int size) (no references) can resize the caller's array and update the caller's pointer.

  29. Q29Resizing

    After int* middle = a + 2; and then a resize that deletes the old block and repoints a, what is *middle? (Practice sheet Q8)

  30. Q30Resizing

    What does this print? (Practice sheet Q8)

    int* a = new int[4]{2, 4, 6, 8};
    int* middle = a + 2;
    int* smaller = new int[3];
    for (int i = 0; i < 3; ++i) smaller[i] = a[i];
    delete[] a;
    a = smaller;
    middle = a + 2;
    std::cout << *middle;
    delete[] a;
  31. Q31Resizing

    What is wrong with this loop when copying into int* smaller = new int[3];? (Practice sheet Q8)

    for (int i = 0; i <= 3; ++i)
        smaller[i] = a[i];
  32. Q32Resizing

    With capacity == 0, what capacity does this give? (Practice sheet Q7)

    int next = capacity == 0 ? 1 : capacity * 2;
    std::cout << next;
  33. Q33Resizing

    A full array {8, 1, 6} (size 3, capacity 3) is grown with doubling, then 4 is appended. What are the size, capacity and elements? (Practice sheet Q7)

  34. Q34Classes

    If scores and count in ScoreLog are private, code outside the class can still modify count directly.

  35. Q35Classes

    Using this ScoreLog (capacity 4, starts empty), add(6), add(3), add(8), add(5), add(9) are called. How many of these calls return true? (Practice sheet Q9)

    class ScoreLog {
        int scores[4];
        int count = 0;
    public:
        bool add(int score) {
            if (count == 4) return false;
            scores[count++] = score;
            return true;
        }
        bool removeLast(int& removed) {
            if (count == 0) return false;
            removed = scores[--count];
            return true;
        }
    };
  36. Q36Classes

    Same ScoreLog as Q35. The log is [6, 3, 8, 5]. You call removeLast(r) twice, then add(7). What does it hold, and what was the last r before the add? (Practice sheet Q9)

  37. Q37Classes

    Same ScoreLog as Q35. If removeLast(r) is called on an empty log, it returns false and r keeps its old value. (Practice sheet Q9)

  38. Q38Templates

    What does this print? (Practice sheet Q10)

    Pair<int> p(8, 3);
    p.swapValues();
    std::cout << p.larger();
  39. Q39Templates

    In this Pair<T>, which value does larger() return when first == second? (Practice sheet Q10)

    template <typename T>
    class Pair {
        T first, second;
    public:
        Pair(T a, T b) : first(a), second(b) {}
        T larger() const { return first < second ? second : first; }
        void swapValues() {
            T saved = first;
            first = second;
            second = saved;
        }
    };
  40. Q40Templates

    Same Pair<T> as Q39. void swapValues() could be declared const because it only uses member variables. (Practice sheet Q10)

  41. Q412D arrays

    What does this print? (bestRow keeps the first row on ties.) (Practice sheet Q6)

    int bestRow(const int a[][3], int rows) {
        int best = -1;
        long long bestSum = 0;
        for (int i = 0; i < rows; ++i) {
            long long sum = static_cast<long long>(a[i][0]) + a[i][1] + a[i][2];
            if (best == -1 || sum > bestSum) { best = i; bestSum = sum; }
        }
        return best;
    }
    
    int a[3][3] = {{-2, 4, 1}, {5, -4, 2}, {0, 1, 1}};
    std::cout << bestRow(a, 3);
  42. Q422D arrays

    Same bestRow as Q41. What does bestRow(a, 0) return? (Practice sheet Q6)