quizlet

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

Given the code snippet below, what is returned by the function call: mystery(5,3)? def mystery(num1, num2) : result = num1 * num2 return result

15

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()

16

Consider the following function: def squareArea(sideLength) : return sideLength ** 2 What is the value of squareArea(3)?

9

A stub function is

A function that acts as a placeholder and returns a simple value so another function can be tested

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"

Another return statement needs to be added to the function

When hand-tracing functions, the values for the parameter variables:

Are determined by the arguments supplied in the code that invokes the function

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()

There is no recursive invocation in this code segment

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))

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

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?

Write one function that reads a letter grade and returns the number equivalent, and one function that computes the average of three numbers.

How many return statements can be included in a function?

Zero or more

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?

avg, total, i, iSquared

One advantage of designing functions as black boxes is that

many programmers can work on the same project without knowing the internal implementation details of functions.

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?

return area

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

scope

A ___________________________ is a sequence of instructions with a name.

function

Given the following code snippet, what is considered an argument(s)? def mystery(num1, num2) : result = num1 ** num2 return result mystery(10, 2)

10, 2

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()

3

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

3.141

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

4

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?

40

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

5

Consider the following function: def w(x, y) : z = x + y return z What is the function's name?

w

When should a computation be turned into a function?

when it may be used more than once

What Python statement exits a function and gives the result to the caller?

return

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

Debugger

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

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

Which of the following statements is true about functions in Python?

Functions can have multiple arguments and can return one return value.

Why is hand-tracing or manually walking through the execution of a function helpful?

It is an effective way to understand a function's subtle aspects

Parameter variables should not be changed within the body of a function because

It is confusing because it mixes the concept of a parameter with that of a variable

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?

Line 3

What is the output from the following Python program? def main() : a = 10 r = cubeVolume() print(r) def cubeVolume() : return a ** 3 main()

Nothing, there is an error.

The term Black Box is used with functions because

Only the specification matters; the implementation is not important.

Which of the following is NOT a good practice when developing a computer program?

Put as many statements as possible into the main function

Which debugging command allows you to quickly run an entire function instead of examining its body a line at a time?

Step over

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)?

The argument is 56.75, and the return value is 57

Consider the following code segment: def f1(): print("A", end="") def f2(): f1() print("B", end="") What output is generated when it runs?

The code segment does not display any output.

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"

The function definition must appear before the function is called.

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?

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

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

The function must return a value

What is stepwise refinement?

The process of breaking complex problems down into smaller, manageable steps

Which of the following statements about variables is true?

The same variable name can be used in two different methods.

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?

There are no recursive function calls in the program

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, b

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)

add the statement global total after line 2

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

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

What is supplied to a function when it is called?

arguments

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?

average = avg(2, 3.14, 4, 5)

The location where the debugger stops executing your program so that you can inspect the values of variables is called a(n):

breakpoint

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?

fcabeg

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?

def boxVolume(length, width, height) :

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)

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

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)

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

You are writing a function that converts from Liters to Gallons. Which function header is the best?

def litersToGallons(liters) :

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")

drawShape("triangle")

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?

n

Given the following code snippet, what is considered a parameter variable(s)? def mystery(num1, num2) : result = num1 ** num2 return result mystery(10, 2)

num1, num2

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?

print(printIt(5))

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:

stub

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

the function must be defined before the statement that calls it

The purpose of a function that does not return a value is

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

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?

total = calc(2, 3)


Ensembles d'études connexes

Inventory True/False Chapter 11 & 13

View Set

Enlightenment Ideas in the Declaration of Independence

View Set

COMM 1200 FINAL- Chapter 17: Persuasive Speaking Review Questions

View Set

Ethics_TOPIC 5: THE IMPACT OF CULTURE AND TIME ON BUSINESS ETHICS

View Set

Physics 222: Chapter 21 Homework

View Set

International Management Exam 2 Review

View Set

Cause and Effect Graphic Organizers

View Set

PSY 103 - Developmental Psychology

View Set