Study questions for Lecture Test 2 CS 115

These will not be collected. They are not guaranteed to be 100% of the test material. You can do these and discuss them with your TA or Dr. Keen.

1. Know the difference between function definitions and calls.
2. Know the correct way to call a value-returning function and to call a function that does not return a value.
3. What is unit testing?
4. Can you describe how the call stack changes as a function is called, and as a function returns?
5. Understand the difference between parameters and arguments. How do they work together? What has to match between them and what does not?
6. Trace this code:
```
def main():
v2 = 5
v3 = v2 * 2
v4 = v2 + v3
v1 = f1( v3, v4)
print ( v1, f1 (v2, v4))

def f1 (a, b):
return (a + b) / 2.0

main()
```
7. Write Python code to compute xn by using a for loop. Assume you have values in x and n. Do not use the exponentiation operator. Hint: use an accumulator.
8. Trace this code. How tall would the call stack be at its highest point?
```
def fun1 (p1):
print ("red")
return p1 + 3

def fun2 (p2):
print ("green")
return fun1(p2) * 3

def main():
print (fun2 (12))
print ("yellow")

main()
```
9. Write a for loop that would generate the output below:
```        0.0 - 0.1
0.1 - 0.2
0.2 - 0.3
0.3 - 0.4
etc.
0.9 - 1.0
```
10. Write some code where you use a for loop to input 10 numbers and report the average of only the POSITIVE ones. Include the initializations needed.
11. The graphics library uses objects and methods. Can you tell the difference between them? do you know the syntax for calling a method?
12. If you had the statement a.b(c) which one is the object? which one is the argument? which one is the method?
13. Describe the coordinate system for the graphics library. Where is the origin by default? Can you change where the origin is?
14. Describe the two different uses of the getMouse method in the graphics library.
15. Sketch what this code will draw (you do NOT have to be precise!) Assume it is part of a valid program.
```        wn = GraphWin()
tt = Point(50,50)
c2 = Circle (tt, 25)
c2.draw(wn)
```
16. Know the difference between logical and relational operators.
17. Put parentheses in the expression below so that they show what order the operations will be done in:
a < 0 and b >= 5 + y or z > 3 and q == p
18. Write an expression logically equivalent to not (a > b and c > d) which does not use the not operator. "logically equivalent" means that when one expression is true, the other is, and the same with false.
19. What is the difference in operation between a "=" operator and a "==" operator?
20. Write a design for this problem: read in three numbers and output the ones that are larger than their average. This should be done with pseudocode, NOT code! no Python at all, just English. Number the steps if you like.
21. Write the code for the design in the previous problem.
22. Given this code:
```def fun3 (x, y):
z = 5
x = 'a'
y = 3.5

def main ():
z = "abc"

```

if z is referred to in the main function, what type is it?
if z is referred to in the fun3 function, what type is it?
if x is referred to in the main function, what happens?
if y is referred to in fun3, what type is it?

23. What goes into a function's documentation? What is a pre-condition? What is a post-condition? What are the "three P's" ?
24. All positive integers below 10 that are multiples of 3 or 5 are 3, 5, 6, 9. The sum of these numbers is 23. Write some code to find the sum of all positive multiples of 3 or 5 below 1000.
25. Write a function that would accept two float parameters and return True if the two parameter values are within 0.01 of each other, and return False otherwise. Use ONE return statement.
26. Write a driver program for the function in the previous problem.
27. Fill in the blanks so that the code uses sentinel logic. The sentinel value is 999.
```        num = int(______________________)
while ______________________:
print(num)
num = int(input())
```
28. What is a driver? Why is it called "scaffolding"?
29. There is two inconsistencies in the code. Point them out.
```        def fun1 (a):
print(a)

def fun2 (b):
return b * b

def main ():
x = 5
if fun1 (x) > 3:
print ("ok")
fun2 (x)

```
30. __________________ are the placeholders that are filled in by copies of _______________________________ when the function is called.
31. Write a function that will have 3 parameters that are numbers. The function returns True if the numbers are in ascending order, False otherwise. Use one return statement. You don't have to use a loop for only 3 numbers. If you like, write one version using a loop and if's, and one using just if's.
32. What's a flag? What values does a flag have?
33. What causes an infinite loop?
34. What's the difference between a control structure and a data structure?
35. Name the basic control structures.
36. What's dead code?
37. What causes an infinite loop? Why is it difficult to write an infinite for loop in Python?
38. What are the guarantees true for EACH control structure? What is the guarantee that is true for ALL control structures?
39. Debugging what are the jobs of a debugger?
40. What is a breakpoint?
41. What is a call stack?
42. What does a "step over" button do for you in debugging? "step into"? "step out of"?
43. Trace this code: (fill in the table below)
```                n = 0                 # line 1
while n < 10:         # line 2
print(n, end=" ") # line 3
n = n + 2         # line 4
```
and so on
Line # n output
44. Trace this code: Fill out a table that shows line numbers executed, variables and output.
```
b = 7				# line 1
x = 5				# line 2
y = x * 15			# line 3
print(b, x, y)			# line 4
a = 3 * b + 9			# line 5
x = y + 3			# line 6
y = x				# line 7
print (b, x, a, y, end=" ")	# line 8
a = 5 * b			# line 9
print (a)			# line 10
```
45. Write some code that would input an integer number from the user which MUST be between 1 and 10, exclusive (not 1 and not 10). If the user does not input a number in that range, tell them so and ask for it again. Repeat until the number is in the right range.
46. Do the same problem as the previous one, but give the user no more than 3 chances to enter the number, then tell them the value 5 will be used and leave the loop.
47. What library do you import for random numbers?
48. What does the seed() function do?
49. What does randrange() give you in its 3 versions?
50. What does randint() do? what are its arguments?
51. What does random() return?
52. What does "deterministic" mean?
53. What is the output of this code with speed = 55?:
```        if (speed < 40)
print("slow")
elif (speed < 55)
print("legal")
else
print("ticket")

```
Write a test plan for this code.
54. How many times will each of these for loops repeat their body? What values will the variable i go through? What _type_ will i be?
1. for i in range(10):
2. for i in range(3, 10, 2):
3. for i in range(2, 5):
4. for i in [2,3,5,1,0]:
5. for i in "APPLE":
6. for i in range(10, 4, 3):
7. for i in range(5, 1, -1):
55. What are DeMorgan's Laws? what are they used for? Use them to simplify not ( num > 50 or ct == 8) "logically equivalent" means that when one expression is true, the other is, and the same with false.
56. What are the test cases you use for testing a loop? what are the test cases you use for testing an if/else?
57. Write some code that will use a flag to find out if ANY numbers input by the user are larger than 500. There will be 10 numbers input from the keyboard. The code should report afterward that "There were no numbers greater than 500" or "there was at least one number greater than 500". Is this an ANY or an ALL pattern for flags?
58. What are the "three P's"?
59. Write a program which will play a guessing game with the user. The user inputs an integer from 1-6 inclusive. The program generates a random number in the same range, 1 through 6 inclusive. If the number the user entered is the same as the random number, they win 3 times the number in dollars. If they don't match, they don't win anything. Write the program to run this guessing game 5 times. Keep a running total of the number of dollars they won and report it at the end.