COMP560 W3 ZyCh5_functions

1) A ___________________________ is a sequence of instructions with a name.

A) function

2) Consider the following function call round(3.14159, 3) what is the return value?
A) 3.141

3) Consider the following function call ceil(3.14159) what is the return value?
A) 4.0

4) Which of the following is a correct call to Python’s round function?
A) x = round(3.14159)

5) Consider a function named calc. It accepts two integer arguments and returns their sum as an integer. Which of the following statements is a correct invocation of the calc function?
A) total = calc(2, 3)

6) The ceil function in the Python standard library math module takes a single value x and returns the smallest integer that is greater than or equal to x. Which of the following is true about ceil(56.75)?
A) The argument is 56.75, and the return value is 57

7) Which process helps identify the functions that should make up a computer program?
A) stepwise refinement
8) The term Black Box is used with functions because
A) Only the specification matters; the implementation is not important.

9) One advantage of designing functions as black boxes is that
A) many programmers can work on the same project without knowing the internal implementation details of functions.

10) What is supplied to a function when it is called?
A) arguments
11) Consider a function named avg, which accepts four numbers and returns their average. Which of the following is a correct call to the function avg?
A) average = avg(2, 3.14, 4, 5) 

12) Which of the following statements is true about functions in Python?
A) Functions can have multiple arguments and can return one return value.

13) What Python statement exits a function and gives the result to the caller?
A) return

14) Given the code snippet below, what is returned by the function call: mystery(5,3)?

def mystery(num1, num2) :
   result = num1 * num2
   return result

A) 15

15) Given the code snippet below, what is returned by the function call: mystery(mystery(5, 3), mystery(5, 3))?

def mystery(num1, num2) :
   result = num1 * num2
   return result

A) 225

16) What is wrong with the following code snippet?

mystery(10, 2)
def mystery(num1, num2) :
   result = num1 ** num2
   return result

A) the function must be defined before the statement that calls it

17) Consider the following function:

def w(x, y) :
   z = x + y 
   return z

What is the function’s name?
A) w

18) The following function is supposed to compute the area of atriangle and return the area as the function’s result.

def triangleArea(base, height) :
   area = base * height / 2
  ____________________

What line of code must be placed in the blank to achieve this goal?
A) return area

19) Assume that you are writing a function that computes the volume of a box for shipping electrical components. The components vary in shape — some are long and skinny, while others are cube-like. Different boxes are used for components with different shapes. Which of the following function headers is the best?
A) def boxVolume(length, width, height) :

20) Consider the following function:

def squareArea(sideLength) :
   return sideLength ** 2

What is the value of squareArea(3)?
A) 9

21) Consider the following function:

def squareArea(sideLength) :
   return sideLength ** 2

What is the value of squareArea(squareArea(2))?
A) 16

22) Consider the following function:

def mystery(a, b) :
   result = (a - b) * (a + b)
   return result

What is the result of calling mystery(3, 2)?
A) 5
23) Consider the following function:

## Compute the volume of a cuboid.
#  @param width the width of the cuboid
#  @return the volume of the cuboid
def volume(width, height, length) :
   return width * height * length

Based on the recommendations in the textbook, what change should be made to improve the comments for this function?

A) Additional @param lines should be added for height and length

24) You are writing a function that converts from Liters to Gallons. Which function header is the best?
A) def litersToGallons(liters) :

25) Which of the following statements correctly defines a function?
A) def functionName(parameterName1, parameterName2) :
26) Consider the following function.

def factorial(n) :
   result = 1
   for i in range(1, n + 1) :
      result = result * i
   return result

What is the parameter variable for this function?
A) n

27) Given the following code snippet, what is considered a parameter variable(s)?

def mystery(num1, num2) :
   result = num1 ** num2
   return result
mystery(10, 2)

A) num1, num2

28) Given the following code snippet, what is considered an argument(s)?

def mystery(num1, num2) :
   result = num1 ** num2
   return result
mystery(10, 2)

A) 10, 2

