You are on page 1of 7

CS 177 – Mid Summer Review: Example Problems

Question 1:
A Fibonacci sequence is a series of integers (usually starting with 0, 1) in which each subsequent value is
the sum of the two preceding numbers. The simplest example of a Fibonacci series is 0, 1, 1, 2, 3, 5, 8, etc.
(note that the 0th Fibonacci value is 0).

Define a function named q1() that accepts 1 integer (n) as a parameter and returns an integer representing
the n th value in the Fibonacci sequence. (NOTE: the function should return the value; it should not print it)

Example output of function q1() at the interactive Python prompt:

>>> q1(0)
0
>>> q1(6)
8
>>> q1(15)
610
>>> q1(39)
63245986

You may use any variable names you need, but your function should return the n th Fibonacci value.

Question 2:
A dog sitter is open for business from 7:00 am until 10:00 pm. She charges $4.75 per hour from 7:00 am
until 5:30 pm. From 5:31 pm until 10:00 pm the rate is $3.50 an hour. These rates are per dog, and she does
not dog sit before 7:00 am or after 10:00 pm. Partial hours are prorated.

Define a function named q2() that accepts the number of dogs (an integer), a starting time (string) and
ending time (string) and calculates the total dog sitting bill. Assume that the starting and ending time strings
are in 24hour format, meaning that the string “07:00” would indicate 7 am and the string “15:30” would be
3:30 pm.

Some example calculations:

# Dogs Start Time End Time Total Bill


======= =========== ========= ===========
1 08:30 17:00 41.00
3 11:15 18:30 99.56
2 07:00 20:10 118.42

NOTE: the function should return the total rate as a Float value with 2 decimal places; it should not print it.
Question 3:
Our sun appears bright in the daytime sky most of the time. During a solar eclipse however, the moon
passes between the earth and sun casting a shadow on the surface of the earth. You have been asked to
create a animated graphics simulation of the appearance of a solar eclipse from the earth’s surface.

Define a function named q3() that uses the Python Graphics module and creates a 300x300 window with a
black background. A large white circle (radius 100) should be drawn in the center of the graphics window
and will represent the sun. A text message should appear in the middle of the circle asking the user to click
to begin the simulation. Once a mouse click is detected, the message should be removed and an identically
sized black circle representing the moon should start to the left of the ‘sun’ then pass from left-to-right
smoothly across until it eventually blocks our view of the sun. The moon’s black circle should continue to
move across the screen (1 or 2 pixels at a time) until the sun is completely visible again. At this point another
message should appear stating the the simulation is over. A final mouse click should close the graphics
window and the function should end.

Your function should create a graphics animation very similar to the following screenshots:

The q3() function will not accept any parameters or return any values. All the necessary commands for the
graphics animation should be contained within the function.

Question 4:
Define a function named q4() that accepts a string as a parameter. After verifying that the string includes
only includes numeric digits, count the number of even (E) and odd (O) digits. Finally, create a new string
made up of these values and their sum. Repeat this process until your end result is a string containing ‘123’.
For example, ‘15327’ contains 1 even digit (E=1), 4 odd digits(O=4) and the sum of these is 5 (E+O). The
new string would be ‘145’. Repeating the process (E=1, O=2, E+O=3) gives me ‘123’. The function q4()
should return the number of repetitions it takes to create the string ‘123’.

Example output of function q4() at the interactive Python prompt:

>>> q4('123')
0 ß Already '123' so takes zero repetitions
>>> q4('5')
2
>>> q4('471')
1
>>> q4('1234567890')
3
>>> q4('0')
2
NOTE: Your q4() function should return only the number of repetitions; it should not print anything.
Question 5:
In Boilerville, a bicycle can be rented between 8:00 am until 9:00 pm. The charge for each bike is $1.75 per
hour from 8:00 am until 3:30 pm. From 3:30 pm until 9:00 pm the rate is $2.50 an hour. These rates are per
bike, and they do not rent bikes before 8:00 am or after 9:00 pm. Partial hours are prorated.

Define a function named q5() that accepts the number of bicycles (an integer), a starting time (string) and
ending time (string) and calculates the total rental fee. Assume that the starting and ending times are in
24hour format, meaning that the string “08:00” would indicate 8 am and the string “16:30” would be 4:30 pm.

Some example calculations:

# Bikes Start Time End Time Total Rent


