CPSC 110 Fall 2011 Quiz 2 Practice Questions

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

  1. Suppose you have a global variables named Var and N. What value would Var have after the following Pseudocode segment?
    Set Var to 100
    Set N to 3
    Set Var to Var - N
    If Var < 97 then do:
       Set Var to 0
    else-do:
       Set Var to 1
    
  2. What values would Var and N have after the following Pseudocode segment?
    Set Var to 1
    Set N to 0
    While Var < 16 do:
       Set Var to Var * 2
       Set N to N + 1
    
  3. Consider the following procedure. What value would Var have after the DoSomethingInteresting procedure is called?
    # Comment: A mystery procedure in Pseudocode
    
    To DoSomethingInteresting
      Set Var to 0
      Set N to 1
      While N < 10 do:
          Set Var to Var + N
          Set N to N + 2
    




    Instructions: The next three problems ask you to write a Pseudocode algorithm. Model your answers after the Pseudocode algorithms in the first there questions.

  4. Suppose you have the global variables A, B, and C. Write a Pseudocode algorithm that will add them together and store the sum in the variable Var








  5. Suppose you have a variable N and a variable M. Write an if/else algorithm that tests whether N and M are equal. If they are, set the variable Var to 1. Otherwise set it to 0.








  6. Suppose you have variables named Var and N. Write a pseudocode loop algorithm that will use these two variables to multiply Var by itself 10 times. For example, if Var is 2, then your algorithm will multiply 2 * 2 * 2 ... * 2, 10 times, giving 1024.(HINT: You may need additional variable(s) for this one.)











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