Testbank-Chapter-5 - qweqwe PDF

Title Testbank-Chapter-5 - qweqwe
Author Yi Li
Course Computer science
Institution The University of Western Ontario
Pages 21
File Size 213.9 KB
File Type PDF
Total Downloads 86
Total Views 137

Summary

qweqwe...


Description

10/24/2017

Testbank (Chapter 5)

Title Book Edition

5 Functions Python for Everyone 1

1. Which process helps with identifying the methods that make up a computer program? A. black boxing B. stepwise refinement Answer C. parameter passing D. debugging Title Section

Which process helps identify methods that make up a program? 5.1 Functions as Black Boxes

2. The term Black Box is used with methods because A. Only the implementation matters; the specification is not important. B. Only the specification matters; the implementation is not important. Answer C. Only the arguments matter; the return value is not important. D. Only the return value matters; the arguments are not important. Title Section

Why is the term black box used with methods? 5.1 Functions as Black Boxes

3. One advantage of designing methods as black boxes is that A. many programmers can work on the same project without knowing the internal implementation details of methods. Answer B. the result that is returned from black-box methods is always the same data type. C. the implementation of the function is open for everyone to see. D. there are fewer parameters. Title Section

What is one advantage of designing methods as black boxes? 5.1 Functions as Black Boxes

4. A ___________________________ is a sequence of instructions with a name. A. variable B. argument C. parameter D. function Answer Title Section

What is a sequence of instructions with a name called in Python? 5.1 Functions as Black Boxes

5. What is supplied to a function when it is called? A. variables B. arguments Answer C. parameters D. functions Title Section

What is supplied to a function when it is called? 5.1 Functions as Black Boxes

6. Consider the following function call round(3.14159, 3) what is the return value? A. 3.14159 B. 3.141 Answer C. 3.14 D. 3.1 Title Section

file:///C:/Horstmann1e TB/ch05.testbank.xhtml

What is returned from the round function when it is called? 5.1 Functions as Black Boxes

1/21

10/24/2017

Testbank (Chapter 5)

7. Consider the following function call ceil(3.14159) what is the return value? A. 3.14159 B. 3.0 C. 4.0 Answer D. 3.1416 Title Section

What is returned from the ceil function when it is called? 5.1 Functions as Black Boxes

8. Which of the following is a correct call to Python's round function? A. x = round("3.14159", 2) B. x = round("3.14159") C. x = round(3.14159) Answer D. x = round(3, 1, 4, 1, 5, 9) Title Section

Which of the following is a correct function call for Python's round function? 5.1 Functions as Black Boxes

9. 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() B. total = calc(2) C. total = calc("2", "3") D. total = calc(2, 3) Answer Title Section

Which of the following is a correct function call? 5.1 Functions as Black Boxes

10. Which of the following syntax statements correctly define a function? A. def functionName(parameterName1, parameterName2,. . .) : Answer B. def functionName(parameterName1, parameterName2,. . .) C. functionName(parameterName1, parameterName2,. . .) : D. functionName(parameterName1, parameterName2,. . .) Title Section

What is the correct syntax for a function definition? 5.2 Implementing and Testing Functions

11. What Python statement exits the function and gives the result to the caller? A. def B. return Answer C. send D. result Title Section

What Python statement exits the function and gives the result to the caller? 5.2 Implementing and Testing Functions

12. 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. 8 B. 15 Answer C. 2 D. 0 Title Section file:///C:/Horstmann1e TB/ch05.testbank.xhtml

What is the result of the given function call? 5.2 Implementing and Testing Functions 2/21

10/24/2017

Testbank (Chapter 5)

13. 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 Answer B. 15 C. 30 D. 0 Title Section

What is the result of the given function call? 5.2 Implementing and Testing Functions

14. What is wrong with the following code snippet? mystery(10, 2) def mystery(num1, num2) : result = num1 ** num2 return result

A. nothing, it will return 20 B. nothing, it will return 100 C. a variable must be used to store the result of the function call D. the function must be defined before the statement that calls it Answer Title Section

What is wrong with the following code snippet? 5.2 Implementing and Testing Functions

15. Consider the following function: def w(x, y) : z = x + y return z

What is the function's name? A. w Answer B. x C. y D. z Title Section

What is the function's name? 5.2 Implementing and Testing Functions

