CPSC 110 Fall 2011 Quiz 3 Practice Questions

    Instructions: The first three problems ask you to read a Pseudocode function definitions. Trace through each of them by hand, keeping track of the variables, to determine the answer to the question.

  1. What value would functionA(5) return?
    # Comment: A function definition that takes 1 parameter
    
    To functionA(a):
       Return: a + 100
    
  2. What value would functionB(3,5) return?
    # Comment: A function with 2 parameters
    
    To functionB(m,n):
      Set Var to m
      Set m to m + 1
      While M <= N do:
         Set Var to Var + m
         Set m to m + 1
      Return Var
    
  3. What value would mystery(3,2) return? What value would mystery(3,3) return?
    # Comment: A mystery function
    
    To mystery(x, y):
       Set M to x
       If y < x then do:
          Set M to y
       Return M
    




    Instructions: The next three problems ask you to define a function in Pseudocode. Model your answers after the Pseudocode algorithms in the first 3 questions.

  4. Define a function named add3 that will compute and return the sum of any three numbers.








  5. Define a function named average that will compute the average of the numbers from 1 to N, where N is a parameter.








  6. To compute 25 you can multiply 2 by itself 5 times. In general to compute 2n you can multiply 2 by itself n times. Write a function named powerOf2, which takes 1 parameter, n and computes 2n.











    Click here for the solutions. But no peeking until after you've tried the problems.