{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Exam 1_Spring 2011

# Exam 1_Spring 2011 - Lehman College CUNY CMP 230 Exam 1...

This preview shows pages 1–5. Sign up to view the full content.

Lehman College, CUNY CMP 230 Exam 1– SAMPLE EXAM Spring 2011 1. Write Python code that prompts the user for the area of a circle and prints out its circumference. Useful formulas: area = π · radius 2 , circumference = π · diameter 2. Trace the Python code below, i.e. indicate the state of the variables as they change. mon = 5 tue = (mon + 6) % 2 tue = tue + 3 wed = mon / 4 thu = mon ** 2 wed = mon = tue thu = mon - 2 * tue tue = wed * mon mon, tue = tue, mon 3. Write the following formulas in Python: (a) x = b + b 2 4 ac 2 a (b) s = a + b + c 2 area = s ( s a )( s b )( s c ) 4. Write for loop based Python code to calculate: (a) The product of all the integers from 10 to 100. (b) 3 . 145 101 . 5. Write a Python graphics based program that requires the user to click on a point in its window and then draws a 2 by 2 rectangle centered around that point. 6. Write for loop based Python code to do the following: (a) Print out the multiples of 5 from 205 to 305 inclusive. Note: the first thing printed will be 205 and the last thing printed will be 305. (b) Print the following output: Month 1 has 31 days. Month 2 has 28 days. Month 3 has 31 days. Month 4 has 30 days. Month 5 has 31 days. Month 6 has 30 days. Month 7 has 31 days. Month 8 has 31 days. Month 9 has 30 days. Month 10 has 31 days. Month 11 has 30 days. Month 12 has 31 days. 7. Write a Python graphics based program that requires the user to click on three points in its window and then draws the triangle connecting those points. 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
8. Trace the following Python code, i.e. indicate the state of the variables as they change and what output is printed to the screen: (a) f = 3 s = 5 for i in range(10,15): f, s = s, f + s print("f =", f, " s=", s) (b) n = 3 k = 4 for c in range(5): n = n + k k = 3 * k print("n =", n, " k = ", k) 9. Draw the output that would be displayed in the program’s window if the following Python graphics based program were executed: from graphics import * def main(): win = GraphWin() win.setCoords(0, 0, 100, 100) for n in range(10, 100, 10): Line(Point(n,0), Point(n,100)).draw(win) for n in range(10, 100, 10): Line(Point(0,n), Point(100,n)).draw(win) win.getMouse() main() 10. Write a complete program that uses a loop to help ask the user for the quantity and cost of each of 10 items. The program should then print out the total cost. An example run of the program should look like: Item 1: Enter quantity -> 3 Enter cost -> 10 Item 2: Enter quantity -> 10 Enter cost -> 2.30 Item 3: Enter quantity -> 4 Enter cost -> 5 : : Item 10: 2
Enter quantity -> 6 Enter cost -> 8.50 Your total cost is: 201.18 3

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Lehman College, CUNY CMP 230 Exam 1, Version 1 Spring 2011 1. Write Python code that prompts the user for the diameter of a circle, and prints out the circumference of the circle. Useful formulas: circumference = π · diameter 2. Trace the Python code below, i.e. indicate the state of the variables as they change. mon = 7 tue = (mon + 6) % 2 tue = tue + 3 wed = mon % 4 thu = mon ** 2 wed = thu = mon + 2 thu = mon - 2 * tue tue = wed * mon mon, tue = tue, mon 3. Write the following formulas in Python: (a) dist = ( x 1 x 2 ) 2 + ( y 1 y 2 ) 2 (b) area = 3 3 2 side 2 apothem = 2 area 6 side 4. Write for loop based Python code to calculate: (a) The product of all the integers from 20 to 200.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern