ECOR1041_Midterm_F23_B_SOLUTION

pdf

School

Carleton University *

*We aren’t endorsed by this school

Course

1041

Subject

Computer Science

Date

Feb 20, 2024

Type

pdf

Pages

7

Uploaded by omarmanna09

Report
ECOR 1041 Midterm Exam Fall 2023 SECTION B Instructions: Do NOT draw or write on the bar codes at the top/bottom of the exam pages. This is a closed-book exam. Non-programmable calculators are permitted. All other electronic devices (tablets, laptop computers, cell phones, smart watches, headphones, etc.) must be placed under your chair or left in your backpack or purse. This exam is 75 minutes long. There are three sections worth 25 marks in total. Attempt all questions. Use the pages labeled “rough work” for rough work. The “rough work” pages will not be graded. You are not allowed to remove any pages from your exam. Do not ask questions unless you believe that you have found a typo. Be sure you are writing this exam in your assigned room . Not writing in your assigned room will result in a 5-mark penalty. Be sure that you have clearly written your name and student number on the first page. Not including your name or student number on this paper will result in a 5-mark penalty. Be sure that you have clearly filled the bubbles for your student number on the first page. Not filling in the bubbles or incorrectly filling in the bubbles will result in a 5-mark penalty. All rights reserved : No part of this document may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission from the instructor.
SECTION 1 Multiple choice (…/1 6) This section contains 16 multiple-choice questions. Each question has one correct answer. Record your answers on the Bubble sheet at the end of the exam using the answer spaces 001-016 . For each question, fill in (shade) the circle (A, B, C, D, or E) that corresponds to the correct answer. Answers recorded on the question paper will not be graded. When answering the questions, Fill in (shade) the answers firmly and neatly, preferably with a dark pencil. Completely erase any changed answers Do NOT tear the bubble sheet. Questions Q1 Convert the 8-bit binary number 00010110 to decimal, assuming that the binary number is the signed representation (signed magnitude) of the decimal value. a) -22 b) 22 c) -44 d) 44 Q2 Convert the 4-bit binary number 1010 to decimal, assuming that the binary number is the unsigned representation of the decimal value. a) 2 b) 4 c) 10 d) 20 Q3 Convert the decimal value 17 to a 5-bit binary number using signed representation (signed magnitude) a) 10001 b) 01001 c) 10010 d) 17 cannot be represented using 5-bits and signed magnitude Q4 Assuming that both values are unsigned binary integers, what is the result of the following operation? 1001 + 1111 a) 11000 b) 10001 c) 01111 d) 10110
Q5 Convert the fractional binary number 10.01 to decimal. a) 2.025 b) 10.025 c) 2.25 d) 10.25 Q6 Which of the following variable names will produce a syntax error in Python? a) My_house b) __my______house__ c) myHouse d) my-house Q7 In Python, a variable can refer to values of different types at different points in the program execution. a) True b) False Q8 What would be the output of the following expression if someone types it in the Python shell? >>> (-5 % 7 - 4 // -1) x 6 a) Syntax error b) -4 c) 24 d) 36 e) 1 Q9 What would be the output of the following expression if someone types it in the Python shell? >>> 5.0 ** 2 // 2 a) 5 b) 5.0 c) 12 d) 12.0 e) Syntax error Q10 In Python brackets, [], braces, {}, and parentheses () have the same meaning. a) True b) False
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Q11 What would be the output of the following set of expressions if someone types it in the Python shell? >>> price = 77 >>> cost = price / 3 >>> print(round(cost)) a) 25.67 b) 25 c) 26 d) 25.7 e) Syntax error Q12 Which of the following is not a built-in function in Python, i.e., it requires an import statement? a) min b) sqrt c) abs d) pow Q13 In Python, if the type annotation of the return value of a function is set to float, the function will always (regardless of the inputs) return a floating-point number. a) True b) False Q14 The definition of the function area_trapezoid is executed in Python. def area_trapezoid(base1: int, base2: int, height: int) -> float: return 1 / 2 * (base1 + base2) * height y = area_trapezoid(2, 2, 2) Which of the following statements is correct? a) An object of type function appears in the global frame. b) An object of type function appears in area_trapezoid frame. c) 4 is bound to y d) 4.0 is bound to y Q15 What is the first step you should perform when following the Function Design Recipe? a) Test the function b) Write the function’s d escription c) Specify the f unction’s h eader d) Write the function’s usage examples e) Code the f unction’s body
Q16 This script is typed in the Python Tutor editor. def f(x): x = 2 * x #Line A return x x = 1 x = f(x + 1) + f(x + 2) Which Python Tutor diagram depicts memory immediately after the assignment state at #Line A is executed for the first time, but before the return x statement is executed? a) b) c) d)
SECTION 2 (…/ 5) Answer this question in the space provided on this page. Given the following function header and docstring, complete the function’s body in the space provided. Note 1 : You will lose marks if you use local variables in your solution. Note 2: You are permitted to use Python’s built -in functions in your solution. MAX_HEART_RATE = 210 def theoretical_max_heart_rate (age: int, fit: bool) -> int: """ Return the maximum theoretical heart rate based on age and fitness. Maximum theoretical heart rate: - For fit people, it is the maximum heart rate minus twice the age. - For unfit people, it is the maximum heart rate minus the age. - The maximum heart rate must be at least 90. Precondition: age > 0 Examples: >>> theoretical_max_heart_rate (40, False) 170 >>> theoretical_max_heart_rate (40, True) 130 >>> theoretical_max_heart_rate (80, True) 90 """ if fit: # if fit == True: return max(90, MAX_HEART_RATE 2 * age) else: # else optional return max(90, MAX_HEART_RATE age) # Correct if/else condition - 1 mark # Correct use of constant MAX_HEART_RATE and parameter age - 1 mark # Correct return value when fit is true - 1.5 marks # Correct return value when fit is false - 1.5 marks Deductions: # Use of local variables: 1 mark deduction # Incorrect indentation: 0.5 marks deduction # No negative marks. Minimum mark is zero. # Comments are not required.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
SECTION 3 (…/ 4) Answer this question in the space provided on this page. This function body calculates the area of a triangle: return 1 / 2 * base * height Complete the function’s header and docstring in the space provided: def __area_triangle_______(_base_____: int, ____height_: int) -> ___float___: """ Return the area of a triangle. Preconditions: base > 0; height > 0 >>> area_triangle(10, 5) 25.0 >>> area_triangle(5, 3) 7.5 >>> area_triangle(1, 1) 0.5 """ # Meaningful function name: 0.25 marks # Function name follows pothole coding convention: 0.25 marks # Correct parameter names - they must be base and height (i.e., taken from the function body).: 0.25 marks # Correct return type: 0.5 marks # Proper indentation and style: 0.25 marks # Function description: starts with "Return" not "Returns": 0.25 marks # Function description: what the function does and not how: 0.25 marks # Preconditions: 0.5 marks # Examples: 1.5 marks (must be at least three): 0.5 each: 0.25 for integer input arguments and 0.25 for correct return value that is of type float