CS 1110 Exam 1, Fall 2018 Page 1 of 8 UVa userid: CS 1110 Exam 1 Name Bubble in your computing ID in the footer of this page. We use an optical scanner to read it, so fill in the bubbles darkly. If you have a shorter ID, leave some rows blank. In case we have trouble with the scanner, please also legibly print your computing id at the top of each page. Also print your name on this page. Please follow these rules throughout the exam: Write legibly; we deduct points if we are unsure what you wrote. Indentation and punctuation do matter. Write on the lines, where possible. If you need to insert a line between two you ve written, make it clear what you are doing. We grade one page at a time. Do not spill answers onto another page. Don t add features we didn t request: only print if we ask you to print, etc. The exam is being given in multiple locations simultaneously, so we cannot fairly answer student questions during the exam. If you find a question ambiguous or unclear, write that down on your exam next to the question and we will give it due consideration during grading. 1. [4 points] Pledge On my honor as a student I have neither given nor received aid on this exam. signed: 2. [4 points] Bubbles Fill in the bubble footer with your computing id: Do not write in this area 0 1 2 3 4 5 6 7 8 9 Department Course Exam Page Version (the bubble footer is automatically inserted into this space)
CS 1110 Exam 1, Fall 2018 Page 2 of 8 UVa userid: Page 2: 3. [12 points] Display and Return Fill in the following table. Displayed in Console means anything that shows up in the output window, whether printed or error message; if an error message answering Error is sufficient, no need to say more. If the code does not display anything then write Nothing. Assume that every time the user is required to type something, they type 34. The first row is done for you Code Displayed in Console Evaluates To 7+5 Nothing 12 print( don\ t, panic ) input( What number ) 2 ((2/1)==2) and ( 10 == ( 1 + 0 )) int(1/2) print(100 + % ) 1 (23 % 12)
CS 1110 Exam 1, Fall 2018 Page 3 of 8 UVa userid: Page 3: 4. [14 points] Evaluate Fill in the following table. The first row is done for you Expression Value Type 7+5 12 int 4/2 int( float ( 1.87 )) 1 2 type(3)==type( 3 ) (1/2) 2 2 3 int( 13 2) + 15
CS 1110 Exam 1, Fall 2018 Page 4 of 8 UVa userid: Page 4: 5. [14 points] What does this print? Give all text printed exactly as this program will print it. def f ( ) : global x x = 2 y = x 2 return y / x x = 3 y = 5 z = f ( ) print ( f ( ) ) print ( x, y, z )
CS 1110 Exam 1, Fall 2018 Page 5 of 8 UVa userid: Page 5: 6. [10 points] Cheers Write a function called cheer which receives an int as an argument which identifies how many times we should cheer. The output should be a string representing the cheer. When we cheer, every third word should be Hooray, and all other words should be Hip. For example, if we cheer 3 times we say HipHipHooray if we cheer 1 time we say Hip if we cheer 7 times we say HipHipHoorayHipHipHoorayHip Complete the function by giving expressions for values of y and z which achieve the required outputs. def cheer ( x ) : y = # Your answer here z = # Your answer here return ( HipHipHooray y ) + ( Hip z )
CS 1110 Exam 1, Fall 2018 Page 6 of 8 UVa userid: Page 6: 7. [12 points] if statements Consider the following code: i f 0 < x : print ( h e l l o ) e l i f 0 > x : print ( goodbye ) else : i f x ==0: print ( welcome ) Give an int value for x which will cause the code to print each of the following (or say it isn t possible). When we say Nothing displayed, we mean nothing is shown in the console (not that the text Nothing Displayed gets printed). Value of x Thing that is printed hello goodbye welcome Nothing Displayed
CS 1110 Exam 1, Fall 2018 Page 7 of 8 UVa userid: Page 7: 8. [15 points] Baggage Fees Write a function called baggage fee which receives the weight of a bag (which is a float) as an argument, and returns a float representing how much an airline would charge for a checked bag of that weight. The airline will charge based on the following rules: The first up to 50.0 pounds will cost 30.0 total Bags over 50.0 pounds will incur additional cost at a rate of 1.50 per pound For example, a bag weighing 10.0 pounds will cost 30.0, and a bag 50.5 pounds will cost 30.75 There is no need to round your answer. We don t care what your function does when its argument is not a float
CS 1110 Exam 1, Fall 2018 Page 8 of 8 UVa userid: Page 8: 9. [15 points] Account Balance Write Python code to define a function called make transaction which receives a float representing a deposit/withdrawal from a bank account as an argument. Deposits will have a positive value, withdrawals a negative value. After each transaction, the function will print the new account balance as a side effect. The balance starts at 0. For example, if we call: make transaction ( 5. 2 ) make transaction ( 2.1) make transaction ( 1 ) the program will print out: 5.2 3.1 4.1 We don t care what your function does when its argument is not a float