APCSP Test Chapter 4,6,9

Ace your homework & exams now with Quizwiz!

What is printed by the following statements? s = "python rocks" print(s.count("o") + s.count("p"))

3

n the following code, what is the value of number the second time Python executes the loop? for number in [5, 4, 3, 2, 1, 0]: print("I have", number, "cookies. I'm going to eat one.")

4

how many lines does this code print? for number in [5, 4, 3, 2, 1, 0]: print("I have", number, "cookies. I'm going to eat one.")

6

What is a function in Python?

A named sequence of statements.

def pow(b, p): y = b ** p return y def square(x): a = pow(x, 2) return a n = 5 result = square(n) print(result) What does this function print?

A. 25

What is a local variable?

A. A temporary variable that is only used inside a function

What happens if you give range only one argument? For example: range(4)

A. It will generate a sequence starting at 0, with every number included up to but not including the argument it was passed.

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

A. None

In the command range(3, 10, 2), what does the second argument (10) specify?

A. Range should generate a sequence that stops before 10 (including 9).

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

How does python know what statements are contained in the loop body?

A. They are indented to the same degree from the loop header.

What is printed by the following statements? s = "python rocks" print(s[-3]) A. c B. k C. s D. Error, negative indices are illegal.

A. c

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

How many times is the letter o printed by the following statements? s = "python rocks" idx = 1 while idx < len(s): print(s[idx]) idx = idx + 2 A. 0 B. 1 C. 2

A.O

s = "python rocks" print(s[7:11] * 3) A. rockrockrock B. rock rock rock C. rocksrocksrocks D. Error, you cannot use repetition with slicing.

A.rockrockrock

What is printed by the following statements? s = "python rocks" print(len(s)) A. 11 B. 12

B. 12

How many times is the word HELLO printed by the following statements? s = "python rocks" for ch in s[3:8]: print("HELLO") A. 4 B. 5 C. 6 D. Error, the for statement cannot use slice.

B. 5

import turtle wn = turtle.Screen() alex = turtle.Turtle() alex.forward(150) alex.left(90) alex.forward(75) What does the line "import turtle" do?

B. It defines the module turtle which will allow you to create a Turtle object and draw with it.

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.

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

B. Yes, but it is considered bad form.

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

What is printed by the following statements? s = "python rocks" print(s[len(s)-5]) A. o B. r C. s D. Error, len(s) is 12 and there is no index 12.

B. r

Which range function call will produce the sequence 20, 15, 10, 5?

B. range(20, 3, -5)

What could the second parameter (12) in range(2, 12, 4) be replaced with and generate exactly the same sequence?

C. 11, 13, or 14

How many times is the word HELLO printed by the following statements? s = "python rocks" for ch in s: print("HELLO") A. 10 B. 11 C. 12 D. Error, the for statement needs to use the range function.

C. 12

What is printed by the following statements? v = 2.34567 print('{:.1f} {:.2f} {:.7f}'.format(v, v, v)) A. 2.34567 2.34567 2.34567 B. 2.3 2.34 2.34567 C. 2.3 2.35 2.3456700

C. 2.3 2.35 2.3456700

turtle-2-6: Why do we type turtle.Turtle() to get a new Turtle object?

C. The first "turtle" (before the period) tells Python that we are referring to the turtle module, which is where the object "Turtle" is found.

What is printed by the following statements: s = "Ball" s[0] = "C" print(s) A. Ball B. Call C. Error

C. error

What is printed by the following statements? s = "python rocks" print(s[3:8]) A. python B. rocks C. hon r D. Error, you cannot have two numbers inside the [ ].

C. hon r

What command correctly generates the sequence 2, 5, 8?

C. range(2, 10, 3)

What is printed by the following statements? x = 2 y = 6 print('sum of {} and {} is {}; product: {}.'.format( x, y, x+y, x*y))

C. sum of 2 and 6 is 8; product: 12.

func-1-5: 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

How many times is the letter o printed by the following statements? s = "python rocks" for idx in range(len(s)): if idx % 2 == 0: print(s[idx]) A. 0 B. 1 C. 2 D. Error, the for statement cannot have an if inside.

C.2

What is printed by the following statements: s = "ball" r = "" for item in s: r = item.upper() + r print(r) A. Ball B. BALL C. LLAB

C.LLAB

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

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)

Which direction does the Turtle face when it is created?

East

True or False: A Turtle object can have any name that follows the naming rules from Chapter 2.

True

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

True

Consider the following code: for aColor in ["yellow", "red", "green", "blue"]: alex.forward(50) alex.left(90) What does each iteration through the loop do?

draw one side of a square

Evaluate the following comparison: "dog" < "Dog"

false

Evaluate the following comparison: "dog" < "Doghouse"

false

What is printed by the following statements? s = "python rocks" print(s[3])

h

What is printed by the following statements? s = "python" excl = "!" print(s+excl*3)

python!!!

What is printed by the following statements? s = "python" t = "rocks" print(s + t)

pythonrocks

What is printed by the following statements? s = "python rocks" print(s[2] + s[-5])

tr

Evaluate the following comparison: "Dog" < "Doghouse"

true

What is printed by the following statements? s = "python rocks" print(s[1] * s.index("n"))

yyyyy


Related study sets

Ch. 12 Biology comprehension check

View Set

American History Chapter 23: The Great Depression

View Set

Chapter 16: Care of Patients Experiencing Urgent Alterations in Health

View Set