CPSC 110-08: Computing on Mobile Phones
Spring 2012

Solutions to Quiz 2 Practice Questions

This set of questions is about number conversions. You might want to look back at this homework and this lecture. You might also want to practice on these Khan exercises:

  1. Convert the binary value 1001 1010 to decimal. Show your work.

    1 * 21 + 1 * 23 + 1 * 24 + 1 * 27 = 2 + 8 + 16 + 128 = 154.

  2. Convert the hexadecimal value 5A to decimal. Show your work.

    10 * 16 0 + 5 * 161 = 10 + 80 = 90.

  3. Convert the decimal value 135 to binary. Show your work.

    135 - 128(27) = 7 - 4(22 = 3 - 2(21) = 1 - 1(20) = 0
    So the binary value has 1s in the 128s, 4s, 2s, and 1s places : 1000 0111

  4. Convert the decimal value 135 to hexadecimal. Show your work.

    135 / 16 = 8 remainder 7, 7 / 16 = 0 remainder 7. So 135 = 8716.

Instructions: The next two problems ask you to read Pseudocode or App Inventor algorithm. Trace through each of them by hand, keeping track of the variables, to determine the answer to the questions.

  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
    
    Initially Var is 100 and N is 3. Var is then set to 97 (Var-N). The if statement tests whether Var is less than 97 (< 97). It is not so Var is set to 1.

  2. Consider the App Inventor procedure on the right. Suppose the procedure was called 100 times. Describe as specifically as you can in 1-2 sentences what the procedure would do.

    If this procedure is called 100 times, it would "speak" Red approximately 1/4 of the time, so around 25 times. It generates a random number between 1 and 4 inclusive so you would expect 1 to come up about 25 per cent of the time.

Instructions: The next problem asks you to write a Pseudocode algorithm. Model your answers after the Pseudocode algorithm in question 5.

  1. 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.
    if N = M then do:
       Set global Var to 1
    else do:
       Set global Var to 0