Sample COMPSCI 1026A/B questions
  1. Expressions, Types and Memory · Trace the code · 2 marks

    A transit card is used for several rides. Complete the table to show the value of each variable after each line runs.

    fare = 3.25
    rides = 4
    spent = fare * rides
    rides = rides + 1
    spent = spent + fare
    After linefareridesspent
    3         
    4         
    5         
    Show solution

    Answer: 3.25; 4; 13.0; 3.25; 5; 13.0; 3.25; 5; 16.25

    See the completed table above. 3.25 * 4 is the float 13.0, not the int 13. Changing rides on line 4 does not recompute spent.

  2. Strings · Predict the output · 2 marks

    Write the output of the code fragment. If the code raises an error, write ERROR and stop.

    filename = 'report_final_v2.pdf'
    print(filename.startswith('report'), filename.endswith('PDF'))
    print(filename.count('_'), filename.count('_', 7))
    print(filename.endswith(''), filename[filename.find('.'):])
    Show solution
    True False
    2 1
    True .pdf
  3. For and While Loops · Complete the code · 2 marks

    Fill in the blanks to complete the function. Do not change any other code.

    def every_third(s: str) -> str:
        """Return a string made of the characters of s at indices
        1, 4, 7, 10, ...
    
        >>> every_third('abcdefgh')
        'beh'
        >>> every_third('a')
        ''
        """
        result = ''
        for i in range(__________, __________, __________):
            result = result + __________
        return result
    Show solution
        for i in range(1, len(s), 3):
            result = result + s[i]
  4. Lists, Mutability and Aliasing · Write code · 2 marks

    Complete the function body according to its docstring.

    def remove_all(values: list[str], target: str) -> int:
        """Remove every occurrence of target from values and return the
        number of items removed.
    
        >>> shelf = ['atlas', 'novel', 'atlas', 'poems']
        >>> remove_all(shelf, 'atlas')
        2
        >>> shelf
        ['novel', 'poems']
        """
    Show solution
        count = 0
        while target in values:
            values.remove(target)
            count = count + 1
        return count

    Also correct: count = values.count(target) followed by a loop that calls remove count times.

  5. Classes and Objects · Predict the output · 2 marks

    Write the output of the code below. If the code raises an error, write ERROR and stop.

    class Song:
        """A song with a title, an artist and a length in seconds."""
    
        def __init__(self, title: str, artist: str, seconds: int) -> None:
            self.title = title
            self.artist = artist
            self.seconds = seconds
    
        def __str__(self) -> str:
            mins = str(self.seconds // 60)
            secs = str(self.seconds % 60)
            return self.title + ' by ' + self.artist + ' ' + mins + ':' + secs
    
    track = Song('Rain', 'Ola', 185)
    print(track)
    print(track.title.upper())
    other = track
    other.seconds = 245
    print(track)
    Show solution
    Rain by Ola 3:5
    RAIN
    Rain by Ola 4:5

    other and track refer to the same object. Note 5 is not padded to 05.

About COMPSCI 1026A/B practice exams

COMPSCI 1026A/B practice exams

Computer Science Fundamentals I · Western University (Western, Western U, UWO) · also written COMPSCI1026, COMPSCI 1026

Make a free, printable COMPSCI 1026A/B 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 800 original questions and skips the ones you have already seen.

Looking for COMPSCI 1026A/B past exams?

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

  • Expressions, Types and Memory · 100 questions
  • Strings · 100 questions
  • Booleans and Conditionals · 100 questions
  • For and While Loops · 100 questions
  • Functions and the Design Recipe · 100 questions
  • Lists, Mutability and Aliasing · 100 questions
  • Tuples and Dictionaries · 100 questions
  • Classes and Objects · 100 questions

More Western courses

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