Sample CS 124 questions
  1. Types, Variables and Expressions · Predict the output · 2 marks

    Write the output of the code fragment below.

    int n = 4739;
    System.out.println(n / 1000);
    System.out.println(n % 10);
    System.out.println(n / 10 % 10);
    System.out.println(n / 100 % 10);
    System.out.println(n / 10000);
    Show solution
    4
    9
    3
    7
    0

    n / 10 % 10 is evaluated left to right: 473 divides 4739 by 10000 using integer division, which leaves 0.

  2. Static Methods, Parameters and Scope · Predict the output · 3 marks

    Write the output of the program below.

    public class Main {
        public static int calc(int x, int y) {
            int z = x - y;
            x = z * 2;
            return x + y;
        }
    
        public static void main(String[] args) {
            int x = 10;
            int y = 4;
            int z = calc(y, x);
            System.out.println(x + " " + y + " " + z);
            z = calc(x, y);
            System.out.println(x + " " + y + " " + z);
            {
                int w = 3;
                w += z;
                System.out.println(w);
            }
            int w = 50;
            System.out.println(w);
        }
    }
    Show solution
    10 4 -2
    10 4 16
    19
    50

    The names x, y and z inside calc are separate variables from those in main; the first call is calc(4, 10), so z is −6-6, x becomes −12-12 and the result is −12+10=−2-12 + 10 = -2. The second call is calc(10, 4): z is 6, x becomes 12, result 16. The inner w ends at the closing brace, so declaring a new w afterwards is legal.

  3. Arrays and Array Algorithms · Write code · 2 marks

    Complete the method body according to its Javadoc comment.

    /**
     * Return the average of the values in nums as a double.
     * Precondition: nums.length > 0
     *
     * average(new int[] {1, 2}) returns 1.5
     * average(new int[] {4, 6, 8}) returns 6.0
     * average(new int[] {7}) returns 7.0
     */
    public static double average(int[] nums) {
    Show solution
    public static double average(int[] nums) {
        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
        }
        return (double) sum / nums.length;
    }

    The cast must apply to sum before the division: dividing two ints would throw away the fraction ((1 + 2) / 2 is 1).

  4. Classes, Objects and References · Write code · 3 marks

    The class Student has a method double getGpa(). Complete the method according to its Javadoc comment.

    /**
     * Return the average gpa of the students in roster, ignoring
     * any element that is null. Return 0.0 if roster has no
     * student at all.
     *
     * If s and t have gpas 3.0 and 4.0, then
     * averageGpa(new Student[] {s, null, t}) returns 3.5
     * averageGpa(new Student[] {null, null}) returns 0.0
     */
    public static double averageGpa(Student[] roster) {
    Show solution
    public static double averageGpa(Student[] roster) {
        double total = 0.0;
        int count = 0;
        for (int i = 0; i < roster.length; i++) {
            if (roster[i] != null) {
                total += roster[i].getGpa();
                count++;
            }
        }
        if (count == 0) {
            return 0.0;
        }
        return total / count;
    }

    The null test must come before the method call, or the call throws a NullPointerException. The average divides by the number of students actually counted, not by roster.length, and the empty case is handled separately to avoid dividing by zero.

  5. Recursion, Searching and Sorting · Write code · 3 marks

    Complete the method according to its Javadoc comment. The method must be recursive: do not use loops.

    /**
     * Return true if and only if target appears among nums[i], nums[i + 1],
     * ..., nums[nums.length - 1].
     *
     * Precondition: i >= 0
     *
     * contains(new int[] {4, 8, 15}, 0, 8) returns true
     * contains(new int[] {4, 8, 15}, 2, 8) returns false
     * contains(new int[] {4, 8, 15}, 3, 4) returns false
     * contains(new int[] {}, 0, 4) returns false
     */
    public static boolean contains(int[] nums, int i, int target) {
    Show solution
    public static boolean contains(int[] nums, int i, int target) {
        if (i >= nums.length) {
            return false;
        }
        if (nums[i] == target) {
            return true;
        }
        return contains(nums, i + 1, target);
    }

    When i has reached the end of the array there is nothing left to look at, so the answer is false. Otherwise either nums[i] is the target or the answer is whatever the search of the rest of the array gives.

About CS 124 practice exams

CS 124 practice exams

Introduction to Computer Science I · University of Illinois Urbana-Champaign (UIUC, Illinois, U of I) · also written CS124

Make a free, printable CS 124 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 CS 124 past exams?

Start with the past exams and tests your instructor or the UIUC library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh CS 124 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
  • ArrayList, HashMap and Generics · 40 questions
  • Classes, Objects and References · 40 questions
  • Inheritance, Interfaces and Polymorphism · 40 questions
  • Exceptions and File Input · 40 questions
  • Recursion, Searching and Sorting · 40 questions

More UIUC courses

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