CS1114 Spring 2015 Test ONE ANSWER KEY page 1 of 8 pages (counting the cover sheet)
For the following questions, use these variable definitions a = 36 b = 3 c = 12 d = '3' What is the type and value of each of the following expressions or, if it won't compile, circle that answer type value circle if will not compile 1 (2 pts) a // b int 12 will not compile 2 (2 pts) a % b int 0 will not compile 3 (2 pts) (c * 8) / 12 float 80 will not compile 4 (2 pts) str( b + 2 ) * int( c ) str '5' will not compile 5 (2 pts) hex( 17 ) str '0x10' will not compile must have quotes of some sort 6 (2 pts) 5 if 4 == 3 else 2 int 2 will not compile 7 (2 pts) None NoneType None will not compile 8 (2 pts) a < c and b == 1 bool False will not compile spelling counts and caps count 9 (2 pts) b + c float 42 will not compile 10 (2 pts) b < 4 or not will not compile 11 (2 pts) b > c >= 0 will not compile 12 (2 pts) (on the next several lines hi lo str 'hi\nlo' will not compile must be str and have newline
13 (5 pts) Given: p = hello i = 'grail' print( p, end='' ) print( p, i, sep = '22' ) Show what will be on the screen after these statements are executed If there will be a runtime error or this will not compile, state that here hellohello22grail A programmer has been hired by a company to write a function that returns a random value between minimum and maximum values set by the company These are inclusive Note that this question is NOT about branching Do NOT write this function Do NOT write any code for this question ONLY answer this question: 14 (10 pts ) What are the pieces of information this programmer needs to solve this subproblem? For each, state the source of information and how each would be implemented in python (If there are no needed pieces of information, just state none) Consider both good design as well as the fact that the company has a very specific minimum length INFORMATION NEEDED SOURCE HOW TO IMPLEMENT random valuecaller (2 pt) calculated locally assignment or random call (2 pt) cannot be known (-2 pt) cannot be device input (-8!) the writer of this function cannot need to know where their caller got the information passed in minimum (2 pt) known (2pt) OR caller (2 pt) global constants or parameters maximum (2 pt) [could be either] [caller would probably pass in the global] 15 (3 pts ) What design issue does this programmer not understand?? def amtuserowesisdivisibleby( amountuserowes, divisor ): returns True when the amount the user owes is evenly divisible by the passed in divisor, otherwise False Both parameters must be ints The user can't owe any value not evenly divisble by divisor return amountuserowes % divisor == 0 The designer of a function cannot need to know how the caller will use their function (and there is no user)
16 (5 pts ) When you want to call a function, what must you NOT NEED TO READ? (circle only one letter) a) the def line (the first line of the function definition) b) the docstring c) the body of the function d) the call of the function e) c) and d) only f) a) and c) only g) a), b) and c) only h) b), c), and d) only 17 (3 pts ) else: is a statement (circle your answer) YES NO 18 (10 pts ) In a raffle, there are 20 possible prizes and a consolation prize The winning user's prize has already been chosen Now it's time to print a line on the screen telling the winning user which prize they won Which problem form is controlling the output? If you can't answer which of the problem forms, for fewer points you may answer by stating which python statement should be used to control the screen display one of many, one must or for a maximum of 4 points: if-elif-elfi- else 19 (5 pts ) If defensive programming, were implemented, how would the code for #18 be different? Do NOT write any code The last else must be changed to an elif and an else must be added (with the defensive handling code, like exit or something)
CS1114 Test ONE Spr15 ANSWER KEY 20 PARTS ONE, TWO and THREE (18 pts together, 6 pts each) def callthee( ): 0 print( 'THEE' ) 1 x = 5 2 p = callme( x ) 3 print x 4 return( p ) 5 def callme( p ): 6 print( 'ME' ) 7 p = 3 8 x = p + 2 9 return 4 PART FOUR (7 pts ) For example: This arrow shows where x refers to after line 1 x PART ONE (6 pts) Draw the arrow showing where p refers after line 5 p PART TWO (6 pts) Draw the arrow showing where p refers after line 7 p PART THREE (6pts) Draw the arrow showing where x refers after line 8 x Write the values that will be in RAM 6 5 4 3 2 1 What is the output when main is called? def main(): write your output here print( callthee( ) ) THEE ME main() 5 4
CS1114 Test ONE Spr15 Print your name legibly: circle your last name Sign: Poly-ID: 21 (20 pts) You are given the randylines module which contains these functions (shown with their docstrings) def drawlineofranddigit( howlong ): displays a line howlong wide using a single randomly chosen digit and positions the cursor onto the next line on the screen assumes howlong is an interger value For example: drawlineofranddigit( 7 ) would produce this output: 4444444 if the randomly chosen digit were 4 # do NOT write the body of this function def drawfourranddigits( ): Displays a four digit long line where each digit is chosen randomly For example drawfourranddigits( ) might display: 5232 or 7744 or even 5555 # do NOT write the body of this function def drawlineofdigit( digit, width ): displays a line width wide using the digit passed in and positions the cursor onto the next line on the screen assumes width and digit are interger values For example drawlineofdigit( 3, 4 ) would display: 3333 # do NOT write the body of this function This question continues on the next page WRITE ANSWERS TO THESE QUESTIONS IN THIS DOCUMENT page 6 of 8 pages (counting the cover sheet)
CS1114 Test ONE Spr15 Print your name legibly: circle your last name Sign: Poly-ID: (21 cont) Calling only the functions in the randylines module and the random module and the print function, create these two functions: (CORRECTION ANNOUNCED DURING TEST) --- a function that displays a 4 x 4 square made of random digits 2287 For example calling this function might produce this pattern: 5388 --- a function that displays a line made of a single random digit that is of random length - between 5 and 31 digits (incl) For example calling this function might produce this pattern: 88888888888888888 (You may not use looping in these two functions) 7760 2704 DO write the docstrings for the these two functions Write only these two defs no main, no program level docstrings You do not have to write from future import print_function, we'll assume it's written import randylines import random def draw4x4squareofrandomdigits( ): displays a 4 x 4 square made of random digits randylinesdrawlineofranddigit( 4 ) randylinesdrawlineofranddigit( 4 ) randylinesdrawlineofranddigit( 4 ) randylinesdrawlineofranddigit( 4 ) def drawlineofsingleranddigitbtwn5and31long( ): displays a line made of a single random digit that is of random length - between 5 and 31 digits, incl randylinesdrawlineofranddigit( randomrandint( 5, 31 ) ) OR randylinesdrawlineofdigit ( randomrandint( 0, 9 ), randomrandint( 5, 31 ) ) WRITE ANSWERS TO THESE QUESTIONS IN THIS DOCUMENT page 7 of 8 pages (counting the cover sheet)
22 (15 pts ) There is a builtin function you have never used before named omytomm Here is a SnakePit session showing the help for that function IDLE 271 >>> help( omytomm ) Help on built-in function omytomm in module builtin : omytomm() omytomm(object, object, object) -> str The first two parameters must be strings, third must be int Returns the str made of the first and second parameters concatenated together and repeated the third parameter number of times Eg: "Hi", "Lo", 3 yields the string: "HiLoHiLoHiLo" (3 concatenated "HiLo"'s) Obviously you cannot define this function because it is built in Your job is to complete drawfigurewithstrs by writing the needed parameters and python statements that make the function do what the docstring says it will do Even though it's not the best, the only functions you can call are omytomm and other builtin functions You do not need to show any testing for your function Do NOT write a main function (obviously) or a complete program (obviously) ONLY complete the definition for drawfigurewithstrs (don't forget the parameters!) def drawfigurewithstrs( first, second ): Given two strings, draws a pattern on the screen made of the strings jammed together in a nice way Here is an example of the output when the strings are "John" and "Mary" There are always these five lines with each str as shown on each line: JohnMaryJohnMaryJohnMary Mary-John John---Mary Mary-----John MaryJohnMaryJohnMaryJohnMaryJohn Assume both parameters are strs print( omytomm( first, second, 3 ) ) print( second, first, sep='-') print( first, second, sep='---') print( second, first, sep='-----') print( omytomm( second, first, 4 ) ) [points off for missing sep-] [points off for missing sep---] [points off for missing sep-----] Can also use '%s' % Not sure how?! Ok if replicate the sep: sep='-'*3 and sep='-'*5 Write your answer to this question in your BLUEBOOK page 8 of 8 pages (counting the cover sheet)