Week 13 summary, Python quiz 1 topics and examples

16 Nov 2011

Example quiz questions

You'll be expected to understand how to read and/or write simple programs (without the use of a computer). You won't be marked off for simple naming errors (like calling the int type integer, for example), but you will be marked off for serious syntax errors.

This post will be updated with more practice questions later.

  1. What is the value of x after executing the following statements?

    x = 5
    y = 2 * x
    x = 3 * y
    
  2. What is the output of the following program (what is printed when the program runs)?

    for num in range(3):
        print("num squared is", num * num)
    
  3. Identify lines that would result in an error. For each such line, explain why.

    a = 'abc'
    b = 'def'
    c = 5
    d = 6
    e = a + b
    f = a + c
    g = c + d
    x = [1, 2, 3]
    y = [4, 5, 6]
    z = x + y
    z2 = x * y
    z3 = x + a
    p = True
    q = p + 'True'
    
  4. Write a program that asks for you to input a number, and then prints the numbers from 1 up through the number you gave as input.

  5. Write a program that prints the maximum value that the function defined by f(x) = x(x-50)(x-100) achieves on an integer in the range 0-99. Hint: build up a list of f(x) for all x in the given range, and then use the max function.

Readings

The quiz will cover topics in chapters 1-4 and 11 in How to Think Like a Computer Scientist: Learning with Python. Make sure you've read and done as many exercises as you can in those chapters.