Final Exam Page 1 of 6 600.112: Intro Programming for Scientists and Engineers Final Exam Peter H. Fröhlich phf@cs.jhu.edu December 20, 2012 Time: 40 Minutes Start here: Please fill in the following important information using a permanent pen before you do anything else! Your exam will not be graded if you use a pencil or erasable ink on this page. Name (print): Email (print): Ethics Pledge: With your signature you certify the information above and you also affirm the following: I agree to complete this exam without unauthorized assistance from any person, materials, or device. Signature: Date: Instructions: Please read these instructions carefully before you start. Switch off your phones, pagers, and other noisy gadgets! You are not allowed to have anything but a pen (pencil, eraser) and this exam on your desk. You are not allowed to talk to anyone during the exam. If you have a question, please raise your hand quietly. You must remain seated quietly until all exams have been collected. Remember that you can not claim grading errors if you do not use a permanent pen for your answers. Do not open before you are told to do so! You got out of 40 points.
Final Exam Page 2 of 6 1 Binary Warmup (12 points) For each of the following statements, determine whether it is either true or false. (1 point each) 1. import this # :-) 2. According to the PEP8 standard, the proper indentation level for PYTHON code is four spaces. 3. In PYTHON, the keys you put into a dictionary have to be immutable. 4. In PYTHON, calling a function that does not execute a return instruction evaluates to None. 5. The BUBBLE SORT algorithm requires O(n 2 ) time to sort a list of n elements. 6. In PYTHON, variables defined inside a function do not affect variables defined outside the function. 7. In PYTHON, the expression you.can().do.this() must always lead to an error. 8. In PYTHON, tuples cannot be modified after they have been created. 9. Doctests not only help you write correct code, they also provide examples of how to use a function. 10. PYTHON uses dots as in big.small to select a component small out of a composite big. 11. By default PYTHON prints floating point numbers with complete accuracy. 12. In a sorted list of length n we can find a particular element in O(log n) time.
Final Exam Page 3 of 6 2 Tough Choices (8 points) For each of the following questions, circle one answer out of the choices given. (2 points each) 1. You need to draw a three-dimensional plot of a function f : R R R for some math course you re taking. Which PYTHON library do you try first? (a) I ll pick NUMPY because I ll need matrices. (b) I ll pick PYGAME because I can animate the plot. (c) I ll pick MATPLOTLIB because it worked before. (d) All of the above. 2. Which of the following statements is not true about functions in PYTHON? (a) Functions are defined using the keyword def. (b) You can pass functions to other functions as parameters. (c) You can print a function which will print its PYTHON code. (d) Functions can call themselves, directly or indirectly. 3. Assume you have a class Point with a constructor def init (self, x, y): ; which of the following is the correct way to create an instance/object called point of that class? (a) point = Point(10, 20) (b) point = new Point(10, 20) (c) Point(point, 10, 20) (d) point = Point(). init (10, 20) 4. Why, when comparing two floating point numbers a and b for equality, did we not use a == b but instead resorted to abs(a - b) <= 0.001? (a) In PYTHON, floating point numbers do not support the == operator. (b) Floating point computations are only approximate, so we have to allow for small errors. (c) Floating point computations are rather slow, this was one way to make them faster. (d) All of the above.
Final Exam Page 4 of 6 3 Short Answer (8 points) For each of the following questions, answer in one to three sentences, the shorter the better. (2 points each) 1. You are supposed to write a function square(matrix) that returns True if the given matrix has the same number of rows and columns, False otherwise. Give at least four interesting test cases for the square function. (Do not write the function itself!) 2. The simple games and simulations we developed using PYGAME shared the overall structure of the main game loop. Describe the core elements of that loop. Explain! 3. We saw that matrix computations can be done a lot faster in NUMPY than in plain PYTHON. Why is this the case? How do you have to change your thinking as a programmer to take advantage of NUMPY? Explain! 4. You need to read a text file list.txt in which each line contains a number of fields separated by commas. For example the line Claus,Santa,North Pole,Nice should turn into [ Claus, Santa, North Pole, Nice ]. Write the PYTHON code!
Final Exam Page 5 of 6 4 Backtracking Meetings (12 points) You are working on a program that will schedule a set of meetings across a set of rooms in such a way that no room is double-booked. Meetings have a start time (in hours) from 0 to 23 as well as a duration (in hours) from 1 to 4. Your program uses tuples of two integers to represent meetings: (7, 3) is a 3-hour meeting that starts at 7 am whereas (10, 1) is a 1-hour meeting that starts at 10 am. The input to your program (which you will not write completely for this problem!) is a list of rooms as well as a list of meetings. The output of your program is a (valid!) schedule (i.e. list of meetings) for each room. 1. Write a PYTHON function valid(schedule) that given a schedule (which is a list of tuples as described above, one for each meeting) for a single room returns True if there are no conflicts, False otherwise. Note that [(7, 3), (10, 1)] is a valid schedule. Hint: The sorted() function works for lists of tuples! For example (6, 3) < (7, 3) < (7, 4). (8 points) 2. You choose to represent the final schedule for all rooms as a dictionary: The key is the room number, the value is a list of meetings for that room. Describe on a high level how you would use recursive backtracking to produce this dictionary (i.e. find a valid schedule for all rooms). Don t forget to explain how to determine that no valid schedule exists! (4 points)
Final Exam Page 6 of 6 This page is intentionally mostly blank in case you run out of space elsewhere. If you ended up here early, please go over everything again and remain seated quietly! Make sure that the title page is filled out correctly and in permanent pen. Maybe you want to rewrite your answers in permanent pen as well?