Sample COM SCI 30 questions
  1. Expressions, Types and Memory · Short answer · 2 marks

    Variable age refers to the int 18. The statement print('Age: ' + age) raises an error.

    1. In one sentence, explain why. [1]

    2. Rewrite the statement so that it prints Age: 18 and still uses the + operator. [1]

    Show solution

    (a) + cannot combine a str with an int (TypeError); Python does not convert age to a string automatically.
    (b) print('Age: ' + str(age))

  2. Functions and the Design Recipe · Short answer · 2 marks

    Consider the function below.

    def mystery(n: int) -> int:
        """Precondition: 10 <= n <= 99
        """
        return n % 10 * 10 + n // 10
    1. What does mystery(52) return? [1]

    2. Give an argument for which mystery returns 9. [1]

    Show solution

    (a) 25 (b) 90, since 0 * 10 + 9 is 9. (9 itself violates the precondition.)

  3. Strings · Predict the output · 3 marks

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

    letters = 'abcdefghij'
    print(letters[::2])
    print(letters[1::3])
    print(letters[::-1])
    print(letters[8:2:-2])
    print(letters[2:8:-1] == '')
    print(letters[-2::-4])
    Show solution
    acegi
    beh
    jihgfedcba
    ige
    True
    iea

    With a negative step the start must be to the right of the stop; otherwise the slice is empty.

  4. For and While Loops · Predict the output · 2 marks

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

    1. a = 'abc'
      b = '12345'
      i = 0
      result = ''
      while i < len(a) or i < len(b):
          if i < len(a):
              result = result + a[i]
          if i < len(b):
              result = result + b[i]
          i = i + 1
      print(result, i)

      [1]

    2. The same code, with or in the loop condition replaced by and. [1]

    Show solution

    (a)

    a1b2c345 5

    (b)

    a1b2c3 3

    With or the loop continues until both strings are used up; with and it stops as soon as the shorter one is.

  5. Reading and Writing Files · Write code · 3 marks

    Complete the function body according to its docstring.

    def split_by_grade(grade_name: str, pass_name: str,
                       fail_name: str) -> None:
        """Copy each line of the file called grade_name to the file
        called pass_name if its grade is at least 50, and to the file
        called fail_name otherwise, keeping the lines in their original
        order. Any existing contents of pass_name and fail_name are
        replaced.
    
        Precondition: each line of grade_name has the form
        student_number,grade and ends with a newline.
        """
    Show solution
        grade_file = open(grade_name)
        pass_file = open(pass_name, 'w')
        fail_file = open(fail_name, 'w')
        for line in grade_file:
            grade = int(line.strip().split(',')[1])
            if grade >= 50:
                pass_file.write(line)
            else:
                fail_file.write(line)
        grade_file.close()
        pass_file.close()
        fail_file.close()

    Both output files must be opened once, before the loop; opening them inside the loop with 'w' keeps only the last line written to each.

About COM SCI 30 practice exams

COM SCI 30 practice exams

Principles and Practices of Computing · University of California, Los Angeles (UCLA, UC Los Angeles) · also written COMSCI30

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

Looking for COM SCI 30 past exams?

Start with the past exams and tests your instructor or the UCLA library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh COM SCI 30 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
  • Functions and the Design Recipe · 100 questions
  • Booleans and Conditionals · 100 questions
  • Strings · 100 questions
  • For and While Loops · 100 questions
  • Lists, Mutability and Aliasing · 100 questions
  • Reading and Writing Files · 100 questions

More UCLA courses

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