Sample MIE250H1 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. Conditionals and Loops · Write code · 3 marks

    Complete the method body according to its Javadoc comment.

    /**
     * Return true if and only if n is prime: an integer greater than 1
     * whose only positive divisors are 1 and itself.
     *
     * isPrime(7) returns true
     * isPrime(9) returns false
     * isPrime(2) returns true
     * isPrime(1) returns false
     */
    public static boolean isPrime(int n) {
    Show solution
    public static boolean isPrime(int n) {
        if (n < 2) {
            return false;
        }
        for (int d = 2; d < n; d++) {
            if (n % d == 0) {
                return false;
            }
        }
        return true;
    }

    The return true comes after the loop: a single divisor proves the number is not prime, but only a complete loop proves that it is. Stopping the loop at d * d <= n is also correct.

  3. Strings and Characters · Write code · 3 marks

    Complete the method according to its Javadoc comment.

    /**
     * Return the number of vowels (a, e, i, o, u) in s.
     *
     * Precondition: s contains only lowercase letters.
     *
     * countVowels("banana") returns 3
     * countVowels("rhythm") returns 0
     * countVowels("") returns 0
     */
    public static int countVowels(String s) {
    Show solution
    public static int countVowels(String s) {
        int count = 0;
        for (int i = 0; i < s.length(); i++) {
            if ("aeiou".indexOf(s.charAt(i)) >= 0) {
                count++;
            }
        }
        return count;
    }

    "aeiou".indexOf(c) is −1-1 exactly when c is not a vowel.

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

    Write the complete class Movie. It has a String title and an int number of minutes, both hidden from other classes, and it must behave as the code below shows.

    Movie m = new Movie("Dune", 155);
    System.out.println(m.isLong());     // true: more than 120 minutes
    System.out.println(m);              // Dune (155 min)
    System.out.println(new Movie("Up", 96).isLong());   // false
    Show solution
    class Movie {
        private String title;
        private int minutes;
    
        public Movie(String title, int minutes) {
            this.title = title;
            this.minutes = minutes;
        }
    
        public boolean isLong() {
            return minutes > 120;
        }
    
        @Override
        public String toString() {
            return title + " (" + minutes + " min)";
        }
    }

    println(m) calls toString(), so the class needs that method with exactly that name and header.

  5. ArrayList, HashMap and Generics · Write code · 3 marks

    Complete the method body according to its Javadoc comment.

    /**
     * Return a map from each string in words to the number of times
     * it occurs in words.
     *
     * If words is [to, be, or, to], then countWords(words) returns
     * the map {to=2, be=1, or=1} (the order of the entries does not
     * matter). If words is empty, it returns an empty map.
     */
    public static HashMap<String, Integer> countWords(
            ArrayList<String> words) {
    Show solution
    public static HashMap<String, Integer> countWords(
            ArrayList<String> words) {
        HashMap<String, Integer> counts = new HashMap<>();
        for (String w : words) {
            counts.put(w, counts.getOrDefault(w, 0) + 1);
        }
        return counts;
    }

    A new word gets the default 0, so its count becomes 1; an existing word gets its old count plus one.

About MIE250H1 practice exams

MIE250H1 practice exams

Fundamentals of Object Oriented Programming · University of Toronto (UofT, U of T, UToronto) · also written MIE250, MIE 250

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

Looking for MIE250H1 past exams?

Start with the past exams and tests your instructor or the UofT library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh MIE250H1 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
  • Arrays and Array Algorithms · 40 questions
  • Strings and Characters · 40 questions
  • Classes, Objects and References · 40 questions
  • Inheritance, Interfaces and Polymorphism · 40 questions
  • ArrayList, HashMap and Generics · 40 questions

More UofT courses

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