To Lecture Notes

IT 212 -- Sep 16, 2020

Review Exercises

  1. What is the output?
    # x is a 2 byte binary number
    x = 0b0000000100000011
    print("{0:d}".format(x))
    
    Ans: 256 + 3 + 1 = 259.
  2. Which of these binary numbers is odd?
    a. 0000000100000000
    b. 0000000100000011
    c. 0101110101101000
    d. 0111010101010100
    
    Ans: b, which is the only answer that ends with 1.
    We know that
    even + even == even
    even + odd == odd
    
    All the bits of a 16-byte binary value, other than the last bit, represent an even power of two. Since the sum of even powers of two is always even, the last bit determines whether the entire number is even or odd.
  3. Interpret this binary number as (a) ASCII characters, (b) packed decimal, (c) answers to a true/false quiz, (d) a 2 byte decimal number.
    0100100001101001
    
    Ans: (a) The ASCII codes 01001000 and 01101001 represent 48 hex and 69 hex, respectively, which are the ascii codes for H and i = Hi.
    (b) The packed decimal value of the binary number is 4869.
    (c) FTFFTFFFFTTFTFFT
    (d) use the powers of 2 table:
    214 + 211 + 27 + 26 + 22 + 21 =
    16,384 + 2,048 + 64 + 32 + 8 + 1 = 18,537.
  4. Write a Python program to compute the bitwise and of the decimal values 13 and 6. Output the result in hex and decimal. Ans:
    a = 13
    b = 6
    c = a & b
    print("{0:02x} {1:d}".format(c, c))
    # Output: 04 4
    
  5. Consider this code fragment:
    value = 37.298
    print(round(value, 2))
    # Output:
    37.3
    
    How can we get it to print as 37.30?
    Ans: Use the str format method:
    value = 37.298
    print("{0:6.2f}".format(value)
    
  6. What is a standalone method? List as many Python built in standalone methods as you can. In particular, what do the standalone methods chr and ord do?
    Ans: See the Builtin Classes and Methods document. Here are the some of the standalone methods that we listed during class today.
    ord chr print type round len 
    input min max int str float 
    bool dict list  
    
  7. Write a standalone method named get_greeting that inputs a name, for example Larry, and returns a greeting using that name, for example Hello, Larry, how are you? Test your method like this (traditional hardcoded test script):
    greeting = get_greeting("Larry")
    print(greeting)
    
    # Ans:
    # Define create_greeting in the module (Python file) greeter.py.
    def create_greeting(the_name):
        return "Hello, " + the_name + ", how are you?"
    
    If the method is defined a different file named greeter.py, here are the traditional test statements to test the get_greeting method:
    from greeter import get_greeting
    greeting = createGreeting("Larry")
    print(greeting)
    
    // or
    
    from greeter import get_greeting
    print(get_greeting("Larry"))
    

Lab 1