Exam 2: Pthon: Estimating PI: Math and Random Methods, Selection and Boolean Expressions, the Print Function

arrow
  • Select Tags
search iconSearch Question
flashcardsStudy Flashcards
  • Select Tags

Case Study 2: >>> acc = 0 >>> for x in range(1, 6): acc = acc + x >>> acc 15 -Refer to the session in the accompanying Case Study 2. Explain what pattern this code implements and why it is useful.

Free
(Essay)
4.8/5
(38)
Correct Answer:
Verified

The code implements a common problem-solving pattern known as the accumulator pattern. This common pattern comes up often. Your ability to recognize the pattern and then implement it will be especially useful as you encounter new problems that need to be solved. As an example, consider the simple problem of computing the sum of the first five integer numbers. Of course, this is quite easy because we can just evaluate the expression 1+2+3+4+5. But what if we wanted to sum the first ten integers? Or perhaps the first hundred? In this case, we would find that the size of the expression would become quite long. To remedy this, we can develop a more general solution that uses iteration.

Another name for an if statement is a selection statement.

Free
(True/False)
4.8/5
(38)
Correct Answer:
Verified

True

To create a random number in Python, use the ____ function.

Free
(Multiple Choice)
4.8/5
(44)
Correct Answer:
Verified

B

Explain the difference between the "=" in line 1 and the "==" in line 2 in the code sample below: 1. >>> apple = 25 2. >>> apple == 25 3. True

(Essay)
4.7/5
(36)

1. Match each definition with its term. -Contains a question and other groups of statements that may or may not be executed, depending on the result of the question.

(Multiple Choice)
4.8/5
(46)

Case Study 3: 1. if <condition>: 2. if <condition>: 3. <statements> 4. else: 5. <statements> 6. else: 7. if <condition>: 8. <statements> 9. else: 10. <statements> -Refer to the code in the accompanying case study. Under what circumstances are the statements on line 10 executed?

(Multiple Choice)
4.8/5
(29)

The code in the accompanying case study 2 (see below) is used to calculate a running product. >>> acc = 0 >>> for x in range(1, 6): acc = acc + x >>> acc 15

(True/False)
4.9/5
(34)

Case Study 1: >>> import math >>> numSides = 8 >>> innerAngleB = 360.0 / numSides >>> halfAngleA = innerAngleB / 2 >>> oneHalfSideS = math.sin(math.radians(halfAngleA)) >>> sideS = oneHalfSideS * 2 >>> polygonCircumference = numSides * sideS >>> pi = polygonCircumference / 2 >>> pi 3.0614674589207183 -Refer to the session in the accompanying case study 1. How many sides does the polygon in this approximation have?

(Multiple Choice)
4.8/5
(31)

Why are decisions referred to as "selection" in computer science?

(Essay)
4.9/5
(43)

Which of the following is a constant in the math module?

(Multiple Choice)
4.9/5
(29)

What equation relates pi to the circumference of a circle?

(Multiple Choice)
4.8/5
(35)

1. Match each definition with its term. -Compares two data values.

(Multiple Choice)
4.8/5
(41)

1. Match each definition with its term. -and, or, and not

(Multiple Choice)
4.9/5
(39)

Explain how to modify the coordinate system of the drawing window when using the turtle module.

(Essay)
4.8/5
(43)

Case Study 2: >>> acc = 0 >>> for x in range(1, 6): acc = acc + x >>> acc 15 -Refer to the session in the accompanying case study 2. What type of variable is acc?

(Multiple Choice)
4.9/5
(40)

The relational operator that means "not equal" is represented with which operator in Python?

(Multiple Choice)
4.9/5
(38)

Explain how a Monte Carlo simulation can be used to calculate the value of pi.

(Essay)
4.7/5
(29)

In order to use the sqrt() function, you must use which of the following Python statements?

(Multiple Choice)
4.9/5
(36)

Case Study 3: 1. if <condition>: 2. if <condition>: 3. <statements> 4. else: 5. <statements> 6. else: 7. if <condition>: 8. <statements> 9. else: 10. <statements> -Refer to the code in the accompanying Case study 3. Why is this section of code referred to as "nested selection"?

(Essay)
4.7/5
(46)

What are some patterns to keep in mind when implementing the Leibniz formula with Python?

(Essay)
4.8/5
(39)
Showing 1 - 20 of 33
close modal

Filters

  • Essay(0)
  • Multiple Choice(0)
  • Short Answer(0)
  • True False(0)
  • Matching(0)