ENGG1811 Sample Mid-term Examination

0 Comments

ENGG1811 Sample Mid-term Examination
Term 3, 2019
October 14, 2019
Instructions
(a) This exam consists of 4 questions. Questions are of equal value. Answer all questions.
(b) Time allowed: 50 minutes including 5 minutes of reading time.
(c) Each question requires you to submit a separate Python program file for marking. Note the
following:
(i) Submission must be made using the Dragon icon located on the Desktop.
(ii) There is a tab for each question.
(iii) Each question requires a specific filename and the submission system will only accept that
particular filename.
(iv) Ensure that you save your file before submission. If the submission system accepts your
file, it will also display the contents of your submitted file. This allows you to check.
(v) You can make multiple submissions during the exam. Only the last submitted file will be
marked.
(d) For each question, an associated test file or testing code has been provided to help you to test
your code. The testing code is rudimentary and is not thorough. The file or code is provided so
that you can write your own tests. It is your responsibility to test your code thoroughly before
submission.
(e) During the mid-term exam, you can use only these applications: Spyder3, a PDF file reader and
a calculator app.
(f) You are allowed to consult the following Python documentations in PDF format:
 Python 3.7 tutorial (clickable link)
 List of functions in the Python math library (clickable link)
Note: You can use the above links in the sample midterm paper but in the actual midterm,
the documentations will be available from the background menu, which can be accessed by
right-clicking on the blue background.
(g) If you have accidentally killed Spyder3, the submission box, PDF file reader or file manager,
you can re-start them from the background menu. The calculator app is available on the background
menu.
1
Question 1
You have two sensor readings which are stored in two Python variables x and y of float type. Your
task is to write a Python code segment which determines a Python variable output_string, which
is a variable of string type, based on the values of x and y. The table below shows the value of
output_string for different values of x and y.
x  50 x > 50
y < 100 normal
y  100 alert poor-performance
Requirements and testing:
 You must write the code segment in the template file q1.py provided. The submission system
will only accept this filename.
 You must write your code segment to determine output_string between the two #-*-*-*
lines in the file q1.py
 You must use the variable names x, y and output_string
 The string variable output_string can only take the values of normal, alert or poor-performance
 The file q1.py contains only one test. You need to test your code for different values of x
and y before submission. You need to make sure that your test code are not between the two
#-*-*-* lines.
 Make sure that you save your file before submission.
2
Question 2
Let x and y be two real numbers. Consider the mathematical functions g(x; y) and h(x; y):
g(x; y) = cos(x)
h(x; y) =
3
xy
Your task is to write a Python function q2_func, which has two inputs and two outputs, and has
the following def line:
def q2_func(x,y):
where you can assume the inputs x and y are variables of float type. You can associate the Python
variables x and y with, respectively, the real numbers x and y. If you call the Python function with
the following statement:
fg, fh = q2_func(x,y)
then the variables fg and fh should take on, respectively, the values of g(x; y) and h(x; y).
Requirements and testing:
 You must write the function q2_func in a file with the filename q2.py. The submission
system will only accept this filename. A template file q2.py has been provided.
 The file test_q2.py contains a rudimentary test. You need to test your function for different
values of x and y before submission. Note that you can assume that both x and y are non-zero.
 You do not need to submit test_q2.py.
 Make sure that you save your file before submission.
3
Question 3
Your task is to write a Python code segment that works on a Python list of numbers. Assume that you
are given a Python list with float type and we will refer to this list as list_a. Your program will
use list_a to produce another Python list, which we will refer to as list_b. These two lists are
expected to have the same length, and the entries these two lists are related by:
list_b[0] = cos(list_a[0])
list_b[1] = cos(list_a[1])
list_b[2] = cos(list_a[2])
….
Note that your Python code must be able to work with any Python list with the name list_a.
You can assume the entries of list_a are all of the float type.
Requirements and testing:
 You must write the code segment in the template file q3.py provided. The submission system
will only accept this filename.
 You must write your code segment to calculate list_b from list_a between the two #-*-*-*
lines in the file q3.py
 You must use the variable names list_a and list_b.
 The file q3.py prints the value returned by your function for the sample list_a, see the
comments in the file. You need to manually check whether the returned value is correct or not.
You need to test your code for different values of list_a before submission.
 Make sure that you save your file before submission.
4
Question 4
Your task is to write a Python function q4_func, which has one input and one output, and has the
following def line:
def q4_func(x):
where you can assume the input x is a Python list whose entries are of the type float. The output of
the function is equal to
(sum of the 1st, 3rd, … entries of x) 􀀀 (sum of the 2nd, 4th, … entries of x)
For examples,
(a) If x is [2, -3.1, 4.2, 6.7, 5.5, 3.4], then the output is (2+4:2+5:5)􀀀(􀀀3:1+
6:7 + 3:4) = 4:7
(b) If x is [2, -3.1, 4.2, 6.7, 5.5], then the output is (2+4:2+5:5)􀀀(􀀀3:1+6:7) =
8:1
Requirements and testing:
 You must write the function q4_func in a file with the filename q4.py. The submission
system will only accept this filename. A template file q4.py has been provided.
 The file test_q4.py checks the second example above. You need to test your function for
different values of x before submission.
 You do not need to submit test_q4.py.
 Make sure that you save your file before submission.
————- End of exam——————
5

Leave a Reply

Your email address will not be published.