CMSC201 Midterm Exam Review
Spring 2012


Picture ID required for the MidTerm

This review list is meant to give you an idea of the types of questions and topics that will be covered. It is by no means an exhaustive list. The actual exam will possibly contain questions and/or coding problems similar to those in this review guide, IN ADDITION to questions and/or coding problems related to any material gone over in lecture, group exercises, labs, homework assignments, course lecture notes, etc.


True/False Questions

Circle TRUE or FALSE for each of the following questions.
  1. TRUE/FALSE
    In Unix, the command rm can be used to delete files.
  2. TRUE/FALSE
    In Unix, the mv command is used to move to a different directory.
  3. TRUE/FALSE
    The following command given at the Unix prompt python prog.py < input.dat is called redirection and causes the contents of the input.dat file to be used as input to the program.
  4. TRUE/FALSE
    Once a text file has been created with emacs or xemacs, it may only be editted with emacs or xemacs.
  5. TRUE/FALSE
    The most basic design pattern is Input, Process, Output.
  6. TRUE/FALSE
    10110 in binary is equivalent to 23 in decimal
  7. TRUE/FALSE
    The input() function is usually used to get string input from the user.
  8. TRUE/FALSE
    A function that returns None and is executed only for its effect is called a predicate function.
  9. TRUE/FALSE
    A predicate function returns True or False.
  10. TRUE/FALSE
    If n is a variable holding an integer value, the value of the expression n % y is 0 whenever n is not a multiple of y.
  11. TRUE/FALSE
    In python, you can have multiple functions with the same name as long as they take different numbers of parameters.
  12. TRUE/FALSE
    When a function is called the number of arguments passed should match the number of formal parameters given in the function header.
  13. TRUE/FALSE
    What is the value of the expression bool(2 * 6 - 5 + 8 % 5)
  14. TRUE/FALSE
    If the integer variables a and b are holding the values 0 and 1, respectively, what is the value of the following expression : (a or b ) ?
  15. TRUE/FALSE
    According to DeMorgan's law, not(x and y) is equivalent to (not x) and (not y)
  16. TRUE/FALSE
    If the variables a and b are holding the values 5 and 2, respectively, the expression
    a / b produces the result 2.5
  17. TRUE/FALSE
    A python program that prints three lines of output must contain three print statements.
  18. TRUE/ FALSE
    String and list subscripts always start at 0.
  19. TRUE/ FALSE
    The fourth character in a string named foo is foo[4].

Multiple Choice Questions

  1. What is the value of the expression 12 - 10 - 8 * 6 % 4 + 2 % 1
  2. What is the output of the following code fragment?
        a = 3 
        b = 17
    
        a = b % a
        a += 1 
        b = a + 5 
    
        print "a = %d, b = %d\n" % (a, b)
    
  3. Execution of a break statement in the body of a while loop
  4. Determine the output of the code fragment.
        a = 4
        b = 3
        c = 1
    
        if a < b + 1:
            print "Stop and smell the roses"
        elif a > b + c:
            print "Type A personality"
        elif a % b >= c:
            print "What doesn't kill me, makes me stronger"
        else:
            print "I sense a lot of anxiety in this room."
    
  5. What is the effect of the following code?
        for i in range(1, 16):
            if i % 3 == 0:
                print i
    
  6. The statement print "%-30s" % ("Dan")

Short Answers