16. The following function is supposed to compute the area of a triangle 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. print area B. print(area) C. return area Answer D. return triangleArea file:///C:/Horstmann1e TB/ch05.testbank.xhtml

3/21

10/24/2017

Testbank (Chapter 5)

Title Section

What line of code is needed to complete the function for computing the area of a triangle? 5.2 Implementing and Testing Functions

17. 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() : B. def boxVolume(sideLength) : C. def boxVolume(a, b, c) : D. def boxVolume(length, width, height) : Answer Title Section

Select the best header for a function that computes the volume of a box 5.2 Implementing and Testing Functions

18. Consider the following function. def factorial(n) : result = 1 for i in range(1, n + 1) : result = result * i

What is the parameter variable for this function? A. factorial B. i C. n Answer D. result Title Section

Identify the parameter variable for a function 5.2 Implementing and Testing Functions

19. Consider the following function: def squareArea(sideLength) : return sideLength ** 2

What is the value of squareArea(3)? A. 2 B. 3 C. 6 D. 9 Answer Title Section

Trace a function call 5.2 Implementing and Testing Functions

20. Consider the following function: def squareArea(sideLength) : return sideLength ** 2

What is the value of squareArea(squareArea(2))? A. 2 B. 4 C. 8 D. 16 Answer Title Section file:///C:/Horstmann1e TB/ch05.testbank.xhtml

Trace a nested function call 5.2 Implementing and Testing Functions 4/21

10/24/2017

Testbank (Chapter 5)

21. 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. 2 B. 3 C. 4 D. 5 Answer Title Section

Trace a function call 5.2 Implementing and Testing Functions

22. 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. The @param line for width should be removed B. Additional @param lines should be added for height and length Answer C. The first line should be expanded to describe how the function performs its calculation D. The @return line should be removed Title Section

How can the function's comment be improved? 5.2 Implementing and Testing Functions

23. You are writing a function that converts from Liters to Gallons. Which function header is the best? A. def litersToGallons() : B. def litersToGallons(liters) : Answer C. def litersToGallons(gallons) : D. def litersToGallons(liters, gallons) : Title Section

Which function header is best for a function that converts from Liters to Gallons? 5.2 Implementing and Testing Functions

24. 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. 10, 2 B. num1, num2 Answer C. result D. mystery Title Section

Identify the parameter variable(s) in the given code snippet 5.3 Parameter Passing

25. Given the following code snippet, what is considered an argument(s)? file:///C:/Horstmann1e TB/ch05.testbank.xhtml

5/21

10/24/2017

Testbank (Chapter 5)

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

A. 10, 2 Answer B. num1, num2 C. result D. mystery Title Section

Identify the argument(s) in the given code snippet 5.3 Parameter Passing

26. Parameter variables should not be changed within the body of a function because A. This will generate a compiler error B. This will generate a run-time error C. It is confusing because it mixes the concept of a parameter with that of a variable Answer D. It is confusing because parameter variables cannot store values Title Section

Why shouldnt parameter variables be changed within the body of a method? 5.3 Parameter Passing

27. Consider the following program: def squareArea(sideLength) : return sideLength ** 2 a = squareArea(4)

What are the arguments (actual parameters) in this program? A. 2 B. 4 Answer C. sideLength D. squareArea Title Section

Identify the arguments in a program 5.3 Parameter Passing

28. 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. 2 B. 4 C. 5 D. 10 Answer Title Section

Trace a main program and function call 5.3 Parameter Passing

29. Consider the following program: file:///C:/Horstmann1e TB/ch05.testbank.xhtml

6/21

10/24/2017

Testbank (Chapter 5)

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. 2 B. 10 C. 20 D. 40 Answer Title Section

Trace a main program and function call 5.3 Parameter Passing

30. 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 Answer B. 4 C. 8 D. Python reports an error because doubleIt does not contain a return statement Title Section

Trace a main program and function call 5.3 Parameter Passing

31. What statement immediately exits the function below: def mystery(num1, num2) : result = num1 ** num2 return result mystery(10, 2)

A. return result B. none, it causes and infinite recursion C. result = num1 ** num2 Answer D. mystery(10,2) Title Section

What statement immediately exits the given function? 5.4 Return Values

32. Which statement should be added to this code snippet to avoid a run-time error? 1 def floorDivision(value1, value2) : file:///C:/Horstmann1e TB/ch05.testbank.xhtml

7/21

