```
```

/******************************************************************/

This document is subject to change.

Please report any errors or omissions in this document:
anne.dawson@gmail.com

This document is current at:
````Tuesday 30th October 2018, 7:07 PT, AD`
```

/******************************************************************/

CSCI120 QUIZ 2
==============

```
```
`EXAM CONDITIONS APPLY FOR ALL QUIZZES`
```ELECTRONIC EQUIPMENT SHOULD BE SWITCHED OFF
AND PLACED AT THE FRONT OF THE ROOM.```
```Exam Rules
Notes on Academic Honesty (Thanks to Greg Baker, SFU)

```

Quiz 2 covers all topics covered in the course so far,
up to and including Strings.

*** The focus of Quiz 2 is functions, lists and strings ***

Note: Students may be tested in a quiz or an exam
on the content of reading assignments and lab assignments.

*** The topics of Files and Searching are NOT on Quiz 2.
These topics will be included on Quiz 3 and on the Final exam. ***

All topics from this course are listed on the
course schedule for the current semester.

CSCI120 QUIZZES
===============

There are three quizzes worth 20% in total:
Quiz 1 = 6%
Quiz 2 = 6%
Quiz 3 = 8%

*** The lowest scoring quiz
will be dropped at the end of the course
so that the remaining 2 quizzes are worth 10% each. ***

The date of the quiz is shown in
the course schedule and on C4.
See C4 for the latest details about Quiz 2.

The format of Quiz 2 is as follows:

Quiz 2
======

(6% of final grade, 90 minutes)

Questions are based on the course notes and assignments.

Quiz 2 is closed-book: no books, notes, calculators, phones
or any other electronic equipment are allowed during quizzes.

Valid photo ID is required.

Questions types may include (in any combination):
questions requiring interpretation of Python code,
questions requiring detection of errors in code
and writing new Python code.

Example Quiz 2 questions:

1. (Short answer) Write the truth table for the not Boolean operator.

value of A       not A
false            true
true             false

2. (Short answer) List four advantages of using functions in a program.

divides the work into teams
makes software development more manageable
enables software reuse
avoids repetition
makes programs more understandable
easier to debug
easier to maintain
easier to test

3. What will be the output when the following code runs?

s = "When can I get rest?"
print(s[5])
print(s[6:12])
print(s[9:])
print(s[0:5].islower())
print(s.count("e"))
s = s.replace("e","X")
print(s)
print(s[20])

c
an I g
I get rest?
False
3
WhXn can I gXt rXst?
index out of range
>>>

4.  What is the output when the following code is executed?

list1 = [2,4,6,8,10,12,14,16,18,20]
print (list1[0:1],list1[5:7])

5. (Short answer) The range built-in (always available) function has this syntax:

range([start,] stop [,step])

Explain this syntax.

The stop argument is required, the others are optional.
Repeats to one minus the stop value, in steps of one, unless other step value specifed.

6. (Short answer)  Write two lines of Python 3 code that prints "error!" forever when run.

while 1:
print ("error!")

7. (Short answer)  Give an example of a run-time error.

attempt to divide by zero

8. (Short answer)  Give an example of a syntax error.

attempt to divide by a string object

9. (Programming Question)
Using either a while loop or a for loop,
write Python 3 code to allow a user to enter 5 strings.
As each string is entered
it is appended to a list called myStringList.
The following is an example run,
user input is shown in bold font...
(Do not attempt to change any colours in your program code.)

myStringList = []
n = 0
while n < 6:
n = n + 1
s = input("Please enter a string #" + str(n) + ": ")
myStringList.append(s)
print(myStringList)

####  or use a for loop...

myStringList = []
for n in range(1,7):
s = input("Please enter a string #" + str(n) + ": ")
myStringList.append(s)
print(myStringList)

10.  What is the output when the following code is executed?

list1 = [2,4,6,8,10,12,14,16,18,20]
print (list1[0:1],list1[5:7])

A.  [2,4] [10, 12]
B.  [2,4] [12, 14]
C.  [2] [12, 14]
D.  [2] [10, 12, 14]

11. (Programming Question)

a) 7% Define a function (the function definition only) named combine_lists that receives two lists of integers (the function has 2 parameters). You can assume that each list is the same length and each list is not empty. The function will create a new list which is composed of the addition of each element of the two lists. The function returns the new list.

For example, if the function is sent this list:

[2,4,6,8,10]

and this list:

[5,6,7,8,9]

then the function will return this list:

[7,10,13,16,19]

b) 2%  Write the docstring for the function

c) 1% Write a single line of code (one statement) to call the function using the two example lists shown in part a)

------

# part a)
def combine_lists(list1,list2):
new_list = []
for i in range(len(list1)):
total = list1[i] + list2[i]
new_list.append(total)
return new_list

# part b)
''' (list,list) -> list
This function receives two (non-empty) lists of integers.
The lists are the same length.
The function will create a new list which is composed of
the addition of each element of the two lists.
The function returns the new list.
'''

# part c)
print(combine_lists([2,4,6,8,10],[5,6,7,8,9]))

GPA
===
Your attendance, conduct and progress are monitored throughout the course.
You may inspect your status report at any time using the online Gradebook
The date of the quiz is shown in the course schedule - latest details on C4.

/************************************************************/

Good Luck!

/************************************************************/

End of document

```

```

```

```