Here are some questions that were asked in 2020-21W’s (online) final exam.

featured background

Fundamentals (20 points)

Definitions (10 points)

Identify the programming or Python term that matches the given definition.

M1. Name for a variable

  1. Algorithm

  2. Expression

  3. Identifier

  4. Keyword

  5. Literal

M2. Value passed to a function call

  1. Algorithm

  2. Argument

  3. Declarative

  4. Module

  5. Parameter

M3. Values and operations that evaluate to a value

  1. Algorithm

  2. Declarative knowledge

  3. Expression

  4. Identifier

  5. Keyword

M4. Statements and facts

  1. Argument

  2. Declarative knowledge

  3. Imperative knowledge

  4. Literal

  5. Parameter

M5. Variable whose value is assigned by a function call

  1. Algorithm

  2. Argument

  3. Declarative

  4. Module

  5. Parameter

M6. Value that means exactly what’s written

  1. Argument

  2. Expression

  3. Imperative knowledge

  4. Identifier

  5. Literal

M7. How to do things

  1. Declarative knowledge

  2. Expression

  3. Imperative knowledge

  4. Keyword

  5. Module

M8. Identifier whose meaning is defined by a language

  1. Algorithm

  2. Expression

  3. Keyword

  4. Literal

  5. Parameter

M9. Collection of statements

  1. Argument

  2. Expression

  3. Literal

  4. Module

  5. Parameter

M10. Procedure with decisions

  1. Algorithm

  2. Argument

  3. Declarative knowledge

  4. Expression

  5. 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

42

bool

float

int

list

str

M12

3.14

bool

float

int

list

str

M13

'Jonathan Anderson'

bool

float

int

list

str

M14

True

bool

float

int

list

str

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

42

bool

float

int

list

str

M16

1.1

bool

float

int

list

str

M17

1/2

bool

float

int

list

str

M18

1 % 2

bool

float

int

list

str

M19

1 < 2

bool

float

int

list

str

M20

1 // 2

bool

float

int

list

str

M21

sin(3.14)

bool

float

int

list

str

M22

sin(0)

bool

float

int

list

str

M23

1 < 2 < 3

bool

float

int

list

str

M24

1 < 2 and 2 < 3

bool

float

int

list

str

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

files
Figure 1. Python files for question L4

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

bool

float

int

list

str

M26

Grade on this exam

bool

float

int

list

str

M27

Final grade in this course

bool

float

int

list

str

M28

Temperature measurement

bool

float

int

list

str

M29

Temperature measurements for a day

bool

float

int

list

str

M30

Distance to the other side of the room

bool

float

int

list

str

M31

Immutable values representing a rectangle’s height and width

dict

list

set

str

tuple

M32

Information about all students in a course, quicly searchable by student ID

dict

list

set

str

tuple

Flowchart to Python (8 points)

L6. Implement the logic from the flowchart in Figure 2 in Python.

flowchart
Figure 2. A flowchart using lab Arduino functions

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