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 right 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
  7. Given the strings st1 = "12cAACC" and st2 = "John Smith" know what type is returned for these operators and functions as well as what value
    1. len(st1)
    2. st2.find(st1)
    3. st2.find("h")
    4. st2.split()
    5. st1.split("c")
    6. st1 + st2
    7. st1[5]
    8. st1[:4]
  8. "Strings are immutable". What does that mean?
  9. 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.
  10. 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")
  11. 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
            0.9 - 1.0
  12. 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.
  13. The graphics library uses objects and methods. Can you tell the difference between them? do you know the syntax for calling a method?
  14. If you had the statement a.b(c) which one is the object? which one is the argument? which one is the method?
  15. Describe the coordinate system for the graphics library. Where is the origin by default? Can you change where the origin is?
  16. 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)
  17. Know the difference between logical and relational operators.
  18. 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
  19. 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.
  20. What is the difference in operation between a "=" operator and a "==" operator?
  21. 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.
  22. Write the code for the design in the previous problem.
  23. 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?

  24. What goes into a function's documentation? What is a pre-condition? What is a post-condition? What are the "three P's" ?
  25. 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.
  26. 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.
  27. Write a driver program for the function in the previous problem.
  28. Fill in the blanks so that the code uses sentinel logic. The sentinel value is 999.
            num = int(______________________)
    	while ______________________:
    	     num = int(input())
  29. What is a driver? Why is it called "scaffolding"?
  30. There is two inconsistencies in the code. Point them out.
            def fun1 (a):
            def fun2 (b):
    		return b * b
            def main ():
    		x = 5
                    if fun1 (x) > 3:
                            print ("ok")
                    fun2 (x)
  31. __________________ are the placeholders that are filled in by copies of _______________________________ when the function is called.
  32. 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.
  33. What's a flag? What values does a flag have?
  34. What causes an infinite loop?
  35. What's the difference between a control structure and a data structure?
  36. Name the basic control structures.
  37. What's dead code?
  38. What causes an infinite loop?
  39. What are the guarantees true for EACH control structure? What is the guarantee that is true for ALL control structures?
  40. Debugging what are the jobs of a debugger?
  41. What is a breakpoint?
  42. What is a call stack?
  43. What does a "step over" button do for you in debugging? "step into"? "step out of"?
  44. 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
  45. 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
  46. 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.
  47. 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.
  48. What is the output of this code with speed = 55?:
            if (speed < 40)
            elif (speed < 55)
    Write a test plan for this code.
  49. 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):
  50. 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.
  51. What are the test cases you use for testing a loop? what are the test cases you use for testing an if/else?
  52. 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?
  53. What are the "three P's"?
  54. What are the "three S's"?
  55. 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.