Learning Python

Final Exam

 

Student name:

 

 

 

 

 

1) Python was created by (circle one):

 

  1. Bill Gates
  2. A committee of open source users
  3. A tall Dutchman named Guido
  4. Al Gore

 

 

2) To traverse a directory tree in Python, you would be most likely to use:

 

  1. Tkinter
  2. The os.path.walk call
  3. The socket module
  4. Hiking boots
  5. Regular expressions

 

 

3) Python is useful for:

 

  1. Internet scripting
  2. Systems programming
  3. Graphical user interfaces
  4. Component integration
  5. Database access
  6. Text processing
  7. All of the above

 


4) In the space below, write a Python program that prints the string “Spam” 10 times on the standard output stream.

 

 

 

 

 

 

 

 

5) Now, write the same program as in question 4 again, but print the string “Spam X”, where the “X” is replaced with the current value of a loop iteration counter (the first output line should be “Spam 0”, the second “Spam 1”, and so on).

 

 

 

 

 

 

 

 

 

 

6) Write a Python function that accepts 2 arguments, and returns their product. That is, the function should apply the “*” operator to the two objects passed in.  Then, write calls to your function: the first call should pass in two numbers to invoke multiplication, and the second should trigger string repetition.

 

 

 

 

 

 

 

 

 

 

 


7) Write a Python function that accepts a list argument, and returns a new list that contains the first half of the items in the list passed in.  For instance, given a list [1,2,3,4], your function should return [1,2].  Given a list [1,2,3,4,5], your function may return either [1,2], or [1,2,3].  Hints: recall the built-in “len” function and list slicing expressions.

 

 

 

 

 

 

 

 

 

 

 

 

8) Does the solution for question 7 work if you pass it a string or tuple?  Why?  What does it return in both cases?

 

 

 

 

 

 

 

 

9) Does the solution for question 7 work if you pass it a dictionary?  Why?  What does it return?

 

 

 

 

 

 

 


10) In the context of Python, the term “immutable” means:

 

  1. Doesn’t talk much
  2. Cannot be changed in future Python releases
  3. Cannot be changed in-place
  4. Unreasonably stubborn objects

 

 

11) Consider the three statements below: does the last one change the value of A?

 

 

A = "spam"

B = A

B = "shrubbery"

 

 

12) Consider the three statements below: does the last one change the value of A?

 

 

A = ["spam"]

B = A

B[0] = "shrubbery"

 

 

13) If Python changes in a non-backward compatible way in the future, you should:

 

  1. Call Guido at home
  2. Seek spiritual enlightenment
  3. Change careers
  4. Mail python-dev@python.org

 

 

14) Extra credit: Why does the word “Spam” appear in so many Python examples?