Quiz prap chapter 5 and 6

¡Supera tus tareas y exámenes ahora con Quizwiz!

func-6-2: Consider the following Python code. Note that line numbers are included on the left. 1 def pow(b, p): 2 y = b ** p 3 return y 4 5 def square(x): 6 a = pow(x, 2) 7 return a 8 9 n = 5 10 result = square(n) 11 print(result) What does this function print?

(A) 25 The function square returns the square of its input (via a call to pow)

In Python a module is:

(A) A file containing Python definitions and statements intended for use in other Python programs.

What is a function in Python?

(A) A named sequence of statements.---

What is a local variable?

(A) A temporary variable that is only used inside a function.....Yes, a local variable is a temporary variable that is only known (only exists) in the function it is defined in.

To find out information on the standard modules available with Python you should:

(A) Go to the Python Documentation site--- The site contains a listing of all the standard modules that are available with Python.

What will the following function return? def addEm(x, y, z): print(x + y + z)

(A) None---We have accidentally used print where we mean return. Therefore, the function will return the value None by default. This is a VERY COMMON mistake so watch out! This mistake is also particularly difficult to find because when you run the function the output looks the same. It is not until you try to assign its value to a variable that you can notice a difference.

Consider the following code: def square(x): runningtotal = 0 for counter in range(x): runningtotal = runningtotal + x return runningtotal What happens if you put the initialization of runningtotal (the line runningtotal = 0) inside the for loop as the first instruction in the loop?

(A) The square function will return x instead of x * x ,,,,,The variable runningtotal will be reset to 0 each time through the loop. However because this assignment happens as the first instruction, the next instruction in the loop will set it back to x. When the loop finishes, it will have the value x, which is what is returned.

True or false: A function can be called several times by placing a function call in the body of a

(A) True Yes, you can call a function multiple times by putting the call in a loop.

Which of the following is a valid function header (first line of a function definition)? (A) def drawCircle(t): (B) def drawCircle: (C) drawCircle(t, sz): (D) def drawCircle(t, sz)

(A) def drawCircle(t): A function may take zero or more parameters. It does not have to have two. In this case the size of the circle might be specified in the body of the function.

Which statement allows you to use the math module in your program?

(A) import math--The module must be imported before you can use anything declared inside the module.

Which of the following is the correct way to reference the value pi within the math module. Assume you have already imported the math module. (A) math.pi (B) math(pi) (C) pi.math (D) math->pi

(A) math.pi ---> To invoke or reference something contained in a module you use the dot (.) notation.

The correct code to generate a random number between 1 and 100 (inclusive) is:

(A) prob = random.randrange(1, 101) ---This will generate a number between 1 and 101, but does not include 101.

One reason that lotteries don't use computers to generate random numbers is:

(B) Because computers don't really generate random numbers, they generate pseudo-random numbers. ---> Computers generate random numbers using a deterministic algorithm. This means that if anyone ever found out the algorithm they could accurately predict the next value to be generated and would always win the lottery.

True / False: All standard Python modules will work in activecode.

(B) False--Only turtle, math, and random have been ported to work in activecode at this time.

What is a variable's scope?

(B) The range of statements in the code where a variable can be accessed.

What is one main purpose of a function?

(B) To help the programmer organize programs into chunks that match how they think about the solution to the problem.----While functions are not required, they help the programmer better think about the solution by organizing pieces of the solution into logical chunks that can be reused.

Can you use the same name for a local variable as a global variable?

(B) Yes, but it is considered bad form. it is generally considered bad style because of the potential for the programmer to get confused. If you must use global variables (also generally bad form) make sure they have unique names.

What is wrong with the following function definition: def addEm(x, y, z): return x + y + z print('the answer is', x + y + z)

(B) You should not have any statements in a function after the return statement. Once the function gets to the return statement it will immediately stop executing the function.

What is the name of the following function? def drawSquare(t, sz): """Make turtle t draw a square of with side sz.""" for i in range(4): t.forward(sz) t.left(90)

(B) drawSquare Yes, the name of the function is given after the keyword def and before the list of parameters.

Which module would you most likely use if you were writing a function to simulate rolling dice?

(B) the random module

What are the parameters of the following function? def drawSquare(t, sz): """Make turtle t draw a square of with side sz.""" for i in range(4): t.forward(sz) t.left(90)

(C) t, sz Yes, the function specifies two parameters: t and sz.

func-6-1: Consider the following Python code. Note that line numbers are included on the left. 1 def pow(b, p): 2 y = b ** p 3 return y 4 5 def square(x): 6 a = pow(x, 2) 7 return a 8 9 n = 5 10 result = square(n) 11 print(result) 12 Which of the following best reflects the order in which these lines of code are processed in Python?

(E) 1, 5, 9, 10, 5, 6, 1, 2, 3, 6, 7, 10, 11 Python starts at line 1, notices that it is a function definition and skips over all of the lines in the function definition until it finds a line that it no longer included in the function (line 5). It then notices line 5 is also a function definition and again skips over the function body to line 9. On line 10 it notices it has a function to execute, so it goes back and executes that function. Notice that that function includes another function call. It returns from the function call and completes the assignment in line 6. Then it returns the result of line 7 and completes the assignment in line 10. Finally, it will go to line 11 after the function square and the assignment are complete.

Considering the function below, which of the following statements correctly invokes, or calls, this function (i.e., causes it to run)? Assume we already have a turtle named alex. def drawSquare(t, sz): """Make turtle t draw a square of with side sz.""" for i in range(4): t.forward(sz) t.left(90)

(E) drawSquare(alex, 10) Since alex was already previously defined and 10 is a value, we have passed in two correct values for this function.

In many programming languages (e.g. Java and C++), it is not possible to simply have statements sitting alone like this at the bottom of the program. They are required to be part of a special function that is automatically invoked by the operating system when the program is executed.

This special function is called main.

If the first thing after the function header is a string (some tools insist that it must be a triple-quoted string), it is called a

a docstring

This pattern of iterating the updating of a variable is commonly referred to as the

accumulator pattern. We refer to the variable as the accumulator.

The figure below shows this relationship. A function needs certain information to do its work. These values, often called

arguments or actual parameters

As we have already seen, you can call one function from within another. This ability to build functions by using other functions is called

composition.

When you are working with functions it is really important to know the order in which statements are executed. This is called the

flow of execution

We need to say a bit more about the parameters. In the definition, the parameter list is more specifically known as the

formal parameters. This list of names describes those things that the function will need to receive from the user of the function. When you use a function, you provide values to the formal parameters.

Functions that return values are sometimes called

fruitful functions

Defining a new function does not make the function run. To do that we need a

function call. Function calls contain the name of the function to be executed followed by a list of values in parentheses, called arguments, which are assigned to the parameters in the function definition.

This process of breaking a problem into smaller subproblems is called

functional decomposition.

The variable y only exists while the function is being executed — we call this its

lifetime.

A shadow means

that the global variable cannot be accessed by Python because the local variable will be found first. This is another good reason not to use global variables. As you can see, it makes your code confusing and difficult to understand.


Conjuntos de estudio relacionados

Med Records Management- Med Records Basics

View Set

Google Ads :Understand the Google Ads Auction

View Set

Missed Questions Study Guide Chapter 5

View Set

Theology - Chapter 3 Directed Reading

View Set

Physics Work, Energy, Momentum and Power Homework

View Set