reec06 review for Test ONE Fall09Print your name legibly:

circle your last name

Sign:

Poly-ID:

REVISED

For the following questions, use these variable definitions.

a = 4444

b = 2

c = 11.11

d = "hello"

What is the value and type of each of the following expressions or, if it won't compile, circle that answer

typevaluecircle if will not compile

1. (2 pts)a// b / c___float______200.0____ will not compile

2. (2 pts)b + 6 % b____int______2______will not compile

3. (2 pts)a / d______will not compile

4. (2 pts)math.ceil(c) + 1______float______13.0_____ will not compile

5. (2 pts)d * 0_____str______''_____ will not compile

6. (2 pts)str(d)______'hello'_____ will not compile

7. (2 pts)
math.sqrt( b = float(c) )______will not compile

8. (2 pts) Circle the letters of ALL correct answers. NameError

a) is an exception

b) is run time error

c) is a compile time error

d) cannot happen once a program passed compilation without an error

9. (6 pts) What is the output when these statements are executed and the user hits these keys: 3 ENTER

WRITE ANSWERS TO THESE QUESTIONS IN THIS DOCUMENTpage 1 of 10 pages (counting the cover sheet)

reec06 review for Test ONE Fall09Print your name legibly:

circle your last name

Sign:

Poly-ID:

x = int( raw_input( "Enter a digit: " ) )