======== =========== ========= ===========
1 08:30 17:00 16.0
3 11:15 18:30 44.82
2 09:00 20:10 46.08

NOTE: the q5() function should return the total rate as a Float value with 2 decimal places; it should not
print it.

Question 6:
One method to determine if an integer is a multiple of 9 is to sum the individual digits, then continue to sum
the resulting values until you have a one-digit result. For example, the integer 1863 can be verified as a
multiple of 9 by adding the digits (1 + 8 + 6 + 3 = 18), then adding the digits of the resulting value (1 + 8 = 9).
Since the final single digit answer is 9, then we know that the starting value was an even multiple of 9.

Define a function named q6() that accepts an integer as a parameter, then (using only string manipulation
and integer addition), creates a list of all the values obtained by adding the digits together until arriving at a
single digit final answer. The function should return the list of resulting values. If the parameter string does
not contain only digits, the function should return an empty list.

Example output of function q6() at the interactive Python prompt:

>>> q6('0')
[ 0 ]
>>> q6('Bob') ß Doesn’t contain digits, returns empty list
[ ]
>>> q6('9')
[ 9 ]
>>> q6('39')
[ 12, 3 ]
>>> q6('9267304815')
[ 45, 9 ]

NOTE: Your q6() function should return only the list of values; it should not print anything.

Question 7:
Define a function named q7() that accepts two integer parameters and returns the sum of them. Next, write
the Python code that uses the q7() function to get the result of 15 + 7 and store that in the variable, x
Question 8:
Define a function named q8() that accepts 1 string as a parameter and returns 1 string. The q8() function
should build the returned string by reversing and capitalizing the characters in each word from the parameter
string (a ‘word’ is defined as a series of characters unbroken by a space – see the examples below). When
writing your code, use these examples for testing.

For example:

Input String Returned String


=========== =============
I am Sam I MA MAS
Sam I am MAS I MA
That Sam-I-am TAHT MA-I-MAS
That Sam-I-am! THAT !MA-I-MAS
I do not like I OD TON EKIL
That Sam-I-am TAHT MA-I-MAS

NOTE: q8() should return the modified Strings -- it should not use use print().

Question 9:
In Boilerville, an umbrella can be rented between 7:00 am until 9:30 pm. The charge for each is $.85 per
hour from 7:00 am until 10:30 am. From 10:30 am until 9:30 pm the rate is $.95 an hour. These rates are per
umbrella, and they do not rent before 7:00 am or after 9:30 pm. Partial hours are prorated.

Define a function named q9() that accepts the number of umbrellas (an integer), a starting time (string) and
ending time (string) and calculates the total rental fee. Assume that the starting and ending times are in
24hour format, meaning that the string “07:00” would indicate 7 am and the string “17:30” would be 5:30 pm.

Some example calculations:

# Rented Start Time End Time Total Bill


======== =========== ========= ===========
1 07:30 13:00 4.93
3 12:25 17:30 4.83
2 09:00 21:30 11.73

NOTE: The q9() function should return the total rate as a Float value with 2 decimals; it should not print it.

Question 10:
Define a function named q10() that includes a while loop which prints the integers from 20 to 32

Question 11:
Define a function named q11() that creates a graphics window with dimensions of 100x100 and fills the
window with a grid of size 10x10 (pixels).
Multiple Choice Practice Questions

CS 177 exams are all Live Coding format, (not multiple choice), however it’s helpful to practice reviewing
questions like these to refine your general knowledge and debugging skills.

1) What is the output of the following Python code? 



A. 78.0

B. 10

C. 10.0

D. None of these are correct 




2) Given the function definition:

def foo(a, b, c)

How many parameters does this function require?

3) How many Circle() objects will this code store in a list?

a) 10
b) 1
c) 5
d) This code will produce an error
4) What size graphics window will be created by running this code?

a) This code will produce an error


b) No error, but won't create a window
c) 200 x 200
d) 400 x 400

5) What does the following program print?

a) 5
b) 17
c) 25
d) None

6) What does the following code print?

a) Itsa me, Mario!


b) Python is so much fun!
c) So many prints.
d) Phew, the last one
7) What is the fundamental difference between a for loop and a while loop?

8) How many times will the following while loop print 'Hello!'?


a) Once
b) Zero times
c) This program will cause an error
d) An undetermined amount of times

9) What does the following code print?

a) 5
b) 1
c) 100
d) This code produces an error

You might also like