10/24/2017

2

Testbank (Chapter 5)

return value1 // value2

A. in line 2: change // to / B. in line 2: change // to % C. add this statement after line 1: if value2 == 0 : return 0 Answer D. add this statement after line 1: if value2 == 0 : return Title Section

Which statement should be added to this code snippet to avoid a run-time error? 5.4 Return Values

33. 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. the function returns 600 B. the function returns -600 C. an error occurs and aborts the program D. a special value of NONE will be returned from the function Answer Title Section

What is wrong with the following code snippet? 5.4 Return Values

34. How many return statements can be included in a function? A. one and only one B. must include at least one, but no more than two C. must include at least one, but there is no limit Answer D. none, one, or infinite Title Section

How many return statements can be included in a function? 5.4 Return Values

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 Answer B. to find the remainder of n divided by 2 C. to find the value of n divided by 2 D. to determine if n is positive or negative Title Section

What is the purpose of this code snippet? 5.4 Return Values

36. When should a computation be turned into a function? A. when it may not be used B. when it is only used once C. when it may be used more than once Answer D. only if it contains complex mathematically equations Title Section

file:///C:/Horstmann1e TB/ch05.testbank.xhtml

When should a computation be turned into a function? 5.4 Return Values

8/21

10/24/2017

Testbank (Chapter 5)

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. Answer B. The input and print statements must reside in a function named main. C. The variable num and the parameter variable n must have the same name. D. An else clause must be added to the if statement. Title Section

What is wrong with this program for determining if an integer is even or odd? 5.4 Return Values

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

A. A return statement must be added at the end of the function B. Both occurrences of elif must be replaced with if C. The = must be replaced with < and > D. Nothing is wrong with the function Answer Title Section

What is wrong with the function? 5.4 Return Values

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 amount of tax due is not computed correctly B. The function must print a value C. The function must return a value Answer D. The function must take an additional parameter Title Section

What is wrong with the function? 5.4 Return Values

40. 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 Answer B. to insert a temporary implementation of a function that can be refined later file:///C:/Horstmann1e TB/ch05.testbank.xhtml

9/21

10/24/2017

Testbank (Chapter 5)

C. to provide a function that can only be included in an assignment statement D. only used when the function needs to produce output Title Section

What is the purpose of methods without a return value? 5.5 Functions Without Return Values

41. 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) B. drawShape("triangle") Answer C. drawShape D. value = drawShape("triangle") Title star triangle? Section

Which function call correctly invokes the partial drawShape function listed below and prints a 5.5 Functions Without Return Values

42. 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)) Answer B. print(incrementIt(5)) C. print(decrementIt(5)) D. print(doubleIt(5)) Title Section

Which of the following function calls is unreasonable? 5.5 Functions Without Return Values

43. 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? file:///C:/Horstmann1e TB/ch05.testbank.xhtml

10/21

10/24/2017

Testbank (Chapter 5)

A. The indenting is wrong. All of the lines should be indented by the same amount. B. The calculation is wrong. The result variable will have something other than n-factorial stored in it. C. The function is missing a line. A print statement must be added at the end of it. Answer D. The function is missing a line. A return statement must be added at the end of it. What is wrong with the function that is supposed to compute n-factorial? 5.5 Functions Without Return Values

Title Section

44. 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

B. def die(low, high) : return randint(low, high) Answer

C. def die(high) : return randint(0, high)

D. def die(low, high) : return high % low Which implementation makes the die function reusable for other types of die? 5.6 Problem Solving: Reusable Functions

Title Section

45. Given these two separate functions, which implemenation combines them into one reusable function? def sixSidedDie() : return randint(1, 6) def fourSidedDie() : return randint(1, 4)

A. def die(low, high) : return

B. def die(low, high) : return high % low

C. def die(high) : return randint(0, high)

D. def die(low, high) : return randint(low, high) Answer file:///C:/Horstmann1e TB/ch05.testbank.xhtml

11/21

10/24/2017

Testbank (Chapter 5)

Title function? Section

Given these two separate functions, which implemenation combines them into one reusable 5.6 Problem Solving: Reusable Functions

46. What is stepwise refinement? A. The process of unit testing B. The design of pseudocode for black-box methods C. The process of breaking complex problems down into smaller, manageable steps Answer D. The use of a temporary implementation of a function that can be improved later Title Section

What is ...


Similar Free PDFs