Comp 115 Test 2 Study Guide

Test on Thursday February 27 in class

I tried my best to list the topics for the exam but I may have forgotten one or two. Your default assumption should be that if I covered it in class before the end of "if" statements, it is fair game for the exam.

I will add practice problems as I find new interesting questions, so come back to this page every few days, more practice problems may have appeared.

In addition to all concepts from Test 1,

Strings

Objects and Graphics

Functions

Practice Problems

  1. Explain why there are so many string delimiters.
  2. Suppose you want to only ever use the single quote as a string delimiter. Explain how you would write strings that contain the single quote as a character.
  3. Suppose that the assignment myString = "a completely random string" was just executed. What value would Python give to each of the following expressions:
    • myString[20:-1]
    • myString[20::-1]
    • myString[:5]
    • myString[1:15:3]
    Find a slice of the string contained in myString that would evaluate to the following values:
    • "pletely"
    • "eyn i"
    • "tdy"
  4. Write a function that takes two input parameters, a number of feet and a number of inches that, together, describe someone's height, and outputs a floating-point number that correspond that height in meters. Remember that there are 2.54 centimeters in an inch, 12 inches in a foot and 100 centimeters in a meter.
  5. Determine what would be printed on screen by the following snippets of code:
    1.  def aPrintingFunction():
           print("I'm printing stuff!")
      
       print("all done")
    2.  def headAndTail(inputString):
           s = inputString[:2] + inputString[-2:]
           return s
      
       print(headAndTail("Supercalifragilisticexpialidocious"))
  6. Write a program that creates a graphic window that is 300 pixels wide and 200 pixels high. In the window, draw a 100 x 100 yellow square with a black outline, and inside it, draw a red circle with a black outline whose center is the center of the square such that the perimeter of the circle barely touches the sides of the square.
  7. An coffee shop sells coffee online at 13.99 a pound plus the cost of shipping. Each order ships for 1.15 per pound, plus a 2.50 fixed cost. A 3% state tax is also applied before the cost of shipping is added. Write a function that takes the weight of an order as its input parameter and returns the cost of the order.
  8. Write a function that takes a string containing many words as its input parameter and returns the number of words contained in the string. To do this the string method count should be useful, look it up in the Python Documentation.
  9. Write a function that, like in the previous problem, takes a string containing many words as its input parameter and returns the average length of the words containined in the string. For this, you will need the function len, which takes a string as its input parameter and returns the length of the string.