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

This page is subject to change.  
Please refresh regularly.

Last updated: Saturday 20th June 2020, 8:42 PT, AD

REFRESH THIS PAGE FOR LATEST VERSION

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

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
Academic Honesty Policy
Notes on Academic Honesty (Thanks to Greg Baker, SFU)
Example of Academic Honesty Violation




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

*** The focus of Quiz 2 is Functions and Lists  ***

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


*** The topics of Strings, 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, 60 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): 
short answer written questions, 
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.

Answer:

value of A       not A
false            true
true             false




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

Answer:

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 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])


Answer:    [2] [12, 14]





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

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

Explain this syntax.

Answer: 

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.



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

Answer: 

while 1:
    print ("error!")




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

Answer:

attempt to divide by zero 





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

Answer:

for i in range(20,40,3)
    print(i)

forgetting to put a colon (:) at the end of the first (for) line above




8. (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.) 


Please enter a string #1: yesterday
Please enter a string #2: all
Please enter a string #3: my
Please enter a string #4: troubles
Please enter a string #5: evaporated
['yesterday', 'all', 'my', 'troubles', 'evaporated']


Please enter a string #1: anne
Please enter a string #2: was
Please enter a string #3: here
Please enter a string #4: again
Please enter a string #5: today
['anne', 'was', 'here', 'again', 'today']
>>> 


Answer:

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

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

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





9.  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])

Answer:

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




Answer: C.  [2] [12, 14]



10. (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)


Answer:
------



# 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

 

 


 




































Valid HTML5!

Valid CSS!