29) Parameter variables should not be changed within the body of a function because
A) It is confusing because it mixes the concept of a parameter with that of a variable

30) Consider the following program:

def squareArea(sideLength) :
   return sideLength ** 2
 
a = squareArea(4)

What are the arguments (actual parameters) in this program?
A) 4

31) Consider the following program:

def main() :
   a = 5
   print(doubleIt(a))
 
def doubleIt(x) :
   return x * 2
 
main()

What output is generated when this program is run?
A) 10

32) Consider the following program:

def main() :
   a = 10
   print(doTwice(a))
 
def doTwice(x) :
   x = x * 2
   x = x * 2
   return x
 
main()

What output is generated when this program is run?
A) 40

33) Consider the following program:

def main() :
   a = 2
   doubleIt(a)
   print(a)
 
def doubleIt(x) :
   x = x * 2
 
main()

What output is generated when this program is run?
A) 2

34) What happens in this code snippet if sideLength = -10?

def cubeSurfaceArea(sideLength) :
   if sideLength >= 0 :
      return 6 * (sideLength * sideLength)
# There are six sides to a cube; surface area of each side is sideLength squared

A) a special value of None will be returned from the function

35) What is the purpose of this code snippet?

def mystery(n) :
   if n % 2 == 0  :
      return True
   else :
      return False

A) to determine if n is even or odd

36) When should a computation be turned into a function?
A) when it may be used more than once
37) The following program is supposed to display a message indicating if the integer entered by the user is even or odd. What is wrong with the program?

num = int(input("Enter an integer: "))
print("The integer is", evenOdd(num))
 
def evenOdd(n) :
   if n % 2 == 0 :
      return "even"
   return "odd"

A) The function definition must appear before the function is called.

38) The following function is supposed to return -1 when x is negative, +1 when x is positive, or 0 if x is zero. What, if anything, is wrong with the function?

def plusMinusZero(x) :
   if x == 0 :
      return 0
   elif x <= 0 :
      return -1
   else x >= 0 :
      return 1

A) Nothing is wrong with the function

39) What is wrong with the following function for computing the amount of tax due on a purchase?

def taxDue(amount, taxRate) :
   amount = amount * taxRate
 
def main() :
   . . .
   total = taxDue(subtotal, TAX_RATE)
   . . .

A) The function must return a value
40) For a program that reads city names repeatedly from the user and calculates the distance from a company’s headquarters, which of the following would be a good design based on stepwise refinement?
A) Write one function that reads city name and another function that calculates distance 

41) Which statement causes the following function to exit immediately?

def mystery(num1, num2) :
   result = num1 ** num2
   return result
mystery(10, 2)

A) return result

42) Which statement should be added or modified to remove the possibility of a run-time error in this code snippet?

1: def floorDivision(value1, value2) :
2:    return value1 // value2

A) add this statement after line 1: if value2 == 0 : return 0

43) How many return statements can be included in a function?
A) Zero or more

44) Given the following code, what is the output?

def main() :
   i = 20
   b = mysteriousFunction2(i)
   print(b + i)
 
def mysteriousFunction1(i) :
   n = 0
   while n * n <= i :
      n += 1
   return n - 1
 
def mysteriousFunction2(a) :
   b = 0
   for n in range(a) :
      i = mysteriousFunction1(n)
      b = b + i
   return b
 
main()

A) 70

45) The purpose of a function that does not return a value is
A) to package a repeated task as a function even though the task does not yield a value

46) Consider the following functions:

def printIt(x) :
   print(x)
 
def incrementIt(x) :
   return x + 1
 
def decrementIt(x) :
   return x - 1
 
def doubleIt(x) :
   return x * 2

Which of the following function calls is not a reasonable thing to do?
A) print(printIt(5))

47) The following function is supposed to compute and display the value of n-factorial for integers greater than 0.

def factorial(n) :
   result = 1
   for i in range(1, n + 1) :
     result = result * i

What is wrong with this function?

A) The function is missing a line. A print statement must be added at the end of it.

48) Which function call correctly invokes the partial drawShape function listed below and prints a star triangle?

