Chapter 8 study terms-cs133p

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What is the difference between a tab ('\t') and a sequence of spaces? (A) A tab will line up items in a second column, regardless of how many characters were in the first column, while spaces will not. (B) There is no difference (C) A tab is wider than a sequence of spaces (D) You must use tabs for creating tables. You cannot use spaces.

(A) A tab will line up items in a second column, regardless of how many characters were in the first column, while spaces will not.

Which type of loop can be used to perform the following iteration: You choose a positive integer at random and then print the numbers from 1 up to and including the selected integer. (A) a for-loop or a while-loop (B) only a for-loop (C) only a while-loop

a) for-loop or a while-loop

it is often said that a for loop creates a ---- because we definitely know how many times we are going to iterate.

definite iteration

The backslash character in '\t' indicates the beginning of an

escape sequence

The amount of each color, sometimes called the

intensity of the color

Repeated execution of a sequence of statements is called

iteration.

The sequence \n represents a

newline.

so we'll do the next best thing: we will use a price of zero to mean "this is my last item." In this program, zero is a-----, a value used to signal the end of the loop. Here's the code:

sentinel value

The string '\t' represents a

tab character.

What would the image produced from ActiveCode box 16 look like if you replaced the lines: newred = 255 - p.getRed() newgreen = 255 - p.getGreen() newblue = 255 - p.getBlue() with the lines: newred = p.getRed() newgreen = 0 newblue = 0

(A) It would look like a red-washed version of the bell image Because we are removing the green and the blue values, but keeping the variation of the red the same, you will get the same image, but it will look like it has been bathed in red.

iter-9-3: What will the following nested for-loop print? (Note, if you are having trouble with this question, review CodeLens 3). for i in range(3): for j in range(2): print(i, j) a. 0 0 0 1 1 0 1 1 2 0 2 1 b. 0 0 1 0 2 0 0 1 1 1 2 1 c. 0 0 0 1 0 2 1 0 1 1 1 2 d. 0 1 0 1 0 1

(A) Output a i will start with a value of 0 and then j will iterate from 0 to 1. Next, i will be 1 and j will iterate from 0 to 1. Finally, i will be 2 and j will iterate from 0 to 1.

In the random walk program in this section, what does the isInScreen function do? (A) Returns True if the turtle is still on the screen and False if the turtle is no longer on the screen. (B) Uses a while loop to move the turtle randomly until it goes off the screen. (C) Turns the turtle right or left at random and moves the turtle forward 50. (D) Calculates and returns the position of the turtle in the window.

(A) Returns True if the turtle is still on the screen and False if the turtle is no longer on the screen. The isInScreen function computes the boolean test of whether the turtle is still in the window. It makes the condition of the while loop in the main part of the code simpler.

True or False: You can rewrite any for-loop as a while-loop.

(A) True--- Although the while loop uses a different syntax, it is just as powerful as a for-loop and often more flexible.

The following code contains an infinite loop. Which is the best explanation for why the loop does not terminate? n = 10 answer = 1 while n > 0: answer = answer + n n = n + 1 print(answer) (A) n starts at 10 and is incremented by 1 each time through the loop, so it will always be positive (B) answer starts at 1 and is incremented by n each time, so it will always be positive (C) You cannot compare n to 0 in while loop. You must compare it to another variable. (D) In the while loop body, we must set n to False, and this code does not do that.

(A) n starts at 10 and is incremented by 1 each time through the loop, so it will always be positive... The loop will run as long as n is positive. In this case, we can see that n will never become non-positive.

What is printed by this code? n = 1 x = 2 while n < 5: n = n + 1 x = x + 1 n = n + 2 x = x + n print(n, x) (A) 4 7 (B) 5 7 (C) 7 15

(C) 7 15 After n becomes 5 and the test would be False, but the test does not actually come until after the end of the loop - only then stopping execution of the repetition of the loop.

Consider the code that prints the 3n+1 sequence in ActiveCode box 6. Will the while loop in this code always terminate for any positive integer value of n? (A) Yes. (B) No. (C) No one knows.

(C) No one knows. That this sequence terminates for all values of n has not been proven or disproven so no one knows whether the while loop will always terminate or not.

The image module defines two classes:

Image and Pixel.

The specific color depends on a formula that mixes various amounts of three basic colors: red, green, and blue. This technique for creating color is known as the

RGB Color Model.

The body of the loop should change the value of one or more variables so that eventually the condition becomes False and the loop terminates. Otherwise the loop will repeat forever.

This is called an infinite loop

In order to manipulate an image, we need to be able to access individual pixels. This capability is provided by a module called

image

The while statement provides

much more general mechanism for iterating. Similar to the if statement, it uses a boolean expression to control the flow of execution. The body of while will be repeated as long as the controlling boolean expression evaluates to True. More formally, here is the flow of execution for a while statement: Evaluate the condition, yielding False or True. If the condition is False, exit the while statement and continue execution at the next statement. If the condition is True, execute each of the statements in the body and then go back to step 1. The body consists of all of the statements below the header with the same indentation. This type of flow is called a loop because the third step loops back around to the top. Notice that if the condition is False the first time through the loop, the statements inside the loop are never executed.

Image processing

refers to the ability to manipulate the individual pixels in a digital image. In order to process all of the pixels,

. Indefinite iteration

simply means that we don't know how many times we will repeat but eventually the condition controlling the iteration will fail and the iteration will stop.

Nested iteration

simply means that we will place one iteration construct inside of another. We will call these two iterations the outer iteration and the inner iteration.


Set pelajaran terkait

APES Unit 9: Chapter 21; Chapter 17 AP Environmental Science

View Set

Team Communication/Difficult Conversations

View Set

Chapter 6 Interactive Assignment

View Set

Articles 200 - 250 - Wiring & Protection

View Set