Chapter 5

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

What is returned from the round function when it is called? 1. 3.14159 2. 3.141 3. 3.14 4. 3.1

2. 3.141

What is the output from the code snippet? 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()

1 ( Your Answer )

Identify the argument(s) in the given code snippet Given the following code snippet, what is considered an argument(s)? def mystery(num1, num2) : result = num1 ** num2 return result mystery(10, 2) 1. 10, 2 2. num1, num2 3. result 4. mystery

1. 10, 2

Trace a main program and function call 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? 1. 2 2. 4 3. 8 4. Python reports an error because doubleIt does not contain a return statement

1. 2

What is the result of the given function call? Note: solve the functions calls in the parenthesis first, then these values are passed to the outer function call 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 1. 225 2. 15 3. 30 4. 0

1. 225

What is wrong with this program for determining if an integer is even or odd? 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" 1. The function definition must appear before the function is called. 2. The input and print statements must reside in a function named main. 3. The variable num and the parameter variable n must have the same name. 4. An else clause must be added to the if statement.

1. The function definition must appear before the function is called.

What is supplied to a function when it is called? What ican be supplied to a function when it is called? 1. arguments 2. numbers 3. return values 4. variables

1. arguments

Which variables are considered local variables? 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? 1. avg, total, i, iSquared 2. i, iSquared 3. avg, total 4. i

1. avg, total, i, iSquared

Which of the following statements correctly defines a function? Which of the following statements correctly defines a function? 1. def functionName(parameterName1, parameterName2) : 2. def functionName(parameterName1, parameterName2) 3. functionName(parameterName1, parameterName2) : 4. functionName(parameterName1, parameterName2)

1. def functionName(parameterName1, parameterName2) :

Which of the following function calls is unreasonable? 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? 1. print(printIt(5)) 2. print(incrementIt(5)) 3. print(decrementIt(5)) 4. print(doubleIt(5))

1. print(printIt(5))

What is the purpose of methods without a return value The purpose of a function that does not return a value is 1. to package a repeated task as a function even though the task does not yield a value 2. to insert a temporary implementation of a function that can be refined later 3. to provide a function that can only be included in an assignment statement 4. only used when the function needs to produce output

1. to package a repeated task as a function even though the task does not yield a value

What is the result of the given function call? Given the code snippet below, what is returned by the function call: mystery(5,3)? def mystery(num1, num2) : result = num1 * num2 return result 1. 8 2. 15 3. 2 4. 0

2. 15

Identify the arguments in a program Consider the following program: def squareArea(sideLength) : return sideLength ** 2 a = squareArea(4) What are the arguments (actual parameters) in this program? 1. 2 2. 4 3. sideLength 4. squareArea

2. 4

How can the function's comment be improved? 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? 1. The @param line for width should be removed 2. Additional @param lines should be added for height and length 3. The first line should be expanded to describe how the function performs its calculation 4. The @return line should be removed

2. Additional @param lines should be added for height and length

Why is the term black box used with functions? The term Black Box is used with functions because 1. Only the implementation matters; the specification is not important. 2. Only the specification matters; the implementation is not important. 3. Only the arguments matter; the return value is not important. 4. Only the return value matters; the arguments are not important.

2. Only the specification matters; the implementation is not important.

Which statement about variable declarations within functions is true? 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)) 1. This multiple use of the same variable perfect will not compile because the scopes overlap 2. While this is legal and will compile in Python, it is confusing 3. Because the scopes of these variables do not overlap, there is no problem 4. This situation rarely occurs and the compiler always issues a warning

2. While this is legal and will compile in Python, it is confusing

Which call correctly invokes a function? 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("*****") 1. drawShape(triangle) 2. drawShape("triangle") 3. drawShape 4. value = drawShape(triangle)

2. drawShape("triangle")

Identify the parameter variable(s) in the given code snippet Given the following code snippet, what is considered a parameter variable(s)? def mystery(num1, num2) : result = num1 ** num2 return result mystery(10, 2) 1. 10, 2 2. num1, num2 3. result 4. mystery