def drawShape(type) :
   length = len(type)
   if length == 0 :
      return
   if type == "triangle" :
      print("  *")
      print(" ***")
      print("*****")
drawShape("triangle")

A) drawShape(“triangle”)

49) Consider the following code segment:

def f1():
   print("A", end="")
 
def f2():
   f1()
   print("B", end="")

What output is generated when it runs?
A) The code segment does not display any output.

50) Consider the following code segment:

def f1():
   print("a", end="")
   return "b"
 
def f2():
   print("c", end="")
   d = f1()
   print(d, end="")
   print("e", end="")
 
def f3():
   print("f", end="")
   f2()
   print("g", end="")
 
f3()

What output is generated when it runs?
A) fcabeg
51) The function below randomly generates a number between 1 and 6 to represent a single die. Which implementation listed below allows for other types of die?

def die() :
   return randint(1, 6)

A) def die(low, high) :
return randint(low, high)

52) Given these two separate functions, which implementation combines them into one reusable function?

def sixSidedDie() :
   return randint(1, 6)
def fourSidedDie() :
   return randint(1, 4)

A) def die(low, high) :
return randint(low, high)

53) A programmer notices that the following code snippet uses the same algorithm for computing interest earned, but with different variables, in the two places shown below and in several other places in the program. What could be done to improve the program?

RATE1 = 10
RATE2 = 5.5
interest = investment * RATE1 / 100
. . . 
balance = balance + balance * RATE2 / 100

A) Define a function that computes the interest earned from an amount and a rate of interest.

54) What is wrong with the following code?

def grade(score) :
   if score >= 90 :
      return "A"
   elif score >= 80 :
      return "B"
   elif score >= 70 :
      return "C"  
   elif score >= 60 :
      return "D"

A) Another return statement needs to be added to the function

55) A stub function is
A) A function that acts as a placeholder and returns a simple value so another function can be tested
56) Consider the following function:

def numberToGrade(x) :
   return "X"

This function will eventually be rewritten so that it returns the letter grade associated with x grade points. However, at the moment it is incomplete, and always returns the letter X as a placeholder. In its current form, this function is referred to as a:
A) stub

57) For a program that reads three letter grades and calculates an average of those grades, which of the following would be a good design based on stepwise refinement?
A) Write one function that reads a letter grade and returns the number equivalent, and one function that computes the average of three numbers. 

58) What is stepwise refinement?
A) The process of breaking complex problems down into smaller, manageable steps

59) Why is hand-tracing or manually walking through the execution of a function helpful?
A) It is an effective way to understand a function’s subtle aspects

60) When hand-tracing functions, the values for the parameter variables:
A) Are determined by the arguments supplied in the code that invokes the function

61) Which of the following is NOT a good practice when developing a computer program?
A) Put as many statements as possible into the main function

62) The tool that allows you to follow the execution of a program and helps you locate errors is known as a(n):
A) Debugger

63) The location where the debugger stops executing your program so that you can inspect the values of variables is called a(n):
A) breakpoint
64) Which debugging command allows you to quickly run an entire function instead of examining its body a line at a time?
A) Step over

65) Given the following code snippet, which statement correctly allows the function to update the global variable total?

1. total = 0
2. def main() :
3.    avg = 0
4.    for i in range(6) :
5.       iSquared = i * i
6.       total = total + iSquared
7.    avg = total / i
8.    print(total)
9.    print(avg)

A) add the statement global total after line 2

66) What is the output from the following Python program?

def myFun(perfect) :
   perfect = 0
   return ((perfect - 1) * (perfect - 1))
 
def main() :
   for i in range(4) :
       print(myFun(i), end = " ")
           
main()

A) 1 1 1 1

67) Which of the following statements about variables is true?
A) The same variable name can be used in two different methods.

68) In the code snippet below, which variables are considered global variables:

a = 0
b = 5
def main() :
   global a, b
   fun1()
   fun2()
 
def fun1() :
   i = 0
   b = 0
 
def fun2() :
   a = b + 1
 
main()

A) a, b

