CMPT 141.3 Practice Final Exam PDF
Document Details
Uploaded by Deleted User
University of Saskatchewan
University of Saskatchewan
Tags
Summary
This document contains a practice final examination for CMPT 141.3 at the University of Saskatchewan. The exam covers topics like algorithms, and programming using various programming techniques. The document includes multiple-choice questions and written answer questions.
Full Transcript
University of Saskatchewan Department of Computer Science CMPT 141.3 PRACTICE Final Examination Timeless Marks: 75 Time...
University of Saskatchewan Department of Computer Science CMPT 141.3 PRACTICE Final Examination Timeless Marks: 75 Time: 180 minutes Name: NSID: Student #: *********************** Solution Guide *********************** NSID: Part I — Multiple Choice Choose the best answer for each question. Choose only one answer per question. (1) 1. Which of the following is an example of an algorithm? A. A voting ballot listing candidates and their political parties B. A box of parts for building a desk C. A recipe for baking a cake D. A movie theater schedule listing showtimes E. A highway road map For questions 2 through 4, assume that the following variable initializations are given. x = 3.3 y = 2.2 z = 1.1 a = 3 b = 2 c = 5 p = 1.5 + c * b q = x - a // c + z r = x + a * b / c - y (1) 2. What is the value of p after the above initializations? A. 13.0 B. 13 C. 11.5 D. 11.0 E. 11 (1) 3. What is the value of q after the above initializations? A. 1.16 B. 0.049 C. 3.8 D. 3.4 E. 4.4 (1) 4. What is the value of r after the above initializations? A. 0.0 B. 3.4 C. 1.1 D. 2.3 E. 1.42 CMPT 141.3 Final Exam Page 1 of 22 Timeless NSID: (1) 5. In total, how many function calls can be found in this program? name1 = "Ash" name 2 = "Gary" longest = max(len(name1), len(name2)) print("Longest name has", longest, "letters.") A. 0 B. 1 C. 2 D. 4 E. 6 (1) 6. What is displayed on the console by this program? a = 7 float(a) print(a) A. a B. "a" C. 7 D. 7.0 E. "7.0" (1) 7. Consider the following four versions of the formatTime() function: def formatTime(totalMinutes): def formatTime(totalMinutes): h = totalMinutes // 60 h = str(totalMinutes // 60) m = totalMinutes % 60 m =str(totalMinutes0 % 60) return str(h) + ":" + str(m) print( h + ":" + m ) (Version 1) (Version 2) def formatTime(totalMinutes): totalMinutes = input() def formatTime(totalMinutes): h = totalMinutes // 60 h = str(totalMinutes // 60) m = totalMinutes % 60 m = str(totalMinutes % 60) return str(h) + ":" + str(m) return print(h + ":" + m) (Version 3) (Version 4) If you ran the code: print(formatTime(95)) which version of the function would exactly cause the text 1:35 to be displayed? A. Version 1 B. Version 2 C. Version 3 D. Version 4 CMPT 141.3 Final Exam Page 2 of 22 Timeless NSID: (1) 8. Which line of code would need to replace the BLANK in order to print Hello, Pikachu! to the console? BLANK message = "Hello, " + s + "!" print(message) greet("Pikachu") A. def greet(str(s)): B. def greet(): C. def greet("s"): D. def greet(input(s)): E. def greet(s): (1) 9. Which line of code would need to replace the BLANK in order to print 1234 to the console? def combine_digits(d1, d2): answer = int( str(d1) + str(d2) ) return answer BLANK print(answer) A. combine_digits(1000, 234) B. combine_digits(str(12), str(34)) C. answer = combine_digits(12, 34) D. combine_digits(int(12), int(34)) E. combine_digits("12", "34") (1) 10. Recall that isdigit() is a string object method that returns True if its string object contains only digits and False otherwise. Which expression needs to replace the BLANK in order for the code to convert the string s to an integer whenever s consists only of digits? if BLANK: num = int(s) A. isdigit(s) B. s = s.isdigit() C. s.isdigit() D. isdigit(str(s)) E. s == isdigit() CMPT 141.3 Final Exam Page 3 of 22 Timeless NSID: (1) 11. Which line of code correctly imports the math library using the name m? A. import math as m B. import math as math C. import m as math D. import math E. import m For questions 12 through 13, assume that s is defined as follows: s = "DetectivePikachu" (1) 12. What is the value of the expression: s[-1:-len(s)-1:-1] ? A. "uhcakiPevitceteD" B. "DetectivePikachu" C. "u" D. "uh" E. "" (1) 13. What is the value of the expression: s[1:len(s):4] ? A. "etPc" B. "Dete" C. "achu" D. "uhca" E. ""eia" Assume that the following variable initializations are given for questions 14 through 15. x = 4 y = 77 (1) 14. Which expression evaluates to TRUE? A. x > y or x > 0 B. x == 4 and y > 100 C. not x == 4 D. not (x > 0 and y > 0) E. x > y or y == 42 (1) 15. Which expression evaluates to TRUE? A. not (x == y and x > 10 or y > 10) B. x > 0 and x > y and y > 0 C. x > 0 and (x == y or y < 100) D. not x > 0 or not y > 0 E. (x > 0 or y > 0) and (x > y or y == 100) (1) 16. What is displayed to the console by this program? CMPT 141.3 Final Exam Page 4 of 22 Timeless NSID: speed = 45 SPEED_LIMIT = 50 turbo = True if speed < SPEED_LIMIT: speed = speed + 10 elif speed < SPEED_LIMIT and turbo: speed = speed + 25 print(speed) A. 45 B. 50 C. 55 D. 70 E. 80 (1) 17. What is displayed to the console by this program? ptype = "grass" level = 3 if ptype == "electric": print("pikachu") elif ptype == "grass": if level == 1: print("bulbasaur") elif level == 2: print("ivysaur") else: print("unknown") A. pikachu B. bulbasaur C. ivysaur D. unknown E. Nothing is displayed CMPT 141.3 Final Exam Page 5 of 22 Timeless NSID: (1) 18. Consider the following programs. Which of them displays 11 to the console? i = 0 while i < 11: for i in range(0, 11, 2): i = i + 1 pass print(i) print(i) (Program 1) (Program 2) i = 11 i = 1 while i