2. num1, num2

What Python statement exits the function and gives the result to the caller? What Python statement exits a function and gives the result to the caller? 1. def 2. return 3. send 4. result

2. return

What is wrong with the function that is supposed to compute n-factorial? he 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? 1. The indenting is wrong. All of the lines should be indented by the same amount. 2. The calculation is wrong. The result variable will have something other than n-factorial stored in it. 3. The function is missing a line. A print statement must be added at the end of it. 4. The function is missing a line. A return statement must be added at the end of it.

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

Which statement should be added or modified to remove the run-time error in a code snippet? 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 1. in line 2: change // to / 2. in line 2: change // to % 3. add this statement after line 1: if value2 == 0 : return 0 4. add this statement after line 1: if value2 == 0 : return

3. add this statement after line 1: if value2 == 0 : return 0

Which statement causes the function to exit immediately? Which statement causes the following function to exit immediately? def mystery(num1, num2) : result = num1 ** num2 return result mystery(10, 2) 1. mystery(10,2) 2. result = num1 ** num2 3. return result 4. None of the statements cause the function to exit immediately.

3. return result

Given the following code, what is printed in the console? def function1(passX): passX = passX +1 function2(passX) def function2(passX): passX = passX *2 function3(passX) def function3(passX): for count in range(passX): print ("*", end = '') def function4(passX): passX = passX **2 return passx function1(3) 1. 9 2. ********* (9 stars) 3. 8 4. ******** (8 stars)

4. ******** (8 stars)

Trace a nested function call Consider the following function: def squareArea(sideLength) : return sideLength ** 2 What is the value of squareArea(squareArea(2))? 1. 2 2. 4 3. 8 4. 16

4. 16

Trace a function call Consider the following function: def mystery(a, b) : result = (a - b) * (a + b) return result What is the result of calling mystery(3, 2)? 1. 2 2. 3 3. 4 4. 5

4. 5

Trace a function call Consider the following function: def squareArea(sideLength) : return sideLength ** 2 What is the value of squareArea(3)? 1. 2 2. 3 3. 6 4. 9

4. 9

What is the output from a code snippet involving multiple functions? What is the output from the following Python program? def main() : a = 10 r = cubeVolume() print(r) def cubeVolume() : return a ** 3 main() 1. 10 2. 30 3. 1000 4. Nothing, there is an error.

4. Nothing, there is an error.

How many return statements can be included in a function? How many return statements can be included in a function? 1. Exactly one 2. One or two 3. One, two or more 4. Zero or more

4. Zero or more

What is wrong with the following code snippet? 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 1. the function returns 600 2. the function returns -600 3. an error occurs and aborts the program 4. a special value of None will be returned from the function

4. a special value of None will be returned from the function

How can global variables be updated in a function definition? Given the following code snippet, which statement correctly allows the function to update the global variabletotal? 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) 1. add the keyword global to line 1 2. line 1 already allows the total variable to be updated 3. move line 1 inside the function definition 4. add the statement global total after line 2

4. add the statement global total after line 2

Select the best header for a function that computes the volume of a box 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? 1. def boxVolume() : 2. def boxVolume(sideLength) : 3. def boxVolume(a, b, c) : 4. def boxVolume(length, width, height) :

4. def boxVolume(length, width, height) :

What is a sequence of instructions with a name called in Python? A ___________________________ is a sequence of instructions with a name. 1. variable 2. argument 3. parameter 4. function

4. function

Which of the following is a correct function call? 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? 1. total = calc() 2. total = calc(2) 3. total = calc("2", "3") 4. total = calc(2, 3)

4. total = calc(2, 3)


Ensembles d'études connexes

(BUS 256) Ch. 4 - Strategic Job Analysis & Competency Modeling (Quiz Questions)

View Set

Macroeconomics - Chapter 9: Long-Run Economic Growth

View Set

SEJPME II - Introduction To Joint Fundamentals

View Set

Team Quizzes (Chapters 14, 16, 17, 18, 19, 20)

View Set

Principles of Macroeconomics - Ch. 12-15

View Set