Unformatted text preview:

Midterm No. 1 for V22.0002.005Wednesday, October 6, 2010There are three sections. Each section is worth 35 points. The maximum score on the test is 105.Please remember to print your name clearly on every page of this test, as well as on the accompanying blue booklet.Please feel free to write answers both on this page and the test booklet. However, please make it clear in each case, so Iknow where to find your answer.Section 1: For each statement, indicate: (a) what will be printed to the screen; and (b) what is the “value” or outputof the expression. Write NOTHING (in all capital letters) to indicate that there is nothing printed or that the expressionhas no value. Note that the equals sign =, which assigns a value to a variable, is assumed to have NOTHING for outputas well. If the statement should produce an error: (a) write ERROR in the last column; and (b) put ’ERROR’ in the ’PrintOut’ column, unless you expect something to print out before the error message. See the examples for clarification.Question Expression Print Out ValueSample 1 print(’123’) 123 NOTHINGSample 2 x=5 NOTHING NOTHINGSample 3 1+1 NOTHING 2Sample 4 1+’Fred’ ERROR ERRORSample 5 print(5) + ’duck’ 5 ERROR1. ’5’ + ’2’ NOTHING ’52’2. ’5’ * 2 NOTHING ’55’3. 5 ** 2 NOTHING 254. print(“A single quote looks like this ’ ”) A single quote looks like this ’ NOTHING5. print(’book’ ** 3) ERROR ERROR6. first number = 37 NOTHING NOTHING7. str(int(’57’)) + ’ ducks’ NOTHING ’57 ducks’8. ’chocolate’ ** 5 ERROR ERROR9. ”’hellooooo ERROR ERROR10. int(’57’) + ’57’ ERROR ERRORSection 2: Below will be a number of function definitions containing errors. On the appropriate line numbers in thetable below the function, please list the errors (if any), along with possible corrections.In each case, a minimum number of errors is listed. You do not need to find more errors than that number. Forexample, if the description says to find three errors, you only need to find three, even if there are actually four or fiveerrors.Sample 6: The function minus takes two numbers as input and subtracts the second from the first. There is onlyone error.1Line Code1. def minus(total,decrement)2 output = total - decrement3. return(output)Line Correction of Error1. need to add colon (:) at end of line2.3.Question 11: This function solicits user input, by querying about some of the user’s personal attributes. It then printsout these attributes in a single sentence. Find 3 errors.Line Code1. def describe intelligent life form():2. height = input(’What is your height in centimeters? ’)3. species = input(’What is your species? ’)4. input(’Are you male or female?’)5. name = input(’What is your name? ’)6. favorite-food = input(’What is your favorite food? ’)7. print(’I see that you are a’, height, ’centimeters tall’, gender \8. ’specimen of the’ species, ’species’)9. print(’and that your favorite food is’, favorite-food+’.’)10. ## Example with user input: 182, male, Adam, chocolate11. ## I see that you are a 182 centimeters tall male specimen of the human species12. ## and your favorite food is chocolate.Line Correction of Error1.2.3.4. no variable is defined5.6. The variable favorite-food contains an illegal character: ’-’7.gender is not defined and comma required after gender8.9. The illegal variable favorite-food is here10.11.12.2Question 12: The length of a person’s foot is claimed to be 15% of their height. This function tests this hypothesisfor one individual. It prints out an informative statement and returns the user’s foot to height ratio. Find 4 errorsLine Code1. def test foot to height ratio():2. print(’This function calculates and records your foot-length to total height ratio.’)3. print(’All measurements should be given in inches if you are Americanor in millimeters if you are from anywhere else in the world.’)4. foot-length=float(input(’Please measure your foot and state its length: ’))5. height=input(float(’Please indicate your total height: ’))6. ratio = foot-length / height7. print(’Thank you for your participation in our study!!!!’)8. print(’Your foot-length to height ratio is: ’, ratio)9. print(’The average foot-length to height ratio may be about .15’)10. print(’Your ratio is ’, (ratio / .15) ’times that ratio’)11. return(ratio)12. ## Example print statements given user input of 11 and 72:13. ## Thank you for your participation in our study!!!!14. ## Your foot-length to height ratio is: 0.152777777777777815. ## The average foot-length to height ratio may be about .1516. ## Your ratio is 1.0185185185185186 times that ratioLine Correction of Error1.2.3. Use Triple Quotes or put a \ before the newline4. foot-length has a -, an illegal character5. input function should contain a string, not a float6. foot-length is an illegal variable7.8.9.10. add comma after second argument of print (ration / .15)11.12.13.14.15.16.3Section 3: Write two functions as specified.Question 13:This program will center three lines of text as follows. The user is asked to provide three sentences of no more than65 characters. The program then centers each line by adding between 0 and 32 spaces to the beginning and then printingit. For example, if one of the sentences was ’Hello World’, the system would add 27 spaces to the beginning of the stringbefore printing. Note that the function: len(string) returns the number of characters in a string. Furthermore, it may benecessary to consistently round the number of spaces added either up or down in order to center the strings consistently.Thus the centering will be as close as possible, but not perfect.Idle Snapshot of Sample Answer: function and testing function>>> def center_3_sentences():import mathprint(’This program will center 3 lines of text that you provide’)print(’Each line should be no more than 65 characters long’)sentence1 = input(’What is the first line: ’)sentence2 = input(’What is the second line: ’)sentence3 = input(’What is the third line: ’)offset1 = round((65 - len(sentence1))/ 2)offset2 = round((65 - len(sentence2)) / 2)offset3 = round((65 - len(sentence3)) / 2)print(offset1*’ ’+sentence1)print(offset2*’ ’+sentence2)print(offset1*’ ’+sentence3)>>> center_3_sentences()This program will center 3 lines of text that you provideWhat is the first line: ChickensWhat is the second line: Giant pigs from


View Full Document

NYU CSCI-UA 0002 - Midterm Exam with answers

Download Midterm Exam with answers
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 Midterm Exam with answers 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 Midterm Exam with answers 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?