Sample C S 312 questions
  1. Types, Variables and Expressions · Write code · 2 marks

    The variable n holds a three-digit number (precondition: 100 ≤\le n ≤\le 999). Declare an int variable reversed holding the number with its digits in the opposite order. For example, if n is 482 then reversed is 284, and if n is 120 then reversed is 21. Do not use strings or loops.

    int n = 482;
    Show solution
    int ones = n % 10;
    int tens = n / 10 % 10;
    int hundreds = n / 100;
    int reversed = ones * 100 + tens * 10 + hundreds;

    For 120 the pieces are 0, 2 and 1, giving 0 + 20 + 1 = 21.

  2. Static Methods, Parameters and Scope · Short answer · 2 marks

    Write only the header (the first line, ending in {) of a public static method for each description. Do not write a body. You choose the names.

    1. Takes a person’s age, a whole number, and tells whether it is from 13 to 19 inclusive.

    2. Takes an array of whole numbers and gives back their mean, which may have a fractional part.

    3. Takes a String and a number of repetitions and prints the string that many times; it gives nothing back.

    Show solution
    public static boolean isTeen(int age) {
    public static double average(int[] nums) {
    public static void printBanner(String text, int times) {

    The return type comes from what the caller gets back: boolean for a yes/no answer, double for a mean, void when the method only prints. Each parameter needs its own type, including the array type int[]; the names may differ.

  3. Arrays and Array Algorithms · Predict the output · 2 marks

    Write the output of the code fragment below. If it does not compile, write COMPILE ERROR; if it throws an exception, write what is printed first, then the exception’s name.

    int[] a = new int[5];
    for (int i = 0; i < a.length; i++) {
        a[i] = i * i;
    }
    a[a.length - 1] = a[1] + a[2];
    System.out.println(Arrays.toString(a));
    int total = 0;
    for (int x : a) {
        total += x;
    }
    System.out.println(total);
    System.out.println(a.length);
    Show solution
    [0, 1, 4, 9, 5]
    19
    5

    The first loop stores 0, 1, 4, 9, 16; the assignment then replaces the last element (16) by a[1] + a[2], which is 5. The sum is 0 + 1 + 4 + 9 + 5 = 19, and the length does not change.

  4. Inheritance, Interfaces and Polymorphism · Multiple choice · 2 marks

    Consider these declarations.

    interface Flyer { void fly(); }
    interface Swimmer { void swim(); }
    class Duck implements Flyer, Swimmer {
        public void fly() { }
        public void swim() { }
    }
    
    Flyer f = new Duck();

    Select all that apply. Circle ALL of the following statements that compile when placed after the code above.

    1. Object o = f;

    2. Duck d = (Duck) f;

    3. Swimmer s = f;

    4. Flyer g = new Flyer();

    5. Swimmer s = (Swimmer) f;

    Show solution

    Answer: (A, B, E)

    Every object is an Object, so the first line is an upcast. A cast from an interface type to a class that implements it ((Duck) f) or to another interface ((Swimmer) f) is accepted by the compiler, which cannot rule out that the object is a Duck; at run time it succeeds because the object is one. Without a cast, Swimmer s = f; fails: the declared type Flyer is not a Swimmer. An interface can never be instantiated with new.

  5. Recursion, Searching and Sorting · Multiple choice · 2 marks

    A pass of bubbleSort is one iteration of its outer for loop. The method swap(a, i, j) exchanges a[i] and a[j].

    public static void bubbleSort(int[] a) {
        for (int end = a.length - 1; end > 0; end--) {
            for (int i = 0; i < end; i++) {
                if (a[i] > a[i + 1]) {
                    swap(a, i, i + 1);
                }
            }
        }
    }

    The method is called on the array {6, 3, 5, 1, 4}. What does the array contain after exactly two passes?

    1. {3, 5, 1, 4, 6}

    2. {3, 5, 6, 1, 4}

    3. {1, 3, 5, 6, 4}

    4. {3, 1, 4, 5, 6}

    Show solution

    Answer: (D)

    (D) Pass 1 carries the 6 to the end: {3, 5, 1, 4, 6}. Pass 2 (which stops before the last cell) carries the 5 up to just before the 6: {3, 1, 4, 5, 6}. The first choice is the array after only one pass; the second is what insertion sort gives after two passes, and the third is what selection sort gives after two passes.

About C S 312 practice exams

C S 312 practice exams

Introduction to Programming · The University of Texas at Austin (UT Austin, UT, UTexas) · also written CS312

Make a free, printable C S 312 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 400 original questions and skips the ones you have already seen.

Looking for C S 312 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 312 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

  • Types, Variables and Expressions · 40 questions
  • Conditionals and Loops · 40 questions
  • Static Methods, Parameters and Scope · 40 questions
  • Strings and Characters · 40 questions
  • Arrays and Array Algorithms · 40 questions
  • Classes, Objects and References · 40 questions
  • Inheritance, Interfaces and Polymorphism · 40 questions
  • Exceptions and File Input · 40 questions
  • ArrayList, HashMap and Generics · 40 questions
  • Recursion, Searching and Sorting · 40 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 314 Data Structures
  • 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.