Python Assignment #4: Qualifying Marks - 95%
Python Assignment #4: Qualifying Marks - 95%
Python Assignment #4: Qualifying Marks - 95%
Question 1
Which Python keyword indicates the start of a function definition?
sweet
continue
help
def
Question 2
In Python, how do you indicate the end of the block of code that makes up the function?
You put the "END" keyword in column 7 of the line which is to be the last line of the function
You put a # character at the end of the last line of the function
You add the matching curly brace that was used to start the function }
Question 3
A built-in function
The central processing unit
A conditional statement
Question 4
What does the following code print out?
def thing():
print('Hello')
print('There')
thing
There
thing
Hello
There
def
thing
Question 5
x = 'banana'
y = max(x)
z=y*2
max
'banana'
Question 6
What will the following Python code print out?
def func(x) :
print(x)
func(10)
func(20)
10
20
def
func
func
20
Question 7
Which line of the following Python program will never execute?
def stuff():
print('Hello')
return
print('World')
stuff()
stuff()
print ('World')
def stuff():
return
print('Hello')
Question 8
def greet(lang):
if lang == 'es':
return 'Hola'
elif lang == 'fr':
return 'Bonjour'
else:
return 'Hello'
print(greet('fr'),'Michael')
Bonjour Michael
Hola Michael
Hello Michael
def
Hola
Bonjour
Hello
Michael
Question 9
What does the following Python code print out? (Note that this is a bit of a trick question and the code has what many
would consider to be a flaw/bug - so read carefully).
14
Traceback
Question 10
What is the most important benefit of writing your own functions?
Following the rule that no function can have more than 10 statements in it
Avoiding writing the same non-trivial code more than once in your program
To avoid having more than 10 lines of sequential code without an indent or de-indent
Following the rule that whenever a program is more than 10 lines you must use a function
Question 11
Write a program to prompt the user for hours and rate per hour using input to compute gross pay. Pay should be the
normal rate for hours up to 40 and time-and-a-half for the hourly rate for all hours worked above 40 hours. Put the
logic to do the computation of pay in a function called computepay() and use the function to do the computation. The
function should return a value. Use 45 hours and a rate of 10.50 per hour to test the program (the pay should be 498.75).
You should use input to read a string and float() to convert the string to a number. Do not worry about error checking
the user input unless you want to - you can assume the user types numbers properly. Do not name your variable sum or
use the sum() function.