Docsity

APCSP Midterm Part 2 exam test sample verified source, Exams of Advanced Education

Toggle sidebar
bg1
1
/
12
APCSP Midterm Part 2 exam test sample verified source
1. We've written a function animate that moves a ball across the
screen like this:
def animate(event):
ball.move(5, 5)
How can we set up animate to be called every time a key on the
keyboard is
pressed down?: add_key_down_handler(animate)
2. In the following code, what will be the last number to print to the
screen before the program finishes?
for i in range(10):
if i % 2 == 0:
print(i) else:
print(2 * i): 18
3. How many times will the following code print "hello"?
for i in range(3, 8):
print("hello"): 5
4. How many times will the following code print "hello"?
for i in range(0, 8, 2):
print("hello"): 4
5.
Why
do
we
use
constant
variables?:
To avoid having numbers with unexplained meaning,
to give
values
a
readable
name,
and
to
let
us
change
our
program
by
only
changing
a
value
in
one
place
6. What will be the value of sum after this code
runs? START = 1
END = 5
sum = 0;
for i in range(START, END):
sum += i: 10
7.
Which
of
the
following
returns
a
random
number
between
1
and
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download APCSP Midterm Part 2 exam test sample verified source and more Exams Advanced Education in PDF only on Docsity!

APCSP Midterm Part 2 exam test sample verified source

  1. We've written a function animate that moves a ball across the screen like this: def animate(event): ball.move(5, 5) How can we set up animate to be called every time a key on the keyboard is pressed down?: add_key_down_handler(animate)
  2. In the following code, what will be the last number to print to the screen before the program finishes? for i in range(10): if i % 2 == 0: print(i) else: print(2 * i): 18
  3. How many times will the following code print "hello"? for i in range(3, 8): print("hello"): 5
  4. How many times will the following code print "hello"? for i in range(0, 8, 2): print("hello"): 4
  5. Why do we use constant variables?: To avoid having numbers with unexplained meaning, to give values a readable name, and to let us change our program by only changing a value in one place
  6. What will be the value of sum after this code runs? START = 1 END = 5 sum = 0; for i in range(START, END): sum += i: 10
  7. Which of the following returns a random number between 1 and

10?: ran-dom.randint(1,10)

  1. What type of error occurs when a program is expecting an integer value and the user enters a string?: ValueError
  2. We want to position a Circle on our screen to be at a random position, but we want to keep the entire shape on the screen. Which of the following will accomplish this given the following function? (Assume SIZE, HEIGHT, and WIDTH are properly defined): x = random.randint(SIZE,WIDTH-SIZE) y = random.randint(SIZE,HEIGHT-SIZE) draw_circle(x, y, SIZE)
  3. What is the output of the following program? def sum_to(num): sum = 0 for i in range(num+1): sum += i print(sum) x = 5 sum_to( x) print(x): 15 5
  4. How many parameters go into the function sum, and how many return values come out of the function sum? def sum(first, second, third): result = first + second + third print(first) print(second) print(third) return result: 3 parameters go in, 1 return value comes out
  5. "It's a bird! It's a plane! No, it's Superman!" We want to write a function "is_superman" that takes in two parameters "is_bird" and "is_plane" and returns True if it is in fact Superman, and False otherwise. If it's not a bird and it's not a plane, it must be Superman.

Which of the following functions is the correct implementation of is_super-man?: def is_superman(is_bird, is_plane): return not is_bird and not is_plane

  1. import random MAX_NUMBER = 10 def sum(x, y): result = x + y #Point A return result num1 = random.randint(0, MAX_NUMBER) num2 = random.randint(0, MAX_NUMBER) print( sum(num1, num2) ) Which variables exist at point A? In other words, which variables are in scope at point A? Which variables could you type at point A and the program would still work?: result, x, y, num1, num2, and MAX_NUMBER
  2. Which of the following is true for an API? I.Allows different devices to connect with each other II. Allows for Interactivity between devices and software III. Messenger program that takes requests and delivers the response back to the user IV. Allows the creation of applications that access the features or data of an operating system, application, or other services V.Specifies how software components should interact VI. Used when programming graphical user interface (GUI) components: I, II, III, IV, V, and VI
  3. What does API stand for in computer science?: Application Programming Interface
  4. What is the purpose of a return statement in a function?: Stops executing the function and returns the value
  5. Which of the following is true about return values?: Functions can have return values, but they are optional.
  6. What does the mystery function do in the code snippet below?