In the following questions, no syntax errors have been put in deliberately. (So, "there is a syntax error" is NOT the right answer and will receive no credit.) Please mark your final answer clearly.
  1. What is the difference between a high-level programming language and a low-level one and give one example of each?
  2. What is the Unix command to list all the files in the current directory?
  3. Assuming a file named foo.py exists in the current directory, what is the Unix command to rename foo.py to bar.py?
  4. What is the Unix redirection command that will place the output from foo.py into a file called output.txt ?
  5. How would the number 36 be represented in binary?
  6. What would the following code output?
        str = "Hello Class"
    
        print str[6]
        print str[3:]
        print str[7:11]
        print str[-1]   
    
  7. What is the index of the last character in a string of length, length ?
  8. Under what conditions will this code fragment print "water" ?
                if temp < 32:
                    print "ice"
                elif temp < 212:
                    print "water"
                else:
                    print "steam"
    
  9. Write a python statement that will print 1.234 in a 9-digit field with leading zeros.
  10. Write a python code fragment using a for loop that prints the numbers 1 to 5 side-by-side on the same line with 4 spaces between each number.
  11. Write a python code fragment using a while loop that prints the even numbers between 1 and 9, inclusive, side-by-side on the same line with 5 spaces between each number.
  12. Write a single python statement that will open a file named foo.txt for reading.
  13. What is the output of the following program? Don't worry about the exact number of spaces to appear before or after any numeric output. If the program goes into an infinite loop, you need show only the first five lines of output. Assume that the program as shown runs.
    
    def mickey (x, y):
    
        z = x - y
    
        print "mickey: x = %d, y = %d, z = %d" % (x, y, z)
    
        return z
    
    
    def mouse (c):
    
        a = 1
        b = c * 2 + a 
        a = b + 5
        c = mickey (a, b)
    
        print "mouse: a = %d, b = %d, c = %d" % (a, b, c)
    
        return b
    
    
    def main():
    
        a = 2
        b = 3
        c = 3
    
        a = mickey (b + 5, c)
        b = mouse (c)
    
        print "main: a = %d, b = %d, c = %d" % (a, b, c)
    
    
    main()
    
    
  14. This question tests your knowledge of nested for loops. What is the output of the following code ?
        for i in range(4):
            for j in range(5):
                if i + 1 == j or j + i == 4:
                    print "+",
                else:
                    print "o",
            print
    
  15. In the nested for loop code above, how many times is the condition of the if clause evaluated ?
  16. Write a function called average that takes three integer parameters n1, n2, and n3 and returns the value of the average of the three as a float.
  17. Write a function called numDigits that has a single formal parameter of type int and returns the number of digits in that integer. You may NOT change the integer to a string.
  18. Write a predicate function called isEven that takes an integer, n, as its single argument and returns True if n is even and False if n is not even.
  19. Fill in the blanks below. The function findNet computes a worker's weekly net pay given hours (an integer), payRate (a float), and exempt (a boolean) as input parameters. The net pay for the worker should be returned as a float. If the worker turns in more than forty hours and the value of exempt is False, then pay is 1 1/2 times the normal rate ("time and a half") for any hours worked over forty. If gross pay exceeds $1000, subtract 33% for taxes. Otherwise, subtract 25% for taxes. Include a statement to print the worker's hours and net pay for the week. (The net pay will be in dollars and cents, so choose the output format accordingly. The number of underscore characters shown is not necessarily an indication of the length of the correct response.)
    
    def findNet ( hours, ________, _______ ):
    
        STANDARD_HOURS = 40
    
        TAX_BRACKET_AMT = 1000
    
        HIGH_TAX_RATE = ________
    
        LOW_TAX_RATE = ________
    
    
        if _______ > STANDARD_HOURS _____  not exempt:
    
            extraHours = hours - STANDARD_HOURS
    
            grossPay = STANDARD_HOURS * _______  + extraHours * payRate * ______
    
        else:
    
            grossPay = hours * _______ 
    
    
        if grossPay > _____________ :
    
            netPay = grossPay - grossPay * _______ 
        
    
        _________
    
            netPay = grossPay - grossPay * _______ 
    
    
    
        ________ "Hours = %d and Net Pay = _______" % (hours, netPay)
    
    
        ________  __________ 
    
    
    

    Determine the output generated by the following code segments, assuming the surrounding program code (whatever it is) compiles correctly.

  20.     a = 2
        b = 3
    
        b *= a
        a += b
    
        print "%d %d" % (a, b)
    
  21.     m = 5
        n = 8
    
        print "%d %d" % (m % n, n % m)
    
  22. def f(x):
    
        print x,
    
    def g(x):
    
        f(x + 1)
    
        print x,
    
    
    def main():
    
        g(4)
        f(5)
        g(6)
    
    
    main()
    
  23.  
    
    def x(m):
    
        print "x: %d" % (m)
    
        return m + 2
    
    
    def y (n):
    
        print "y: %d  %d" % (n, x(n))
    
        return n * 3
    
    
    def main():
    
        r = x(12)
        s = y(7)
    
        print "main: %d  %d" % (r, s)
    
    
    main()
    
  24.     x = 1
    
        if x > 3:
            if x > 4:
                print "A",
            else:
                print "B",
        elif x < 2:
            if (x != 0):
                print "C",
    
        print "D"
    
  25. 
        for g in range (1, 4):
            for h in range (3):
                if g == h:
                    print "O",
                else:
                    print "X",
            print
    
  26. 
    def f1(a, b):
    
        print "f1:  %d  %d" % (a, b)
    
        return a
    
    
    def f2(b, c):
    
        print "f2:  %d  %d" % (b, c)
    
        return c
    
    
    def main():
    
        a = 2
        b = 5
        c = 7
    
        c = f1(a, b)
        print "main:  %d  %d  %d" % (a, b, c)
    
        b = f2(a, c)
        print "main:  %d  %d  %d" % (a, b, c)
    
    
    main()