CS 111X - Spring 2016 - Final Exam 1/10 Computing ID: CS 111X - Spring 2016 - Final Exam - KEY Name: Computing ID: On my honor as a student, I have neither given nor received unauthorized assistance on this exam. Signature: First, write your name and ID, pledge this test, and bubble your ID on this page. Failure to do so will result in penalty points. This is a closed note, closed book, closed computing device, 3-hour test. You are not to speak with anyone except the instructor or TA for any reason except an emergency during the test. Important: Students may take this test at different times. It is a violation of the Honor Code to talk about the test in any way with a student who has not yet taken it. All questions MUST be answered only on the page on which they are asked (answers on scratch paper or on other pages will not be accepted). Also, hard questions may be mixed with easy questions. If you start a question and it s hard or time-consuming, you might want to save it for the end of the test and go ahead and work on easier problems. State any additional assumptions you make. Good luck!
CS 111X - Spring 2016 - Final Exam 2/10 Computing ID: Multiple Choice [30 pts total; 2 pts each] Question 1. The function removes all whitespace from both ends of a string. A. rstrip() D. concat() B. trim() E. strip() C. remove_whitespace() 2. After the execution of the following statement, the variable price will reference the value. price = int(82.934) A. 82 D. 80 B. 83 E. False C. 82.9 3. What is the purpose of RAM in a computer? A. Store data after a machine has turned off B. Allow you to connect to the Internet C. Makes your hard drive run faster D. Helps keep heat consumption down E. Keeps active programs and data in short term memory available for usage 4. What is the value of val in the following expression if x = 4, y = 3, and z = 6? val = x < y or z > x A. True D. 5 B. False E. x C. 8 Answer E A E A 5. True / False: The last item in a list has the index -1. TRUE 6. What would be the value of the variable nums after the execution of the following code? nums = [1, 2, 3, 4] nums[3] = 10 A. [1, 2, 3, 10] D. [10] B. [1, 2, 10, 4] E. The code is invalid. C. [1, 10, 10, 10] 7. You want a variable that keeps track of the number of students in each class section at UVa. The best type for that variable to be is: A. str D. int B. bool E. dict C. list A E
CS 111X - Spring 2016 - Final Exam 3/10 Computing ID: Question 8. Ambiguity in a programming language can be a problem because: A. Designing algorithms that are clear and efficient is tough. B. Code has to be readable by others. C. Computers only do exactly what they are told to do. D. Ambiguous languages run slower than static languages. E. All of the above. 9. Which of the following is not a useful tip for the process of problem solving? A. Break complex problems into smaller problems B. Expect future reuse of code C. Find out as much info as you can D. Never copy your existing code E. Write out your algorithm before starting to code 10. True/False: If a whole paragraph is included in a single string, the split() method can be used to obtain a list of the words included in the paragraph. 11. True/False: Both of the following for clauses would generate the same number of loop iterations: for num in range(6) and for num in range(1,7) 12. What is the index of the last item found in a dictionary? A. 0 D. A dictionary is not indexed by a number B. 1 E. The key of the first item in the dictionary C. len(dictionary) - 1 13. What is the best thing to use to find all phone numbers in a large text document? A. Beautiful Soup D. regular expressions B. str methods E. my best friend C. phone_number_finder 14. The purpose of a library, like cimage or gamebox, is to: A. Allow your code to compile B. Prevent you from causing a stack overflow error C. Provide extra functionality for you to use in your program D. Enable PyCharm to use the optimization routines E. Allow for the use of the lambda functions 15. The following line of code will do what when put in the double loop of our image manipulation function: new_image.setpixel(old_image.width - col-1, row, old_pixel) A. Flip an image on the y axis B. Rotate the image 90 degrees clockwise C. Turn the image grayscale D. Resize the image to 50% E. Create a negative image Answer C D TRUE TRUE D D C A
CS 111X - Spring 2016 - Final Exam 4/10 Computing ID: Functions [30 pts total] 16. Write a function called word_wrap that takes a paragraph of text as a string and prints it to the screen such that each line is as close to 80 characters long as possible without going over. You should not try to split up words (such as with a hyphen). For example, if a line is currently at 78 characters long and the next word is summertime, you should start the next line with summertime. [10 pts] def word_wrap(string): words = string.split( " " ) lines = [ "" ] pos = 0 for word in words: if len ( lines [pos]) + len (word) < 80 : lines [pos] = lines [pos] + " " + word else : pos += 1 lines.append(word) for line in lines : print (line) 10 pts: Everything works, code is readable, good variable names, good formatting 8 pts: Minor errors in code - perhaps bad loop, incorrect algorithm, not printing correctly 6 pts: Multiple minor errors in code - multiple problems like those listed above 4 pts: Major errors in code - no chance correct answer is printed, major readability problems 2 pts: Code attempt is somewhat reasonable
CS 111X - Spring 2016 - Final Exam 5/10 Computing ID: 17. Write a function called most_common_names that takes a list of names and returns the name that appears the most often. If there is a tie, print just one of the names - it doesn t matter which. HINT: lists have a function called.count(x) that returns the number of times x appears in the list. [10 pts] def most_common_names(names_list): max_count = 0 max_name = "" for name in names_list: if max_count < names_list.count(name): max_count = names_list.count(name) max_name = name return max_name 10 pts: Everything works, code is readable, good variable names, good formatting 8 pts: Minor errors in code - perhaps bad loop, incorrect algorithm, not printing correctly 6 pts: Multiple minor errors in code - multiple problems like those listed above 4 pts: Major errors in code - no chance correct answer is printed, major readability problems 2 pts: Code attempt is somewhat reasonable 18. When executed, what is output by the following code fragment? [5 pts] 5.0 0.2_(3 pts: one right) def helper2(y, x): return x/y, y/x def helper(y, x): return helper2(x, y) x = 10 y = 2 x,y = helper(x,y) print (x,y) 19. When executed, what is output by the following code fragment? [5 pts] [2] 1_(3 pts: one right) def helper(x, y ): -1 if missing [] x[ 0 ] = 2 y = "2" x = [ 1 ] y = "1" helper(x, y)
CS 111X - Spring 2016 - Final Exam 6/10 Computing ID: print (x, y) Programming [20 pts total] 20. Write a program that will compare two text files line-by-line and print the differences between them. For example, assume you have the following two files: file_1.txt Here is some text that we can compare with another file. Even with a blank line! file_2.txt Here is some text that I can compare with another file... Even with a blank thingy! And an extra line! Notes: Your program should report any line that is different in any way between the two files. If a line is inserted in the middle of one file but not another, then every line below is probably going to be considered different. Treat this as a 1-to-1 comparison, line-by-line. You must handle the case that one file may be longer than the other. Print [none] if a line exists in one file but not the other. The first line of the file is Line: 0. You do NOT have to write a function for this problem - just code that does the job is fine. For the two example files above, an execution of your program should look like this: First file: file_1.txt Second file: file_2.txt Line: 1 < text that we can > text that I can Line: 3 < another file. > another file... Line: 6 < blank line! > blank thingy! Line: 7 < [none] > And an extra line!
CS 111X - Spring 2016 - Final Exam 7/10 Computing ID: 20. Only code on this page will be graded! So practice on scratch paper if needed! file_name_1 = input( First file: ) File_name_2 = input( Second file: ) file_1 = open (file_name_1, "r" ) file_2 = open (file_name_2, "r" ) file_1_lines = [] file_2_lines = [] for line in file_1: file_1_lines.append(line.strip()) for line in file_2: file_2_lines.append(line.strip()) loops = max ( len (file_1_lines), len (file_2_lines)) for pos in range (loops): if pos >= len (file_1_lines): print ( "Line:", pos) print ( "<", "[none]" ) print ( ">", file_2_lines[pos]) elif pos >= len (file_2_lines): print ( "Line:", pos) print ( "<", file_1_lines[pos]) print ( ">", "[none]" ) elif file_1_lines[pos]!= file_2_lines[pos]: print ( "Line:", pos) print ( "<", file_1_lines[pos]) print ( ">", file_2_lines[pos]) 5 pts: file management - opening files, reading files, etc 5 pts: overall algorithm - works and efficient 5 pts: code structure - correct loops, variables, etc 5 pts: handles extra lines on one file correctly
CS 111X - Spring 2016 - Final Exam 8/10 Computing ID: S hort Answer [20 pts total; 5 pts each] Please limit your answers to the lines provided. 21. What is the difference between a dynamically typed and statically typed language? Dynamic languages store data types with the value; statically typed store types with the variable. 5 pts: discusses both correctly. 3 pts: discusses one correctly 22. Describe the basic algorithm for turning an image into grayscale. No code is needed here. Double loop to go through each pixel. For each pixel, take the average of the RGB values to get the intensity of each pixel, representing its gray value. Put that pixel in the new image. Must have: loop and average of RGB values - 5 pts: both, 3 pts: one 23. Describe the fundamental differences between a list and a dict, including when you would use each. List is an ordered linear structure for multiple values; dict is a key/value structure and is unordered 5 pts: both, 3 pts: one 24. Describe the basic algorithm a gamebox game (i.e. the game loop ). In other words, what appears in the tick() function, in what general order, and what does each part do. Clear the screen, move everything that needs it, detect collisions and handle, handle input, draw everything, camera.draw() 5 pts: basic structure is good, mentions most things above 3 pts: structure is okay, mentions at least 3 things above
CS 111X - Spring 2016 - Final Exam 9/10 Computing ID: Have a great summer break! Thanks for taking CS 1110/1111 this semester! SCRATCH PAPER - Nothing on this page will be graded!
CS 111X - Spring 2016 - Final Exam 10/10 Computing ID: SCRATCH PAPER - Nothing on this page will be graded!