= [1, 2, 3, 4, 5]

  1. Consider the following code segment. aList • [20, 40, 60] bList • [10, 30, 50] aList • bList bList • [15, 45, 90] What is stored in aList?: [10, 30, 50]
  2. Consider the following code segment. The indices on the exam are different. They do not start at 0, but rather at 1. aList • ["dog", false, "bird"] Which of the following will throw an error? I.aList[0] II. aList[2] III. aList[3] IV. aList[4]: III and IV
  3. How would you create an empty list and assign it to aList?: aList
    • [ ]
  4. What does the following code print? time_of_day = ["morning", "afternoon", "evening"] for word in time_of_day: print("Good " + word): Good morning Good afternoon Good evening
  5. What will this code print? name = "Tim" name_list = list(name) for index, value in enumerate(name_list): print("Letter " + str(index+1) + ": " + name_list[index]): Letter 1: T Letter 2: i Letter 3: m
  1. Look at the following program: my_list = ["bananas", "oranges", "grapes", "pineapples", "apples"] # You pick the code that goes here... print(my_list) Pick the code that results in the following output: ['apples', 'bananas', 'grapes', 'oranges', 'pineapples']: my_list.sort()
  2. Look at the following program: my_list = ["bananas", "oranges", "grapes", "pineapples", "apples"] # You pick the code that goes here... print(my_list) Pick the code that results in the following output: ['pineapples', 'oranges', 'grapes', 'apples']: my_list.sort() my_list.reverse() my_list.remove("bananas")
  3. The AP Exam uses the following format to insert an item to a list and to generate a random integer from a to b, including a and b. INSERT(aList, i, value) RANDOM(a, b) Given the following code segment, how can you best describe its behavior? i • 1 FOR EACH x IN list { REMOVE(list, i) random • RANDOM(1, LENGTH(list)) INSERT(list, random, x) i • i + 1 }: This code shuffles the order of the numbers in the list by removing them and inserting them back in a random place.
  4. What kind of data structure is user_data in the following declaration?

my_list = [...] num = 0 for thing in my_list: num = num - thing print (num): 5, -10, 6

  1. What will be the output of the following program? my_list = [1, 2, 3, 4] num = 5 for index in range(len(my_list)): my_list.append(num + index) print (my_list): [1, 2, 3, 4, 5, 6, 7, 8]
  2. We want to simulate constantly flipping a coin until we get 3 heads in a row. What kind of loop should we use?: While Loop
  3. What will the following program print when run? for j in range(2): for i in range(6, 4, - 1): print (i): 6 5 6 5
  4. What is the output of the following program? result = 0 max = 5 for i in range(max):

result += i print(result): 10

52.. Which of the following returns a random number between 1 and 99?: ran-dom.randint(1,99)

  1. The AP Exam does not use for loops and while loops, but rather REPEAT or REPEAT UNTIL commands as shown below. REPEAT n TIMES {

} REPEAT UNTIL(condition) {

} Consider the following program, which is intended to print the sum of all the positive integers up to number. sum • 0 REPEAT number TIMES { sum • sum + number } DISPLAY sum Which of the following best describes the behavior of this program?: The program does not work as intended but rather it displays the number squared.

  1. The AP Exam uses the following relational (comparison) operators: =, ,` >, <, ,eand .dAs well, AND, OR and NOT are used instead of and, or and not. A county 911 system uses an automated computer program to dispatch one