Sample C S 314 questions
  1. Interfaces, Generics and Dynamic Dispatch · Multiple choice · 1 mark

    The class below defines equals using only the field id.

    class Item {
        final int id;
        final String label;
        Item(int id, String label) { this.id = id; this.label = label; }
    
        @Override
        public boolean equals(Object o) {
            return o instanceof Item i && id == i.id;
        }
        // hashCode() goes here
    }

    Which implementation of hashCode violates the contract between equals and hashCode?

    1. return Objects.hash(id, label);

    2. return (id * 31) % 1024;

    3. return 17;

    4. return Integer.hashCode(id) + 1;

    Show solution

    Answer: (A)

    (A). The contract says that objects that are equal must have equal hash codes. Here two items with the same id and different labels are equal, but Objects.hash(id, label) gives them different hash codes. The other three depend only on id (a constant such as 17 is legal, only slow), so equal objects always agree.

  2. ArrayLists, Linked Lists and Deques · Multiple choice · 1 mark

    What does the following code print?

    List<StringBuilder> a = new ArrayList<>();
    a.add(new StringBuilder("x"));
    a.add(new StringBuilder("y"));
    List<StringBuilder> b = new ArrayList<>(a);
    b.get(0).append("!");
    b.add(new StringBuilder("z"));
    b.set(1, new StringBuilder("w"));
    System.out.println(a);
    1. [x, y]

    2. [x!, w]

    3. [x!, y]

    4. [x!, w, z]

    Show solution

    Answer: (C)

    (C). The copy constructor makes a new list with its own slots, but the slots hold the same StringBuilder objects as a (a shallow copy). b.get(0).append("!") changes the shared object, so a shows x!. Adding to b and replacing b’s slot 1 change only b’s own slots, so a still has two elements and keeps y.

  3. Recursion and Backtracking · Write code · 2 marks

    Complete the method below using recursion and no loop.

    /** Return the largest of a[i], a[i + 1], ..., a[a.length - 1].
     *  Precondition: 0 <= i < a.length.
     *  Example: for a = {3, 9, 4}, maxFrom(a, 0) returns 9
     *  and maxFrom(a, 2) returns 4. */
    static int maxFrom(int[] a, int i) {
    Show solution
    static int maxFrom(int[] a, int i) {
        if (i == a.length - 1) {
            return a[i];
        }
        return Math.max(a[i], maxFrom(a, i + 1));
    }

    The base case is the last element, because the precondition rules out an empty range. Otherwise the answer is the larger of a[i] and the maximum of the rest.

  4. Hash Tables, hashCode and equals · Predict the output · 2 marks

    String.hashCode() computes s0⋅31n−1+s1⋅31n−2+⋯+sn−1s_0 \cdot 31^{n-1} + s_1 \cdot 31^{n-2} + \cdots + s_{n-1} from the character codes sis_i of a string of length nn; the empty string gives 0. The codes are 'A' =65= 65, 'B' =66= 66, 'a' =97= 97 and 'b' =98= 98. What does the following code print? Write each line of output on its own line.

    System.out.println("ab".hashCode());
    System.out.println("Aa".hashCode() == "BB".hashCode());
    System.out.println("Aa".equals("BB"));
    System.out.println("".hashCode());
    Show solution
    3105
    true
    false
    0

    "ab" gives 97⋅31+98=310597 \cdot 31 + 98 = 3105. "Aa" gives 65⋅31+97=211265 \cdot 31 + 97 = 2112 and "BB" gives 66⋅31+66=211266 \cdot 31 + 66 = 2112, so the hash codes collide although the strings differ.

  5. Graphs and Graph Algorithms · Short answer · 2 marks

    A maze is an R×CR \times C grid in which every cell is open. Each cell is a vertex, and two cells are joined by an (undirected) edge when they share a side. Breadth-first search from the top-left cell visits the cells without building any adjacency list: it computes the neighbours of a cell from its row and column.

    The number of vertices VV   
    The number of edges EE   
    Running time of the search, as Θ\Theta of RR and CC   
    Show solution

    Answer: RCRC; R(C−1)+(R−1)CR(C-1) + (R-1)C; Θ(RC)\Theta(RC)

    Each of the RR rows has C−1C - 1 horizontal edges and each of the CC columns has R−1R - 1 vertical edges. Every cell has at most four neighbours, so E≤2VE \le 2V and the running time Θ(V+E)\Theta(V + E) is just Θ(RC)\Theta(RC): the search does a constant amount of work per cell.

About C S 314 practice exams

C S 314 practice exams

Data Structures · The University of Texas at Austin (UT Austin, UT, UTexas) · also written CS314

Make a free, printable C S 314 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 C S 314 past exams?

Start with the past exams and tests your instructor or the UT Austin library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh C S 314 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

  • Interfaces, Generics and Dynamic Dispatch · 50 questions
  • Asymptotic Analysis of Java Code · 50 questions
  • ArrayLists, Linked Lists and Deques · 50 questions
  • Stacks, Queues and Their Uses · 50 questions
  • Recursion and Backtracking · 50 questions
  • Binary Trees and Binary Search Trees · 50 questions
  • Hash Tables, hashCode and equals · 50 questions
  • Priority Queues and Heaps · 50 questions
  • Sorting Algorithms · 50 questions
  • Graphs and Graph Algorithms · 50 questions

More UT Austin courses

  • M 408C Differential and Integral Calculus
  • M 408K Differential Calculus
  • M 408L Integral Calculus
  • M 408M Multivariable Calculus
  • M 340L Matrices and Matrix Calculations
  • M 362K Probability I
  • M 378K Introduction to Mathematical Statistics
  • SDS 301 Elementary Statistical Methods
  • SDS 302F Foundations of Data Analysis
  • C S 303E Elements of Computers and Programming
  • C S 313E Elements of Software Design
  • CH 301 Principles of Chemistry I
  • PHY 301 Mechanics
  • PHY 302K General Physics Technical Course: Mechanics, Heat, and Sound
  • BIO 311C Introductory Biology I
  • PSY 301 Introduction to Psychology
  • ECO 304K Introduction to Microeconomics
  • ECO 304L Introduction to Macroeconomics
  • ACC 311 Fundamentals of Financial Accounting
  • ACC 312 Fundamentals of Managerial Accounting
  • C S 312 Introduction to Programming
  • ECE 312 Software Design and Implementation I
  • M 427L Advanced Calculus for Applications II
  • PHY 303K Engineering Physics I
  • SDS 321 Introduction to Probability and Statistics
  • C E 311S Probability and Statistics for Civil Engineers
  • M E 335 Engineering Statistics
  • BME 335 Engineering Probability and Statistics

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