To Lecture Notes

IT 212 -- Sep 14, 2020

Review Exercises

  1. Zoom Poll 1 Review Exercises questions and answers. Correct answers are marked with *:
    1. For questions 1 to ?, which of the following are legal Python local variable names? Recall that a variable name must start with a lower case letter or underscore. The remaining characters must be letters, digits, or underscores.
      The python variable name is total_value
      *a. Legal    b. Not legal
    2. total value
      a. Legal    *b. Not legal
      Explanation: no embedded blanks allowed.
    3. totalValue
      *a. Legal    b. Not legal
      Explanation: Camel casing is allowed in Python, but the class examples will use underscore notation.
    4. TotalValue
      a. Legal    *b. Not legal
      Explanation: Local variable names must start with a lower case letter. User defined class names start with an uppercase letter, so TotalValue would be a good class name.
    5. total value
      a. Legal    *b. Not legal
      Explanation: an embedded minus sign or dash is not one of the allowed characters for a Python variable name.
    6. i2
      *a. Legal    b. Not legal
      Explanation: i2 starts with a letter and the digit 2 is allowed for the second character.
    7. 2i
      a. Legal    *b. Not legal
      Explanation: A digit is not allowed as the first character of a variable name.
    8. hook&ladder
      a. Legal    *b. Not legal
      Explanation: & is not allowed in a Python variable name.
    9. _
      a. Legal    *b. Not legal
      Explanation: A single underscore is allowed for a variable name but is not recommended because it is hard to read.
    10. an_extremely_long_identifier_if_you_ask_me
      *a. Legal    b. Not legal
      Explanation: Although variable names of any length are legal, variable names longer than 10 or 12 characters are hard to read.
  2. Zoom Poll 2 Review Exercises questions and answers:
    1. What is the output of this script?
      val1 = 4
      val2 = 1.237
      val3 = "dog"
      print("{0:4d} {1:5.2f} {2:s}".format( 
          val1, val2, str(val1 + val2) + val3))
      
       *a. 4 1.24 41.237dog
        b. 4 1.24 5.237dog
        c. 4 1.24 41.237 dog
        d. 4 1.24 5.237 dog
    2. What is the output of this script?
      n = 13
      print("{0:03d} {1:x} {2:02x} {3:08b}".format( _
          n, n, n, n)
      
      a. 00001101 0d d 013     b. d 013 00001101 0d
      c. 0d 013 d 00001101      *d. 013 d 0d 00001101
    3. What is a bit?
        a. One of the bool values True or False.
      *b. A binary digit.
        c. A decimal digit.
        d. A base 64 digit.
    4. What is a byte?
        a. Two binary digits         b. Four binary digits
        c. Six binary digits         *d. Eight binary digits
    5. How many bits are required to represent a hex digit?
      a. 2     *b. 4    c. 6     d. 8
    6. Convert the binary value 01101111 to hex and decimal.
      a. 6d 109 b. 6f 109 *c. 6f 111 d. 615 111
    7. Convert the hex value 7a to binary and decimal.
      *a. 01111010 122     b. 11101010 122
        c. 01111000 710     d. 11101000 710
    8. Convert the decimal value 117 to binary and hex.
        a. 01010101 55       b. 01010111 57
      *c. 01110101 75       d. 01110111 77
  3. Write a script that creates this list:
    25  81  95  15
    
    a. a = 25 81 95 15          b. a = 25, 81, 95, 15
    c. a = [25 81 95 15]     *d. a = [25, 81, 95, 15]
  4. Write Python scripts to verify your answers for the review exercises 2f, 2g, and 2h. Ans:
    # Problem 6
    a = 0b01101101
    print("{0:d} {1:02x}".format(a, a))
    
    # Problem 7
    b = 0x6d
    print("{0:08b} {1:d}".format(b, b))
    
    # Problem 8
    c = 117
    print("{0:08b} {1:02x}".format(c, c))
    
    # Output:
    01110101 75
    109 6d
    01101101 109
    
    # You can also omit the argument numbers like this:
    print("{:d} {:02x}".format(a, a))
    print("{:08b} {:d}".format(b, b))
    print("{:08b} {:02x}".format(c, c))
    

Bitwise Operations

Project 1

Traditional Testing