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. 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.
  2. 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
    
  3. 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.
  4. The graphics library uses objects and methods. Can you tell the difference between them? do you know the syntax for calling a method?
  5. If you had the statement a.b(c) which one is the object? which one is the argument? which one is the method?
  6. Describe the coordinate system for the graphics library. Where is the origin by default? Can you change where the origin is?
  7. Describe the two different uses of the getMouse method in the graphics library.
  8. 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)
    
  9. Know the difference between logical and relational operators.
  10. 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
  11. 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.
  12. What is the difference in operation between a "=" operator and a "==" operator?
  13. 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.
  14. Write the code for the design in the previous problem.
  15. 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.
  16. Fill in the blanks so that the code uses sentinel logic. The sentinel value is 999.
            num = int(______________________)
    	while ______________________:
                 print(num)
    	     num = int(input())
    
  17. What's a flag? What values does a flag have?
  18. What causes an infinite loop?
  19. What's the difference between a control structure and a data structure?
  20. Name the basic control structures.
  21. What's dead code?
  22. What causes an infinite loop? Why is it difficult to write an infinite for loop in Python?
  23. What are the guarantees true for EACH control structure? What is the guarantee that is true for ALL control structures?
  24. Debugging what are the jobs of a debugger?
  25. What is a breakpoint?
  26. What is a call stack?
  27. What does a "step over" button do for you in debugging? "step into"? "step out of"?
  28. 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
  29. 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
    
  30. 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.
  31. 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.
  32. What library do you import for random numbers?
  33. What does the seed() function do?
  34. What does randrange() give you in its 3 versions?
  35. What does randint() do? what are its arguments?
  36. What does random() return?
  37. What does "deterministic" mean?
  38. 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.
  39. 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):
  40. 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.
  41. What are the test cases you use for testing a loop? what are the test cases you use for testing an if/else?
  42. 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?
  43. What are the "three P's"?
  44. 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.
  45. What is an alias? What is the output of the code below?
        p1 = [1, 3, 5]
        p2 = p1
        p2[2] = 7
        print(p1)
    
  46. Describe a flag variable. What type should it be? why? List the three steps you do with a flag. What is the difference between the ANY pattern and the ALL pattern?
  47. Describe the "dot notation" and what it means. What kinds of objects did you see this semester? What kinds of methods?
  48. Why are each of these an infinite loop?
    a.      Answer = 'y'
            while Answer == 'y':
                n +=1
                ans = input("another loop? y/n")
    
    b.      i = n+1
            while i != n:
               i +=1
               print(i)
    
    
  49. Correct the logic errors in the code below.
       # Add numbers entered until sentinel -1 is encountered.
              n = int(input("enter a number"))
              sum = 0
              while n == -1:
                 sum = sum + n
              n = int(input("enter a number"))
    
    
  50. Which kind of loop do you want in these circumstances?
    1. You are writing a game where the user will guess numbers until they guess the one the computer picked at random.
    2. You are testing a student to see if they know the capitals of all 50 states. They only get one chance per state.
    3. You are drawing a standard-sized chessboard.
  51. What's the output?
    count = 1
    sum = 0
    for number in range(2):
         while count  > number:
              sum = sum + 4
              count = count - 1
    print(sum)
    
  52. What's the output?
    list3 = [0,0,0]
    for i in range(2):
        for j in range(2):
            if i == j:
                list3[i] = i-j
            else:
                list3[j] = j-i
    print(list3)