print x, str(x)*(x // 3)

print x - 1

print x

Write the output here
OR
if there will be a runtime error, state that here.

3 3

2

3

WRITE ANSWERS TO THESE QUESTIONS IN THIS DOCUMENTpage 1 of 10 pages (counting the cover sheet)

reec06 review for Test ONE Fall09Print your name legibly:

circle your last name

Sign:

Poly-ID:

10. (10 pts ) Draw arrows to show which values the variables refer to after the

statements are executed

Show the numbers in RAM also.
(It doesn't matter where the are in the program.)

WRITE ANSWERS TO THESE QUESTIONS IN THIS DOCUMENTpage 1 of 9 pages (counting the cover sheet)

reec06 review for Test ONE Fall09Print your name legibly:

circle your last name

Sign:

Poly-ID:

1. x = 3

2. y = 4

3. y += x

4. x = y

Draw the arrow showing

wherex refers to after line 1.

x

Draw the arrow showing
where y refers after line 2.

y

Draw the arrow showing
where y refers after line 3.

y

Draw the arrow showing
where x refers after line 4.

x

Write the values that will be in RAM

.

.

.

3
4
7

.

.

.

WRITE ANSWERS TO THESE QUESTIONS IN THIS DOCUMENTpage 1 of 9 pages (counting the cover sheet)

reec06 review for Test ONE Fall09Print your name legibly:

circle your last name

Sign:

Poly-ID:

True False Circle the letter of your one answer

11. (5 pts ) This will compile:

floatVar = float( "one hundred and two point seven" )

TrueFalse

(it WILL compile but it will raise a runtime exception because the string cannot be cast to a float)

Trying it in the SnakePit won't let you do compilation by itself: ALT-X is used to test a .py file for syntax errors. F5 says to run the file (actually it says to compile and then run).

12. (5 pts ) Given that a module we want to useis in a file named ralph.py, this will run:

import ralph.py

TrueFalse

13. (5 pts ) There is a function named rrrrrin the hmmm module that takes one parameters and returns a strand we have successfully imported hmmm and the math module. This will compile:

printmath.sqrt( rrrrr(math.sqrt() ) )

TrueFalse

NOTE: the return types of functions are not known until runtime.
The question is about compilation and there are no compilation erros.
At runtime, the check for matchingquantities of parameters happens.
At runtime type checking happens because in python type doesn't happen until runtime!
Also at runtime, there will be an AttributeError because the programmer didn't use hmmm.rrrrr
(unless by some weird situation, a function named rrrrrhad been defined outside any module!)
Well there are lots of runtime errors that will be noted (probably not all during one attempt to run) but there are no compilation errors.

14. (3 pts ) There is no way to display a number in hexadecimal notation.

TrueFalse

There is the hex() BIF that returns a str representing the way the numeric value passed to it would look when interpreted in hexadecimal notation. Recall that hexadecimal, octal, binary and decimal as just notations – the same number displsyed in any of these notations is still the same number.

15. (3 pts ) A test is not a statement.

TrueFalse

bool expressions are part of the syntax of some statements:
if, if-else, it-elif-elif-…-elif amd if-elif-elif-elif-…-else

Short Answer Questions – NOTE: these are probably harder than the ones on the test

16. (5 pts )

How many programmers does it take to call a function?

2

What are their names?

the caller and the one who designs or builds or creates or writes the function def.

Which can break the subproblem into more subparts? How?

ONLY the one who designer or builds ot creates or write the function def.

Then how many programmers are involved?

The original 2 + however many function calls need to be made to solve the subproblems

17. (5 pts )The run time system does what when it first encounters these lines of code (what happens during the compilation phase)?

def meme( prammy, sammyt ):

return 2 ** (prammy + sammy)

places the name meme in the global namespace (G)

places the names prammy and sammy in the local namespace (L) associated with the name meme..
(It also compiles the code to machine code, stores it in RAM and makes the name meme refer to that code)

18. (15 pts ) There is a builtin in function you have never used before named oblatt.

Here is a SnakePit session showing the help for that function.

IDLE 2.7.1

> help( oblatt )

Help on built-in function oblatt in module __builtin__:

oblatt(...)

oblatt(object, object) -> float, float

Return these two values:
1st parameter raised to the power of the 2nd parameter
and
2nd parameter raised to the power of the 1st parameter

Obviously you cannot define this function because it is built in.

Your job is to complete thefollowing function by writing the needed parameters and python statements that
do what the docstring for drawWeirdString says it will do.
Even though it's not the best, your function MUST use the oblatt builtin function

You do not need to show any testing for your function.

Do NOT write a main function (obviously).

Do NOT write a complete program (obviously).

ONLY complete the definition for drawWeirdString (don't forget the parameters!)

def drawWeirdString( first, second ):

''' draw a string that is the string made of the value of first raised to

the power of second repeated second rasied to the power of irst

times.

E.g.: If the calls passes in 2 and 3, the string will be: '888888888\n'

2^3 is 8 and 3^2 is 9 so there are 9 '8' characters drawn

'''

pow1, pow2 = oblatt( first, second )

print str( pow1 ) * pow2

19. (20 pts ) Show that you can break a problem into its parts by writing only the main function.

Write ONLY variable definitions and function calls – we will hire professional python programmers to create the modules you'll use that will have the definitions for all the functions you need to solve the whole problem. (Of course they have to be able to know what to write based only on your good names for the functions they will write.)

Do NOT write any function defs except main.

Do NOT write a complete program, just import needed modules and define main

Whenever you need to call a function to solve a subproblem, choose a good namefor the module you want it in and choose the name you want it to be called, and call it.

We will hire someone to define the module with the function in it for you (and it will work perfectly!).

Yes, you can have local variables to solve the problem.
You may also need to create global constants.

You may assume that any of the functions will have default parameter values but only if there are meaningful default values that the the programmer designing the module for you would consider when doing good design.

You have not requested any default parameters values be provided.

When the other programmers have finished writing the definitions of the functions in the modules they will provide, your main function should solve the following problem:

Get a name (last, first and middle initial) from a database of names.
Display a pentagon made out of the letter Q.
Display a hexagon made of the middle initial
Display a solid line made of the middle initial
Display a octagon made of the letter Q

You may NOT write any calls to raw_input or printin main.

Notice that the letter Q is used more than once as the letter value used in displaying shapes.

import databaseOfNames

import shapeySutff

import lineyStuff

def main( ):

lastName, firstName, initial = databaseOfNames.getName()

shapeySutff.drawPentagon( 'Q' )

drawnHexagon( initial )

lineyStuff.drawnLine( initial )

shapeySutff.drawnOCtagon('Q' )

WRITE ANSWERS TO THESE QUESTIONS IN THE BLUE BOOKpage 1 of 10 pages (counting the cover sheet)

WRITE YOUR ANSWER TO THIS QUESTION IN THE BLUE BOOK

If you need "scratch" paper, use the Blue Book but cross out anything you do not want graded.

WRITE ANSWERS TO THESE QUESTIONS IN THE BLUE BOOKpage 1 of 10 pages (counting the cover sheet)

WRITE YOUR ANSWER TO THIS QUESTION IN THE BLUE BOOK

If you need "scratch" paper, use the Blue Book but cross out anything you do not want graded.

PUT YOUR ANSWERS TO THIS QUESTION IN THE BLUE BOOK

rec06 - BLUE BOOK QUESTIONs

NOTE these are not programs but we had to put these questions somewhere.
On the actual midterm you might get a real problem to solve.

All functions MUST have docstrings (of course – they help you focus on solving the problem!).

20. (20 pts)
Define a module which will contain functions that work with linesmade of characters.

You must provide (at least) these two functions in the module:

--- One of the required functions will use three characters.
The length of the line should be randomly determined.

The caller should be able to give 1, 2 or 3 characters to be used in drawing the line.
When the caller doesn't provide parameters, the function should use 'X', 'Y' and 'Z' as the default values.

The first part of the line is drawn using the first parameter which is printed between 3 and 17 times, the actual quantity is randomly determined.
The second part of the line is drawn using the second parameter which is printed between 5 and 22 times, the actual quantity is randomly determined.
The third part of the line is drawn using the third parameter which is printed between 2 and 14 times, the actual quantity is randomly determined.

---- The second required function will return a line of characters exactly like the line that was drawn in the first required function in this module.

Be sure to correctly document your module so that other programmes can use dir() and help() on it.

Here are some sample calls to the first required function (here named fbut that's a horrible name, so don't use it!).
We have not provided any samples runs for the other function.

lineyStuff.f( 'A', 'B', 'C' )

AAAAAAAABBBBBBBBBBBBBBBCCC

lineyStuff.f( 'A', 'B', 'C' )

AAAAAAAAAAAABBBBBBBBBBBBBCCCCCCCC

lineyStuff.f( 'A', 'B' )

AAAAAAABBBBBBBBBBBBBBBBZZZZZZ

lineyStuff.f( 'A' )

AAAAAAAYYYYYYYYYYYYYYYYZZZZ

lineyStuff.f( )

XXXYYYYYYYYYYYYYYZZZZZZZZZZZZZZ

THIS WOULD BE WRITTEN IN YOUR BLUE BOOK

''' this module contains wacky line drawing and returning functions '''

import random # this could be in the body of makeRandyLine

def makeRandyLine( char1 = 'X', char2 = 'Y', char3 = 'Z' ):

'''

returns a line of characters passed in

char1 is repeated from 3 to 7 times, chosen randomly
char2 is repeated from 5 to 22 times, chosen randomly
char3 is repeated from 2 to 14 times, chosen randomly

'''

return char1*( random.randomInt( 3, 7 ) ) + \

char2*( random.randomInt( 5, 22 ) ) + \

char3*( random.randomInt( 2, 14 ) )

def drawRandyLine( char1 = 'X', char2 = 'Y', char3 = 'Z' ):
''' draws a line made from the call to makeRandyLine

cursor is taken to the start of the next line
'''

print makeRandyLine( char1, char2, char3 )

21. (20 pts)

Complete the definition of a function named lastBiggerThanFirstUnlessIsOthers that prints "YES" on the screen if the last of its five parameters is larger than the first parameter and prints "NO" otherwise – but do this output only as long as the fifth is not the same as the second, third or fourth – in which case "OOPS" is printed on the screen. The cursor is left on the next line.

Write two versions of this function – one using the in or not in operators and another not using them.

def lastBiggerThanFirstUnlessIsOthers( first, second, third, fourth, fifth ):
''' you do not have

to write the docstring

'''

Write ONLY the body of this function in the BlueBook.
Do not waste your time copying these lines into your BlueBook

ONLY THESE BODIES WOULD
BE WRITTEN IN YOUR BLUE BOOK

if fifth in [ second, third, fourth ]:
# could be: if fifth in ( second, third, fourth ):
print( "OOPS" )

else:

if fifth > first:

print( "YES" )

else:

print( "NO" )

if fifth == second or fifth == third or fifth == fourth:
print( "OOPS" )

else:

if fifth > first:

print( "YES" )

else:

print( "NO" )

The order of the clauses in both if-else statements does not matter as long as the tests are reversed.

What would have been even better?

if fifth in [ second, third, fourth ]:
# could be: if fifth in ( second, third, fourth ):

print( "OOPS" )

else:

printYesOrNoIf2ndBigger( first, fifth )

if fifth == second or fifth == third or fifth == fourth:

print( "OOPS" )

else:

printYesOrNoIf2ndBigger( first, fifth )

def printYesOrNoIf2ndBigger( first, second ):

''' … '''

if fifth > first:

print( "YES" )

else:

print( "NO" )

Does the def for printYesOrNoIf2ndBiggerhaveto be written before it's first use?
NO – def is only compiling and preparing for use.

def order is not runtime order.

WRITE ANSWERS TO THESE QUESTIONS IN THE BLUE BOOKpage 1 of 10 pages (counting the cover sheet)