69) The variable name perfect in the function myFun in the code snippet below is used as both a parameter variable and a variable in the body of the function. Which statement about this situation is true?

def myFun(perfect)
   perfect = 0
   return ((perfect - 1) * (perfect - 1))

A) While this is legal and will compile in Python, it is confusing

70) Consider the following code segment:

def main() :
   avg = 0
   total = 0
   for i in range(6) :
      iSquared = i * i
      total = total + iSquared
      avg = total / i
   print(total)
   print(avg)

Which of the following answers lists all of the local variables in this code segment?
A) avg, total, i, iSquared

71) What is the output from the following Python program?

def main() :
   a = 10
   r = cubeVolume()
   print(r)
 
def cubeVolume() :
   return a ** 3
 
main()

A) Nothing, there is an error.

72) What term is used to describe the portion of a program in which a variable can be accessed?

A)  scope

73) What is the output of the following code snippet?

def main() :
   print(blackBox(4))
 
def blackBox(a) :
   if a <= 0 :
      val = 1
   else :
      val = a + blackBox(a - 2)
   return val
 
main()

A) 7

74) Based on the code snippet, which of the following statements is correct?

def main() :
   reoccur(1)
 
def reoccur(a) :
   print(a)
   reoccur(a + 1) 
 
main()

A) The code snippet executes and infinitely recurses, displaying1, 2, 3, 4, and so on.

75) What is the output if the function call is testMyVal(6) in the following code snippet?

def testMyVal(a) :
    if a > 0 :
       testMyVal(a - 2)
    print(a, end = " ")

A) 0 2 4 6

76) Which of the following code snippets returns the factorial of a given number? (Hint: Factorial of 5 = 5! = 1 * 2 * 3 * 4 * 5 = 120)
A) def factorial(num) :
if(num == 1) :
return 1
else :
return num * factorial(num – 1)

77) Consider this recursive function:

def mystery(x) :
   if x <= 0 : 
      return 0
   else :
      return x + mystery(x - 1)

What is mystery(5)?
A) 15

78) Consider the following program:

def main() :
   print(factorial(n))         # Line 1
 
def factorial(n) :             # Line 2
   result = 1
   for i in range(1, n + 1) :
      result = result * i      # Line 3
   return i
 
main()

Which of the following lines is a recursive function call?

A) There are no recursive function calls in the program

79) Consider the following program:

def main() :
   print(factorial(n))            # Line 1
 
def factorial(n) :                # Line 2
   if n <= 1 :
      return 1
   return n * factorial(n - 1)    # Line 3
 
main()

What line is the recursive call on?
A) Line 3

80) What output is generated when the following program runs?

def main() :
   x = mystery(5)
   print(x)
 
def mystery(n) :
   if n == 1 :
      return 1
   return 2 * mystery(n - 1)
 
main()

A) 16
81) What output is generated when the following program runs?

def main() :
   x = mystery(9, 12)
   print(x)
 
def mystery(a, b) :
   if b == 0 :
      return a
   else :
      return mystery(b, a % b)
 
main()

 A) 3
 82) Which line of code in the Python program below is the recursive invocation of function myFun?

1: def main() :
2:    for i in range(4) :
3:       print(myFun(i), end = " ")
4: def myFun(perfect) :
5:    perfect = 0
6:    return ((perfect - 1) * (perfect - 1))
7: main()

A) There is no recursive invocation in this code segment

83) What is the output of the following code snippet?

def main() :
   print("fun(2) =", fun(2))
 
def fun(a) :
   returnValue = 0
   if a > 5 :
      returnValue = a
   else :
      returnValue = fun(2 * a)
   return returnValue
 
main()

A) fun(2) = 8

84) What term is used to refer to a collection of functions and classes organized into one or more user-defined modules?
A) software toolkit

85) Which function should not be included in a software toolkit for managing student enrollment in courses?
A) computeNegativeImage
 
 
 

 
 
 
 

 
 

 
 
 
 
 
 
 
 
 
 
 
 
 

Leave a Reply

Your email address will not be published. Required fields are marked *