Sample COM SCI 32 questions
  1. Classes, ADTs and Operator Overloading · Write code · 3 marks

    // EFFECTS: returns the number of days in month m (1..12) of year y,
    //          taking leap years into account
    int daysIn(int y, int m);
    
    class Date {
    public:
        Date(int y, int m, int d);   // REQUIRES: a valid date
        void advance();              // moves this date to the next day
        int year() const;
        int month() const;
        int day() const;
    private:
        int m_y;
        int m_m;
        int m_d;                     // always a valid date
    };

    Write Date::advance. For example, 28 February 2024 becomes 29 February 2024, and 31 December 2025 becomes 1 January 2026.

    Show solution
    void Date::advance() {
        m_d++;
        if (m_d > daysIn(m_y, m_m)) {
            m_d = 1;
            m_m++;
            if (m_m > 12) {
                m_m = 1;
                m_y++;
            }
        }
    }

    Increase the day; only when it passes the end of the month does the month roll over, and only when the month passes December does the year roll over. (1 mark each for the day step, the month rollover and the year rollover.)

  2. Inheritance and Virtual Functions · Predict the output · 2 marks

    What does this program print? Assume <iostream> is included and using namespace std;.

    class Base {
    public:
        void who() const { cout << "Base "; }
        virtual void what() const { cout << "Base\n"; }
    };
    class Derived : public Base {
    public:
        void who() const { cout << "Derived "; }
        void what() const override { cout << "Derived\n"; }
    };
    int main() {
        Derived d;
        Base* b = &d;
        Derived* p = &d;
        b->who();
        b->what();
        p->who();
        p->what();
    }
    Show solution
    Base Derived
    Derived Derived

    who is not virtual, so the call is decided by the pointer’s static type: b->who() runs Base::who and p->who() runs Derived::who (which merely hides the base version). what is virtual, so both calls run Derived::what.

  3. Linked Lists · Write code · 2 marks

    struct Node {
        int value;
        Node* next;
    };

    Write pushBack. It adds a new node holding v after the last node of the list and returns the head of the resulting list. The list may be empty.

    // EFFECTS: appends a new node with value v; returns the head of the list
    Node* pushBack(Node* head, int v) {
    Show solution
    Node* pushBack(Node* head, int v) {
        Node* n = new Node{v, nullptr};
        if (head == nullptr)
            return n;
        Node* p = head;
        while (p->next != nullptr)
            p = p->next;
        p->next = n;
        return head;
    }

    An empty list is a special case because the new node becomes the head, which is why the function returns the head. Otherwise we stop at the last node (the one whose next is nullptr), not after it. (1 mark new node, 1 mark empty case, 1 mark walk and attach.)

  4. Recursion · Predict the output · 2 marks

    What does this program print? Assume <iostream> is included and using namespace std;.

    int gcd(int a, int b) {
        cout << a << " " << b << "\n";
        if (b == 0) return a;
        return gcd(b, a % b);
    }
    
    int main() {
        cout << gcd(48, 18) << "\n";
    }
    Show solution
    48 18
    18 12
    12 6
    6 0
    6

    Each call prints its arguments first. The last call returns a = 6, which is passed back unchanged through the earlier calls and printed by main.

  5. Big-O and Sorting · Write code · 3 marks

    Write mergeSorted. It must make a single pass over both inputs (O(∣a∣+∣b∣)O(|a| + |b|) time) and must not sort anything.

    // REQUIRES: a and b are each sorted in increasing order
    // EFFECTS:  returns a sorted vector holding all elements of a and b;
    //           equal elements from a come before equal elements from b
    vector<int> mergeSorted(const vector<int>& a, const vector<int>& b) {
    Show solution
    vector<int> mergeSorted(const vector<int>& a, const vector<int>& b) {
        vector<int> out;
        int i = 0;
        int j = 0;
        int n = a.size();
        int m = b.size();
        while (i < n && j < m) {
            if (b[j] < a[i]) out.push_back(b[j++]);
            else out.push_back(a[i++]);
        }
        while (i < n) out.push_back(a[i++]);
        while (j < m) out.push_back(b[j++]);
        return out;
    }

    Repeatedly take the smaller front element. Taking from b only when b[j] < a[i] (strictly) puts the element of a first on a tie. When one input runs out, the rest of the other is copied. (1 mark main loop, 1 mark tie-breaking, 1 mark leftovers of both inputs.)

About COM SCI 32 practice exams

COM SCI 32 practice exams

Introduction to Computer Science II · University of California, Los Angeles (UCLA, UC Los Angeles) · also written COMSCI32

Make a free, printable COM SCI 32 practice exam: choose the topics, the length and the difficulty above, and get an exam-style PDF with a full answer key. Each paper draws on 500 original questions and skips the ones you have already seen.

Looking for COM SCI 32 past exams?

Start with the past exams and tests your instructor or the UCLA library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh COM SCI 32 practice exams here on the same topics, with full answer keys, as many as you need. The questions here are original, not copies of past exams.

Topics covered

  • Classes, ADTs and Operator Overloading · 50 questions
  • Dynamic Memory, the Big Three and RAII · 50 questions
  • Inheritance and Virtual Functions · 50 questions
  • Templates, the STL and Iterators · 50 questions
  • Linked Lists · 50 questions
  • Stacks and Queues · 50 questions
  • Recursion · 50 questions
  • Binary Trees and Binary Search Trees · 50 questions
  • Hash Tables · 50 questions
  • Big-O and Sorting · 50 questions

More UCLA courses

questionbank.dev is independent and not affiliated with University of California, Los Angeles. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.