cmpt 120 finalsample

cmpt 120 finalsample - CMPT120 – Introduction to...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CMPT120 – Introduction to Computing Science and Programming I Instructor: Muntaseer Salahuddin SAMPLE Final Exam Total Points: 100 Total Time: 180 minutes Last Name: _______________________________________________ First Name: _______________________________________________ English Name (if applicable):_________________________________ Student ID: _______________________________________________ Signature: ________________________________________________ INSTRUCTIONS: • Please fill in the information above very clearly. • Do not open this test booklet until instructed to do so. • Please write all your answers in this booklet. You will NOT be given any extra pages to write your answers. There are two empty pages at the end in case you need to make draft notes. • There are 35 questions in total. Write your answers to the first 30 questions (multiple choice questions) in the table ON PAGE 2. There is partial marking for questions 31 to 35 so answer as much as you can. IGNORE QUESTIONS 22-24 AS THEY ARE BASED ON THE TOPIC OF “DICTIONARIES” WHICH WE DID NOT COVER THIS SEMESTER TRY THE QUESTIONS YOURSELF AND YOU MAY VERIFY YOUR ANSWERS WITH ME Please write your answers to Questions 1 to 30 in the following table. Question Answer Question Answer Question Answer 1 11 21 2 12 22 3 13 23 4 14 24 5 15 25 6 16 26 7 17 27 8 18 28 9 19 29 10 20 30 Multiple Choice Questions (Questions 1 – 30, each worth 1 point, no negative marking) 1. What is (11011000)2 in decimal? (assume unsigned integer) a. 108 b. -74 c. 217 d. 216 2. What is (100)10 in binary? a. 100 b. 1100100 c. -0 d. 1100110 3. What is (11)2 + (11)2? (assume unsigned integers) a. 100 b. 110 c. 111 d. 22 4. What is the 1’s complement of (1100011)2? a. 0011100 b. 1011100 c. 0011000 d. 0000110 5. What is (-57)10 in binary? (assume 2’s complement) a. 000111 b. 1000111 c. 111010 d. 100111 6. Which one of the following is a valid variable name? a. _9 b. -9 c. ~9 d. @9 7. What would be the output of: print type("[mark,greg,dave]") a. <type 'float'> b. <type 'int'> c. <type 'list'> d. None of the above 8. Which one of the following is an invalid variable name? a. _765 b. n765 c. 765_ d. _765_ 9. What would be the output of: print 20%2 = = 1 a. False b. 0 c. True d. 1 10. What would be the output of: print 4*1**2 a. 16 b. 8 c. 4 d. 2 11. What would be the output of: print range(5,0,-1) a. [1, 2, 3, 4, 5] b. [5, 4, 3, 2, 1] c. [0, 1, 2, 3, 4, 5] d. [5, 4, 3, 2, 1, 0] Figure 1: A Python Program 12. What would be the output of the code in Fig. 1? a. Lower b. Middle c. Upper Middle d. Rich 13. If we replace line 12 with inc = round(inc), what would be the output of the code in Fig. 1? a. Lower b. Middle c. Upper Middle d. Rich 14. Evaluate the following expression: A!=B and (C and not(C)) and not(F) where A = "True", B = "true", C = random.randint(0,1), F = False Assume that the random module has been imported and recall that the randint(a,b) function generates integer numbers n such that a<=n<=b. Also assume 0 stands for the Boolean False and 1 stands for the Boolean True. a. 1 b. 0 c. Some times 1 some times 0 d. None of the above 15. If myList = [‘dan’, ‘andy’, ‘vicky’, ‘alex’, ‘george’, ‘jess’, ‘sarah’, ‘lauren’] then what is the output of: print myList[:-len(myList)] a. [‘dan’, ] b. [‘dan’, ‘andy’] c. [‘lauren’] d. Figure 2: 4 Python Functions Questions 16 to 19 are based on the functions (func1, func2, func3, and func4) in Figure 2. The argument s passed to each of these functions is a string. For these questions assume that the string module has been imported. The string module has a string called lowercase which contains all the lower case alphabets. In other words, string.lowercase = “abcdefghijklmnopqrstuvwxyz”. 16. Which function in Figure 2 checks whether the last character in s is in lowercase? a. func1 b. func2 c. func3 d. func4 17. Which function in Figure 2 checks whether any of the characters in s is in lowercase? a. func1 b. func2 c. func3 d. func4 18. Which function in Figure 2 checks whether all the characters in s are in lowercase? a. func1 b. func2 c. func3 d. func4 19. Which function in Figure 2 checks whether the first character in s is in lowercase? a. func1 b. func2 c. func3 d. func4 20. If a = [1,2,3] , b = [4,5,6] and c = [7,8,9] then what is the output of: print [c,a+b] a. [[7,8,9,5,7,9]] b. [[7,8,9],[1,2,3,4,5,6]] c. [7,8,9,1,2,3,4,5,6] d. [[7,8,9,1,2,3,4,5,6]] 21. The slice operator can take a third argument indicating the step size. For example, if d = [1,2,3,4,5,6,7,8,9] then d[1:5:2] = [2,4] , d[:4:3] = [1,4] , d[::-1] = [9,8,7,6,5,4,3,2,1] (If the step size is negative, default start location is the back of the list and list is read backwards). Now what would be the output of: print d[::-2] a. [9,7,5,3,1] b. [9,8,7,6,5,4,3,2,1] c. [1,3,5,7,9] d. Error Figure 3: A Python Program Questions 22 and 23 are based on Figure 3. Recall that every dictionary has a function called values() that returns the values in the dictionary in a list, and every list has a function called sort() that sorts the list in ascending order. 22. What is the program in Figure 3 doing? a. Printing the names of famous people born in each year with the years sorted in ascending order. b. Printing the names of famous people born in each year with the years in some random order. c. Printing the names of famous people born in each year with the names sorted in descending order. d. Printing the names of famous people born in each year with the years sorted in descending order. 23. If we comment out line #5 in Figure 3 then what is the program doing now? a. Printing the names of famous people born in each year with the years sorted in ascending order. b. Printing the names of famous people born in each year with the years in some random order. c. Printing the names of famous people born in each year with the names sorted in descending order. d. Printing the names of famous people born in each year with the years sorted in descending order. 24. If we comment out line #5 in Figure 3, and replace line #6 with for e in vals: then what is the program doing now? a. Printing the names of famous people born in each year with the years sorted in ascending order. b. Printing the names of famous people born in each year with the years in some random order. c. Printing the names of famous people born in each year with the names sorted in descending order. d. Printing the names of famous people born in each year with the years sorted in descending order. Figure 4: A Python Program Questions 25 and 26 are based on the python program in Figure 4. Assume in this program that n is some integer. 25. What is the runtime of the python program in Figure 4 in terms of n? a. n2 b. i/n c. 1 d. n 26. If we replace line #2 with j = i/2 then what would be the runtime of the python program in Figure 4? a. n2 b. n2/2 c. n/2 d. n Figure 5: A Python Function Question 27 to 30 is based on the python function in Figure 5. Assume that we pass to this function a list of integers. Recall that an iterative program solves problems using loops, whereas recursive programs solve the same problems by recursively calling themselves. Also recall that recursive solutions to problems can be designed in 4 steps as discussed in class. 27. What is the function in Figure 5 doing? a. Recursively reducing the size of a list. b. Iteratively counting the number of even integers in a list of integers. c. Recursively counting the number of even integers in a list of integers. d. Recursively counting the number of zeros in a list of integers. 28. Lines 2 and 3 of Figure 5 represent which step of a recursive design of a solution to a problem? a. Step 1 b. Step 2 c. Step 3 d. Step 4 29. Line 5 of Figure 5 represents which step of a recursive design of a solution to a problem? a. Step 1 b. Step 2 c. Step 3 d. Step 4 30. Lines 6 to 9 of Figure 5 represent which step of a recursive design of a solution to a problem? a. Step 1 b. Step 2 c. Step 3 d. Step 4 31. Following is a python code where I have numbered the lines for easy reference. Figure 6: A Python Program a. Write down the first 12 lines to be executed by the program in Figure 6 in the exact sequence as they are executed? Just mention the numbers of each line. [3 points] b. How many different built-in python functions/commands are there in Figure 6? What are they? [2 points] c. How many different user defined functions are there in Figure 6? What are they called? [2 points] d. What is the output of the program in Figure 6? [3 points] 32. Write a program that reads some text from a file (the file has multiple lines of text and is called Paragraph.txt), then asks the user to provide a search word, then counts the number of times the search word has occurred in the file, and finally writes this count along with the search word in another file (called WordCount.txt). So, for example, imagine Paragraph.txt looks like this, The following list of selected readings provide you with the opportunity to read ahead of the lectures Please note that these may provide additional details beyond what time permits in the lectures It is therefore useful to review these sections after the lectures as well And the user wants to know how many times the word the shows up in the file. Then WordCount.txt should look like this, the: 5 Notice that in the paragraph provided in Paragraph.txt the word the occurs 5 times. Also recall that every string has a method called lower() that returns that string in all lowercase letters. a. To solve this problem first write a function called ReadParagraph() that opens the file named Paragraph.txt and returns a list of strings with all the words in the file as its elements. [5 points] b. Now write a function called Count(searchWord, paragraph) that takes as argument a string (searchWord) and a list of strings (paragraph) and returns an integer count that indicates how many times searchWord has occurred in paragraph. [5 points] c. Write another function called WriteCount(searchWord, count) that takes as argument a string (searchWord) and an integer (count) and writes searchWord and count to a file named WordCount.txt in the format I showed in the example at the beginning of this question. [5 points] d. Finally call all the functions you wrote in part a, b, and c of this question and also add whatever other python code necessary (for example taking input from the user) to complete the program. So now you are completing the main body of your python program. [5 points] 33. Following is a python code where I have numbered the lines for easy reference. Figure 7: A Python Function a. Of all the algorithms that we have seen in this course, which algorithm is the function in Figure 8 most similar to? [1 point] b. What is the conceptual difference between the algorithm you mentioned in part a and the function in Figure 8? [5 points] c. What is the running time of the function in Figure 8? Show your calculations. [6 points] d. Is the running time of the function in Figure 8 better/worse/same as the algorithm you mentioned in part a? Justify your answer. [3 point] 34. Write a program that plays a simple game of dices between 2 players. Remember that a dice has 6 faces, so when you throw a dice you can get either 1 or 2 or 3 or 4 or 5 or 6 in each throw. Here’s the basic idea of the game, each player has 2 dices and they throw them one at a time. Whoever gets the highest score among the first throw wins, if both get the same score in their first throws then whoever gets the highest score among the second throw wins, if the second throws are also equal then we have a draw. So, for example, when you run your program you may see the following output, Player1: [1, 2] Player2: [6, 2] Player 2 wins. In another run of your program you may see the following output, Player1: [6, 1] Player2: [3, 6] Player 1 wins. You should note that each player’s dice value from a throw is generated randomly. Recall that the random module has a function randint(a,b) that generates numbers n such that a<=n<=b. a. To solve this problem first write a function called GetPlayerThrows() that returns a list of 2 integers where the first integer represents the first throw of a player and the second integer represents the second throw of a player. [5 points] b. Now write a function called CompareThrows(player1, player2) that takes as argument 2 lists (player1, player2) where each list has 2 integers representing the first and second throws of the dice for that player. This function should return either 0 or 1 or 2 where 0 means a draw, 1 means player 1 wins and 2 means player 2 wins. [5 points] c. Write another function called Declare(player1,player2,winner) that takes as argument 2 lists (player1, player2) where each list has 2 integers representing the first and second throws of the dice for that player, and an integer (winner) that indicates who won the game or was it a draw. This function should show the output in the format I mentioned in the 2 examples at the beginning of this question. [5 points] Finally we can call all the functions you wrote in part a, b, and c of this question to complete the program. I have done this part for you so NO NEED to write anything here. Just answer the parts a, b and c of this question. player1 = GetPlayerThrows() player2 = GetPlayerThrows() winner = CompareThrows(player1, player2) Declare(player1,player2,winner) 35. Recall that in English the alphabets a,e,i,o,u are known as vowels and all other alphabets are consonants. Write a recursive python function that counts the number of consonants in a string. Remember the 4 steps to designing a solution to a problem with recursion. Assume that the input string will be in all small case letters and will contain alphabets and white spaces only. [10 points] ...
View Full Document

This note was uploaded on 06/21/2011 for the course CMPT 120 taught by Professor Cuikerman during the Spring '08 term at Simon Fraser.

Ask a homework question - tutors are online