Sample EECS 1022 questions
  1. Types, Variables and Expressions · Trace the code · 2 marks

    Fill in the value of each variable after the given line has executed. A variable that has not been declared yet is shown as –.

    int x = 4;
    int y = 9;
    int t = x;
    x = y;
    y = t + y;
    x = x - y;
    After linexyt
    249–
    3         
    4         
    5         
    6         
    Show solution

    Answer: 4; 9; 4; 9; 9; 4; 9; 13; 4; −4-4; 13; 4

    Line 4 overwrites x with 9; the old 4 survives only in t. Line 5 computes t + y = 4 + 9 = 13 (not x + y), and line 6 computes 9 - 13 = -4. The variable t keeps the value 4 throughout.

  2. Conditionals and Loops · Write code · 2 marks

    Complete the method body according to its Javadoc comment.

    /**
     * Return the number of positive integers that divide n evenly.
     *
     * Precondition: n >= 1
     *
     * countDivisors(12) returns 6
     * countDivisors(7) returns 2
     * countDivisors(1) returns 1
     */
    public static int countDivisors(int n) {
    Show solution
    public static int countDivisors(int n) {
        int count = 0;
        for (int d = 1; d <= n; d++) {
            if (n % d == 0) {
                count++;
            }
        }
        return count;
    }
  3. Static Methods, Parameters and Scope · Write code · 3 marks

    The method printStars is already written. Using it, complete the method printTriangle according to its Javadoc comment. Do not use System.out in printTriangle.

    /** Print n stars on one line, followed by a newline. */
    public static void printStars(int n) {
        for (int i = 0; i < n; i++) {
            System.out.print("*");
        }
        System.out.println();
    }
    
    /**
     * Print n lines of stars: line k (counting from 1) has k stars.
     *
     * printTriangle(1) prints the line *
     * printTriangle(3) prints the lines *, **, ***
     * printTriangle(0) prints nothing
     */
    public static void printTriangle(int n) {
    Show solution
    public static void printTriangle(int n) {
        for (int k = 1; k <= n; k++) {
            printStars(k);
        }
    }
  4. Strings and Characters · Predict the output · 2 marks

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

    String a = "apple";
    System.out.println(a.compareTo("apricot") < 0);
    System.out.println(a.compareTo("app") > 0);
    System.out.println("Zebra".compareTo(a) < 0);
    System.out.println("b".compareTo("abc") < 0);
    System.out.println("10".compareTo("9") < 0);
    System.out.println(a.compareTo("apple") == 0);
    Show solution
    true
    true
    true
    false
    true
    true

    compareTo compares character by character: p comes before r; a longer string with the same beginning is larger; every upper-case letter comes before every lower-case letter, so "Zebra" is smaller than "apple"; "b" is larger than "abc" because b is larger than a; and "10" is smaller than "9" because 1 is smaller than 9.

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

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

    /**
     * Return a new array that holds, in the same order, exactly those
     * students of roster whose gpa is at least cutoff. The result has
     * length 0 if there are none. Do not change roster.
     *
     * Precondition: no element of roster is null.
     *
     * If the gpas of a, b, c are 3.5, 2.0, 3.0, then
     * honorRoll(new Student[] {a, b, c}, 3.0) returns {a, c}
     */
    public static Student[] honorRoll(Student[] roster, double cutoff) {
    Show solution
    public static Student[] honorRoll(Student[] roster, double cutoff) {
        int count = 0;
        for (int i = 0; i < roster.length; i++) {
            if (roster[i].getGpa() >= cutoff) {
                count++;
            }
        }
        Student[] result = new Student[count];
        int next = 0;
        for (int i = 0; i < roster.length; i++) {
            if (roster[i].getGpa() >= cutoff) {
                result[next] = roster[i];
                next++;
            }
        }
        return result;
    }

    An array’s length is fixed when it is created, so the students are counted first. The new array holds the same Student objects as roster, not copies; next is a separate index for the result.

About EECS 1022 practice exams

EECS 1022 practice exams

Introduction to Object Oriented Programming · York University (York, YorkU, York U) · also written EECS1022

Make a free, printable EECS 1022 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 240 original questions and skips the ones you have already seen.

Looking for EECS 1022 past exams?

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

More York courses

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