DOC PREVIEW
USF CS 110 - CS 110 Midterm 1

This preview shows page 1-2 out of 7 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Intro to CS I NameMidterm 1, Fall 2011Show Your Work! The point values for each problem are in square brackets. There are 35 pointspossible.1. Bob has written a Python function called weeb. It has the following header:def weeb(x):Is the following call to weeb legal (i.e., will the Python interpreter allow it)?weeb("hello")[1 point]This is legal. For example,def weeb(x):print xweeb("hello")In fact, even if the weeb function needs the type of x to be something different from str, theinterpreter won’t object to the call. For example, in the codedef weeb(x):print x+1weeb("hello")the interpreter doesn’t diagnose an error until it encounters the print statement.2. Bob is writing a Python function named Bleeb. When bleeb is called it should be passedtwo ints and a float, in this order. bleeb returns a float. Write a header (the first line) forbleeb. [2 points]def bleeb(int1, int2, float1):The names of the parameters don’t matter. However, there must be three parameters.3. A Python program contains the following assignments.p = 9q = 2r = 2.0Find the value of each of the following expressions. (For example, the value of p - q is 7.)[6](a) p % q = 1(b) p/q = 4(c) p/r = 4.5(d) p/q*r = 8.0(e) p > q = True(f) p > q or r < 1.0 = True14. Interpreters and compilers are both programs that translate high-level language programsinto machine language. In two sentences or less, what is the main difference between them?[2]Interpreters translate one statement at a time. After translating a statement, an interpreterexecutes the translated statement. Compilers translate an entire program, but don’t executeany of it: execution of statements comes after compilation.5. The following program is supposed to read 2 floats, find the square root of their product, andprint the result. While the basic design is correct, it contains at least 4 errors that will bedetected by the Python interpreter. Find and correct 3 of them. [3]from math import sqrt# Get rid of ,zdef root_product(x, y, z):result = sqrt(x*y)return resultfirst = float(raw_input("Please enter a positive real number\n"))# Add a close-parenthesis to the end of the statementsecond = float(raw_input("Please enter a positive real number\n")# Don’t indent: "result" should be in line with "second"result = root_product(first, second)# Add a quote before the commaprint "The square root of their product is, result2Figure 1: Trace of Program in Problem 66. What is the output of the following program when it is run? I n other words, what will beprinted on the screen? [4]def func(x, y, z):print x, y, zp = 2*xq = y-4r = z+1print p, q, rreturn ra = 4b = 5c = 2print a, b, cc = func(a, b, c)print a, b, cFigure 1 shows a trace of the execution of the program. Here’s the output:4 5 24 5 28 1 34 5 33Figure 2: Trace of Program in Problem 77. What is the output of the following program when it is run? [3]def blunc(a, b):if a >= b:c = a - belse:c = b - areturn cx = 3y = 8z = blunc(x, y)print x, y, zFigure 2 shows a trace of the execution of the program. Here’s the output:3 8 54Figure 3: Trace of Program in Problem 88. What is the output of the following program when it is run? [4]def dunk(a, b):while a < b:a = 2*areturn ax = 3y = 10z = dunk(x, y)print x, y, zFigure 3 shows a trace of the execution of the program. Here’s the output:3 10 1259. Write a Python function called bigger. The function is passed two floats. It should printthe value of the larger of the two floats (it can print either if they’re equal). It should alsoreturn the value 1 if the first float is greater than or equal to the second. If the second isbigger than the first, it should return 0. For example, Bigger(23.5, 16.1) should print 23.5and return 1. The function header isdef bigger(first, second):Note that the function does not read in any data. It is not necessary for you to commentyour code. [5]def bigger(first, second):if first >= second:print firstreturn 1else:print secondreturn 0610. Write a Python program that prompts the user to input a series of ints, one per input line.End of input is indicated by an int that’s less than or equal to 0. The program finds theproduct of the positive ints and prints it. If there are no positive ints entered, the programprints the value 1.For example, if the user enters2350then the program will print 30. [5]val = int(raw_input("Enter an int (<= 0 to stop)\n "))prod = 1while val > 0:prod = prod*valval = int(raw_input("Enter an int (<= 0 to stop)\n "))print "The product is",


View Full Document

USF CS 110 - CS 110 Midterm 1

Download CS 110 Midterm 1
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view CS 110 Midterm 1 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view CS 110 Midterm 1 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?