Solutions for the second quiz COSC 1306 Fall 2104
First Question What is the difference between a compiled language and an interpreted language? (2×5 points)
Answer What is the difference between a compiled language and an interpreted language? (2×5 points) A compiled language is translated before execution into binary code that can be directly executed. An interpreted language is interpreted just before and during execution into something executable.
Second question The University of Houston stores your names in a “last name first followed by a comma and no space” format as in “Edison,Thomas Alva”. Write a Python 3 function adding a space after the comma to and return something like “Edison, Thomas Alva”, which is more pleasing to the eye. (2×5 points). (Hint: I would split the string at the comma and rebuild it with the comma and the space.)
Answer def student_name(uh_name): lst = uh_name.split(',') return lst[0] + ', ' + lst[1] Space
Second question (variant) The University of Houston stores your names in a “last name first followed by a comma and no space” format as in “Edison,Thomas Alva”. Write a Python 3 function adding a space after the comma to and return something like “Thomas Alva Edison,”, which is more pleasing to the eye. (2×5 points). (Hint: I would split the string at the comma and rebuild it with the space.)
Answer def student_name(uh_name) : lst = uh_name.split(',') return lst[1] + ' ' + lst[0]
Third question Write a Python function computing restaurant tips. Your function should have as inputs the amount on the check the purchase and the tip rate in percent. In addition, it should assume a default tip rate of 15 percent. For instance, tip(50) should return 7.50 and tip(100, 18) should return 18. (2×5 points)
Answer def tip(bill, rate = 15) : return bill*rate/100
Fourth question Consider the following list containing two events, which themselves are lists: sched = [['1030', 'Faculty meeting'] ['1430', 'COSC 1306']] What would be the outcomes of the following Python statements, taken individually? (3×5 points) sched [0][0] = '1100' sched [0].append('Must attend') sched.pop(0)
Answer sched = [['1030', 'Faculty meeting'] ['1430', 'COSC 1306']] sched [0][0] = '1100' [['1100', 'Faculty meeting'] ['1430', 'COSC 1306']] sched [0].append('Must attend') [['1030', 'Faculty meeting', 'Must attend'] ['1430', 'COSC 1306']] sched.pop(0) [['1430', 'COSC 1306']]
Fifth question If lst = ['Ann', 'Barbara', 'Charles'], what would be the outcomes of the following Python statements, taken individually? (4×5 points) lst.pop() lst[0:1] lst[0] lst[:]
Answer If lst = ['Ann', 'Barbara', 'Charles'], what would be the outcomes of the following Python statements, taken individually? (4×5 points) lst.pop() ['Ann', 'Barbara'] lst[0:1] ['Ann'] lst[0] 'Ann' lst[:] ['Ann', 'Barbara', 'Charles']
Sixth question Complete the following program to have it compute the sum of all numbers entered. Your program should end once the user has entered a single minus sign and then print the total. (4×5 points)
Sixth question astring =input('Enter a number or a minus to terminate: ') sum = ___ while ___________: sum =___________ ________________ print('Total is ' + str(sum))
Answer astring =input('Enter a number or a minus to terminate: ') sum = 0 while astring != '-' : sum =sum + float(astring) astring =input('Enter a number or a minus to terminate: ') print('Total is ' + str(sum))
Seventh question Professor Jenkins has to compute her semester averages. Each of her student records consists of a student name followed by the grades she gave for the essays she assigned to her students as in: ['Avarez, Alonzo', 85, 70, 90, 85] Given that all essays have the same weight, what code will she write? (3×5 points) (Hint: use the sum() and len() methods and handle correctly students who have not turned in any essay.)
Seventh question def semester_average(record): ___________________________ if ________ : return ______________ else: return ______________
Answer def semester_average(record): essays= record[1:] # eliminate first entry if len(essays) == 0 : return 0 else: return sum(essays)/len(essays)
Seventh question (variant) Professor Patel has to compute her semester averages. Each of her student records consists of a student name followed by the grades she gave for the essays she assigned to her students as in: ['Alvarez', 'Alonzo', 85, 70, 90, 85] Given that all essays have the same weight, what code will she write? (3×5 points) (Hint: use the sum() and len() methods and handle correctly students who have not turned in any essay.)
Answer def semester_average(record): essays= record[2:] # eliminate two entries if len(essays) == 0 : return 0 else: return sum(essays)/len(essays)