Sample Exam 1

Python Programming
Chapters 1 through 4


Test One - Thursday, October 15, 2015 @ 9:30 AM

VIP: Be sure to look at and use TEST ONE STUDY GUIDE too!

VIP: Be sure to take exam one again: Oct 15th actual TEST ONE. Then look at your graded test #1 to see if you made the same mistakes. Check using IDLE and using Python .py programs and using the textbook. The final is comprehensive.


Instructions



Problems

  1. Write numeric expressions in Python for these value:
    1.      4 + 3 * 2n-2
           ------------
                10     
      
    2. the sum of the cubes of integers x and y
    3. the number of seconds in 4 hours, 14 minutes, and 32 seconds
      (There are sixty seconds in a minute, and sixty minutes in an hour.)

  2. Write boolean expressions in Python for these conditions:
    1. x is a factor of y (that is, x divides evenly into y)
    2. age is at least 18 and state equals 'Iowa'
    3. the string name contains a 'z'

  3. Consider this code:
        if x % 2 == 1:
            if x**3 != 27:
                x = x + 4
            else:
                x = x / 1.5
        else:
            if x <= 10:
                x = x * 2
            else:
                x = x - 2
        print(x)
    

    What does this code print if x == 8?

    What does this code print if x == 5?


  4. The user enters the birthdays of, a and b, two people who were born on different days in the same year. Write Python code that prints a is younger if a's birthday comes before b's, and b is younger if b's birthday comes before a's.
        a_month = int( input('Enter the month: ') )
        a_day   = int( input('Enter the day  : ') )
        b_month = int( input('Enter the month: ') )
        b_day   = int( input('Enter the day  : ') )
    
        # fill in the blank
    

  5. Answer briefly each of these questions about algorithms and programs:
    1. In what ways is an algorithm similar to a program?
    2. In what ways is an algorithm different from a program?
    3. What are the first three steps of the problem solving/programming process?

  6. Consider this code:
        x = input('Enter a string: ')
        y = 0
        for i in x:
            print(y, i)
            y += 1
    

    What does this code print if the user enters 'Felix'?


  7. Write a Python loop to get the sum of all the odd numbers between 1 to n, for a given value of n. For example, the sum of the odds between 1 and 7 is 1 + 3 + 5 + 7 = 16.

    ... or if n = 11 or n = 12, the output would be 36, since 1 + 3 + 5 + 7 + 9 + 11 = 36.


  8. Consider this code:
        x = input('Enter a string: ')
        y = 0
        for i in x:
            if i == 'a':
                y += 1
        print(y)
    

    Write a while loop that does exactly the same thing as the for loop.


  9. Answer briefly each of these questions about loops:
    1. What does the break statement do in a loop?
    2. What is a loop and a half?
    3. What must the body of a while loop do that the body of a for loop does not need to do?

  10. Suppose we want to censor a string in this way:
        Enter the source     : Ghostbusters
        Enter char to replace: s
        Enter char to insert : *
    
        Result: Gho*tbu*ter*
    

    Write Python code that prints out the characters in string str, only with character old_ch replaced with character new_ch.

        str    = input('Enter the source     : ')
        old_ch = input('Enter char to replace: ')
        new_ch = input('Enter char to insert : ')
    
        # fill in the blank
    
        print('\nResult:', result)
    

  11. Suppose we want to know the sum and the product of the digits in an integer:
        Enter an integer     : 357
    
        Result: 357 digit sum is 15 and digit product is 105
    
        Enter an integer     : 123456
    
        Result: 123456 digit sum is 21 and digit product is 720
    
    

    Write the Python code that obtains the sum of and the product of the digits in number.
    The first statement and the last statement of the program are given for you.

        number  = input('Enter an integer     : ')
    
        # fill in the blank
    
        print('\n', number, "digit sum is", digitSum, "and digit product is", digitProduct)
    

  12. Convert the following numbers from one base to another base:
    VIP Note: You MUST show your work and process of arriving at the answer.
                   Calculators are not allowed on the exam. Another reason to show your work!

    1. Decimal base ten number 81 is what in binary (bsse two).
    2. Base ten 357 in binary is:
    3. The binary number 11001010 is what value in base ten decimal?
    4. The binary number 11001010 is what value in base sixteen hexadecimal?



VIP: Be sure to look at and use TEST ONE STUDY GUIDE too!