Here are some questions that were asked in 2020-21W’s (online) final exam.
Fundamentals (20 points)
Definitions (10 points)
Identify the programming or Python term that matches the given definition.
M1. Name for a variable
-
Algorithm
-
Expression
-
Identifier
-
Keyword
-
Literal
M2. Value passed to a function call
-
Algorithm
-
Argument
-
Declarative
-
Module
-
Parameter
M3. Values and operations that evaluate to a value
-
Algorithm
-
Declarative knowledge
-
Expression
-
Identifier
-
Keyword
M4. Statements and facts
-
Argument
-
Declarative knowledge
-
Imperative knowledge
-
Literal
-
Parameter
M5. Variable whose value is assigned by a function call
-
Algorithm
-
Argument
-
Declarative
-
Module
-
Parameter
M6. Value that means exactly what’s written
-
Argument
-
Expression
-
Imperative knowledge
-
Identifier
-
Literal
M7. How to do things
-
Declarative knowledge
-
Expression
-
Imperative knowledge
-
Keyword
-
Module
M8. Identifier whose meaning is defined by a language
-
Algorithm
-
Expression
-
Keyword
-
Literal
-
Parameter
M9. Collection of statements
-
Argument
-
Expression
-
Literal
-
Module
-
Parameter
M10. Procedure with decisions
-
Algorithm
-
Argument
-
Declarative knowledge
-
Expression
-
Parameter
Number representation (10 points)
L1.1. What is the binary representation of the decimal number 10? (2 pts)
L1.2. What is the binary representation of the decimal number 50? (2 pts)
L1.3. What is the binary representation of the decimal number 255? (2 pts)
L1.4. What is the decimal representation of the binary number 1111? (2 pts)
L1.5. What is the decimal representation of the binary number 10001100? (2 pts)
Software analysis (50 points)
Types (14 points)
What are the types of the following literal values? Answer on your multiple-choice answer sheet.
Question | Value | a | b | c | d | e |
---|---|---|---|---|---|---|
M11 |
|
|
|
|
|
|
M12 |
|
|
|
|
|
|
M13 |
|
|
|
|
|
|
M14 |
|
|
|
|
|
|
What type will each of the following expressions evaluate to? Answer on your multiple-choice answer sheet.
Question | Expression | a | b | c | d | e |
---|---|---|---|---|---|---|
M15 |
|
|
|
|
|
|
M16 |
|
|
|
|
|
|
M17 |
|
|
|
|
|
|
M18 |
|
|
|
|
|
|
M19 |
|
|
|
|
|
|
M20 |
|
|
|
|
|
|
M21 |
|
|
|
|
|
|
M22 |
|
|
|
|
|
|
M23 |
|
|
|
|
|
|
M24 |
|
|
|
|
|
|
Boolean expressions (10 points)
Given the variables p = True
, q = False
and r = True
, what will the
following expressions evaluate to?
L2.1. p
L2.2. p and q
L2.3. p and not q
L2.4. p or q and r
L2.5. p or q or r
L2.6. p and not q and r
L2.7. p ^ q
L2.8. p ^ q ^ r
L2.9. (p and not q) ^ r
L2.10. p ^ p
Arithmetic expressions (6 points)
Given the variables x=1
, y=2
and z=3
, what will the following expressions
evaluate to?
L3.1. x * 20 // y ** z
(2 pts)
L3.2. z > y > x
(2 pts)
L3.3. x—y*z
(2 pts)
Software behaviour (12 points)
The Python files illustrated in Figure 1 are located in the same
directory/folder on a computer.
When the script main.py
is run, what will the final values of x
, y
and
z
be?
L4.1. Value of x
L4.2. Value of y
L4.3. Value of z
Python errors (8 points)
L5.1. Explain the syntax error in the following Python script: (2 pts)
a = 1_000_000
b = a ** 1j
numbers = [1, 2, 3]
10 = c
print(a, b, c, *numbers)
L5.2. Explain the syntax error in the following Python script: (2 pts)
my_name = 'Jon'
MY_NAME = 'JON'
_your_name_72 = 'Someone'
their_name_83 = 'Someone Else'
4getaboutit = 'Donnie Brasco'
L5.3. Explain the logical error in the following Python function. How could it be fixed? (4 pts)
def promotion_average(grades):
i = 0
total = 0
n = 0
while i < 10:
total += grades[i]
n += 1
print('Total thus far:', total)
return total / n
Software implementation (40 points)
Types (8 points)
Choose an appropriate type to represent each of the following values. Answer on your multiple-choice answer sheet.
Question | Value to be represented | a | b | c | d | e |
---|---|---|---|---|---|---|
M25 |
Name of a student |
|
|
|
|
|
M26 |
Grade on this exam |
|
|
|
|
|
M27 |
Final grade in this course |
|
|
|
|
|
M28 |
Temperature measurement |
|
|
|
|
|
M29 |
Temperature measurements for a day |
|
|
|
|
|
M30 |
Distance to the other side of the room |
|
|
|
|
|
M31 |
Immutable values representing a rectangle’s height and width |
|
|
|
|
|
M32 |
Information about all students in a course, quicly searchable by student ID |
|
|
|
|
|
Flowchart to Python (8 points)
L6. Implement the logic from the flowchart in Figure 2 in Python.
Binary numbers (24 points)
Having considered how to convert numbers to and from a binary representation, you will now implement Python code to convert lists of numbers into bits. In this question, you may not import any Python modules, but you may re-use any code that you write within this question.
L7.1. Write a Python function that, when passed a single integer, will return the binary representation of that number in the form of a list of zeroes and ones. (8 points)
L7.2. Write a Python function that, when passed a list of integers, will print out the binary representation of each. There should be one space between each number and no spaces or other characters between bit values (ones and zeroes). (8 points)
L7.3. Write a Python function that will accept a list of numbers and display
each number’s value by blinking its binary representation on an LED at 10 Hz,
i.e., ten bits per second.
You may assume that the engi1020
and time
modules
have already been imported using
from engi1020 import *
and from time import *
.
(8 points)
Reference material
Arduino functions
def analog_read(pin):
"""Read a value from an analog port.
Parameters:
pin (int): Analog pin to read from (e.g., 2 for A2).
Returns: Quantized analog value (0–1023).
"""
def analog_write(pin, duty):
"""Write a value to an analog port.
Parameters:
pin (int): Analog pin to write to (e.g., 2 for A2).
duty (int): Duty cycle to set (0–255).
"""
def digital_read(pin):
"""Read a value from a digital port.
Parameters:
pin (int): Digital pin to read from (e.g., 4 for D4).
Returns: True or False.
"""
def digital_write(pin, value):
"""Write a value to a digital port.
Parameters:
pin (int): Digital pin to write to (e.g., 4 for D4).
value (bool): Value to write (True or False).
"""
Python standard library
time.sleep(secs)
-
Suspend execution of the calling thread for the given number of seconds. The argument may be a floating point number to indicate a more precise sleep time. The actual suspension time may be less than that requested because any caught signal will terminate the sleep() following execution of that signal’s catching routine. Also, the suspension time may be longer than requested by an arbitrary amount because of the scheduling of other activity in the system.
- Selected
list
methods -
-
append
-
clear
-
count
-
extend
-
index
-
insert
-
remove
-
reverse
-
sort
-
- Selected
str
methods -
-
capitalize
-
count
-
endswith
-
find
-
index
-
isalnum
-
isalpha
-
isascii
-
isdecimal
-
isdigit
-
isidentifier
-
islower
-
isnumeric
-
isprintable
-
isspace
-
istitle
-
isupper
-
join
-
lower
-
lstrip
-
replace
-
rfind
-
split
-