Download exam2

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
3/6/2016
CS 110G Exam 2
Page 1
Computer Science I
Spring 2016, Friday, 3/4/16
100 points
Name _____________________________
1. True/false on while and for loop structures.
[10 pts]
_____ A while loop is an execution control structure that repeats the loop body zero or more times.
_____ The loop control variable associated with a for loop cannot be referenced inside the loop body.
_____ The condition controlling the while or for loop continues the looping as long as it evaluates to true.
_____ The condition of the while or for loop is evaluated before any of the loop body is executed.
_____ The loop control variable’s value should be altered at some point during execution of the loop.
_____ For loops provide additional functionality over while loops; there are situations that only for loops solve.
_____ For loops are generally used when we know the number of iterations prior to the loop.
_____ Loop control variables can only be changed by one, up or down.
_____ The range function specifies exactly last value the loop control variable will have, e.g. range(5) means the
lcv will have a 5 in its last iteration.
2. Show the output of the following independent code segments. Syntax errors are unintentional.
[4 pts each = 20]
int count = 1
while count < 5:
print(count)
count += 1
for count in range(6):
print(count,’ ’, end =’’)
String dna = “ATGATTCCTAA”
for count in range(6,1,-1):
print(dna[count,’ ’, end=’’);
count=1
while count*count<50:
print(count**2,’’, end=’’)
count = count + 2;
3/6/2016
CS 110G Exam 2
Page 2
(2. continued)
phrase = ”Spring break is nearly here!”;
count = 0
for i in range(len(phrase)):
if(phrase[i] == ’e’){
count += 1
print(i,’ ’, sep=’,’, end=’’)
print(”There were”, count, ”e’s found.”)
3. True/false on docstrings.
[5 pts]
______ Docstrings are delimited by triple double quotes and may be several lines long.
______ Docstrings ought to be put into each function that is written.
______ Docstring content may appear in some interactive development environments as pop up information on
function usage of the current function being typed by the programmer.
______ Docstrings should describe each parameter to a function.
______ Docstrings should describe the return value of a function
4. Global and local variables.
[15 pts]
Given the following program code, answer the questions below about this Python code:
g = 13
def func1(h):
g = h*2
print(locals())
return g
def func2():
global g
g = 50
print(g)
print(func1(10))
print(g)
func2()
print(g)
a. Show in the box on the right of the code what would be printed when this code is executed.
b. What kind of variables are in the third line of the code? _______________________
c. Is the use of global variables referenced in functions considered good practice? Why or why not?
d. Is the use of parameters considered good practice? Why or why not?
3/6/2016
CS 110G Exam 2
Page 3
5. Control structures and statements.
[12 pts]
a. Check off which of the following are the necessary control structures for programming?
_____ Sequence
_____ Iteration
_____ Calculation
_____ Selection
_____ Function/procedures
_____ Input/output
_____ Conditions
b. Describe the dual use of the return statement. Note in the absence of a return statement in a function there is
an implied one at the end of the function. Consider the code in question 4 to help answer.
6. For each of the number sequences, specify a range() function that would be used to either create a list of the
sequence or control the loop control variable of a for loop.
[12 pts]
0,1,2,3,4,5 _____range( ________________ )
1,2,3,4,5,6 _____________________________
10,12,14,16,18,20 ______________________________
99, 98, 97, 96, 95, ….3, 2, 1, 0 ____________________________
7. Fill in the blanks for the powers2 function that would generate the output as shown further below.
[10 pts]
def powers2(limit):
pattern = '2**{0:<____} = {1:>______}'
for ____ in range(_________):
p = 2 ____ i
print(pattern.format(_____ , ________))
>>> powers2(10)
2**0
=
1
2**1
=
2
2**2
=
4
2**3
=
8
2**4
=
16
2**5
=
32
2**6
=
64
2**7
=
128
2**8
=
256
2**9
=
512
3/6/2016
2**10
>>>
CS 110G Exam 2
=
1024
Page 4
3/6/2016
CS 110G Exam 2
Page 5
8. Fill in the blanks for the code to interactively enter a list of positive numbers, compute the sum, the average and
the largest number. End the interactive entry when the word ‘end’ is entered.
[16 pts]
num = input('enter number or "end" to quit')
total = ________
count = ________
max = 0
while num ______ 'end' :
number = ________(num) # allow decimal numbers
total = ________ + number
count = __________ + _____
if number > ______:
max = ___________
num = input(‘____________________________________________')
print("The sum of the numbers is",_________)
print("The average of the numbers is",________ ___ _________)
print("The maximum of the numbers is",_________)
Related documents