1) What is printed by the following code snippet?
print("The answers are:", 4 + 3 * 2, 7 * 5 - 24)
A) The answers are: 10 11
2) Although the following code statement is valid, print(10/0), what will happen when this code is executed?
A) The error message ZeroDivisionError: int division or modulo by zero is displayed
3) What is wrong with the following code snippet?
num = 78A
A) 78A is not a valid value in a Python program
4) What is the value of result after the following code snippet?
num1 = 10
num2 = 20
num3 = 2
result = num1 / num2 / num3
print(result)
A) 0.25
5) What is the value of 4 ** 3?
A) 64
6) What is returned by the function: abs(x) if x = 5.64?
A) 5.64
7) Consider the following code segment:
x = 5
y = 3
z = 2
result = x // y + x % z
After this code segment, the value of result is:
A) 2
8) What is the value of length after this statement: length = len("Good Morning")?
A) 12
9) What will be printed by the following code snippet?
name = "Ravi Avalon"
counter = name.count("av")
print(counter)
A) 1
10) What will be printed by the following code snippet?
name = "Dino the Dinosaur"
counter = name.count("Di")
print(counter)
A) 2
11) Which of the following statements returns the number of blank spaces contained in the string sentence?
A) sentence.count(” “)
12) Which of the following checks to see if the string variable sentence starts with the string "Dear"?
A) if sentence.startswith(“Dear”) :
13) What value is printed by the following code snippet?
name = "John R. Johnson"
firstName = "John"
location = name.find(firstName)
print(location)
A) 0
14) Which of the following checks to see if there is a comma anywhere in the string variable name?
A) if "," in name :
15) The following code snippet contains an error. What is the error?
cost = int(input("Enter the cost: "))
if cost > 100
cost = cost - 10
print("Discounted cost:", cost)
A) Syntax error: missing colon after if statement
16) Which of the following statements can be used to validate whether the value a user entered for a grade is in the range 0 to 100, including both 0 and 100?
A) if grade >= 0 and grade <= 100 :
17) Review the code snippet below:
month = int(input("Enter your two digit birth month: "))
Which of the following statements checks that the user entered a valid month?
A) if month >= 1 and month <= 12
18) What is the output of the following code snippet if count contains 56?
if count % 2 == 0 :
print("Count is an even number")
else :
print("Count is an odd number")
A) Nothing, there is a syntax error
19) Consider the following code segment:
s1 = "CAT"
s2 = "cat"
Which of the following if statements has a condition that evaluates to True?
A) if s1 < s2 :
20) Given the following list of strings, what is the correct order using lexicographic ordering: "Ann", "amy", "Heather", "hanna", "joe", "john", "Leo", "Jim" ?
A) Ann, Heather, Jim, Leo, amy, hanna, joe, john
21) How many times will the following loop run?
i = 0
while i < 10 :
print(i)
i = i + 1
A) 10
22) What is the output of the following code snippet?
for i in range(4) :
for j in range(3) :
print("*", end="")
print()
A) Prints 4 rows of 3 asterisks each
23) How many times does the loop execute in the following code fragment?
for i in range(0, 50, 4) :
print(i)
A) 13
24) How many times does the following code snippet display “Loop Execution”?
for i in range(0, 10) :
print("Loop Execution")
A) Ten times.
25) What values does counter variable i assume when this loop executes?
for i in range(20, 2, -6) :
print(i, end = ", ")
A) 20, 14, 8
26) Is the following code snippet legal?
b = False
while b != b :
print("Do you think in Python?")
A) Yes, it is legal but does not print anything.
27) How many times will the output line be printed in the following code snippet?
for num2 in range(1, 4) :
for num1 in range(0, 3) :
print(num2, " ", num1)
A) 9 times
28) In the following code snippet, how many times will “Hello” be printed?
for i in range(0, 10) :
for j in range(1, 5) :
print("Hello")
A) 40
29) How many copies of the letter A will the following code segment display?
for i in range(100) :
for j in range(5) :
print("A")
A) 500
30) Consider the following code segment. It is supposed to count the number of digits (0 – 9) in a string, text.
count = 0
for char in text :
____________________
count = count + 1
What line of code should be placed in the blank to achieve this goal?
A) if char >= “0” and char <= “9” :
31) Consider the following code segment. It is designed to identify the first location within a string, text where two adjacent characters are the same.
i = 1
found = False
while not found and i < len(text) :
____________________ :
found = True
else :
i = i + 1
What line of code should be placed in the blank to achieve this goal?
A) if text[i] == text[i – 1] :
32) Assume the following variable has been declared and given a value as shown:
from random import randint
number = randint(0, 27) * 2 + 3
What are the smallest and largest values that may be assigned to number?
A) 3, 57
33) How many times does the following code fragment display “Hi”?
i = 10
while i >= 0 :
print("Hi")
i = i - 1
A) 11 times
34) Which of the following statements correctly prints the result of simulating the toss of a pair of coins to get 0 (heads) or 1 (tails) for each coin?
A) print(randint(0, 1), randint(0, 1))
35) Consider a function named avg, which accepts four numbers and returns their average. Which of the following is a correct call to the function avg?
A) average = avg(2, 3.14, 4, 5)
36) Given the code snippet below, what is returned by the function call: mystery(mystery(5, 3), mystery(5, 3))?
def mystery(num1, num2) :
result = num1 * num2
return result
A) 225
37) The following function is supposed to compute the area of atriangle and return the area as the function’s result.
def triangleArea(base, height) :
area = base * height / 2
____________________
What line of code must be placed in the blank to achieve this goal?
A) return area
38) Consider the following program:
def main() :
a = 10
print(doTwice(a))
def doTwice(x) :
x = x * 2
x = x * 2
return x
main()
What output is generated when this program is run?
A) 40
39) What is the purpose of this code snippet?
def mystery(n) :
if n % 2 == 0 :
return True
else :
return False
A) to determine if n is even or odd
40) Consider the following functions:
def printIt(x) :
print(x)
def incrementIt(x) :
return x + 1
def decrementIt(x) :
return x - 1
def doubleIt(x) :
return x * 2
Which of the following function calls is not a reasonable thing to do?
A) print(printIt(5))
41) The following function is supposed to compute and display the value of n-factorial for integers greater than 0.
def factorial(n) :
result = 1
for i in range(1, n + 1) :
result = result * i
What is wrong with this function?
A) The function is missing a line. A print statement must be added at the end of it.
42) The function below randomly generates a number between 1 and 6 to represent a single die. Which implementation listed below allows for other types of die?
def die() :
return randint(1, 6)
A) def die(low, high) :
return randint(low, high)
43) Given a list values that contains the first ten prime numbers, which statement prints the index and value of each element in the list?
A) for i in range(10) :
print(i, values[i])
44) Consider the following code segment:
values = [4, 12, 0, 1, 5]
print(values[1])
What is displayed when it runs?
A) 12
45) Consider the following list:
values = [12, 4, 8, 16, 24]
Which statement displays the last element in the list?
A) print(values[4])
46) Given the definition of a list: names = [] which statement correctly adds "Harry" to the list?
A) names.append(“Harry”)
47) Which of the following statements looks for the name 'Bob' in the list names?
A) if “Bob” in names :
print(“found Bob”)
48) Given the following code snippet, what is the value of the variable indexValue?
states = ["Alaska", "Hawaii", "Florida", "Maine", "Ohio", "Florida"]
indexValue = states.index("Hawaii")
A) 1
49) Given the following code snippet, what is the content of the list states?
states = ["Alaska", "Hawaii", "Florida", "Maine", "Ohio", "Florida"]
states.pop()
A) [“Alaska”, “Hawaii”, “Florida”, “Maine”, “Ohio”]
50) Given the following code snippet, what are the contents of the list fullNames?
firstNames = ["Joe", "Jim", "Betsy", "Shelly"]
lastNames = states = ["Jones", "Patel", "Hicks", "Fisher"]
fullNames = firstNames + lastNames
A) [“Joe”, “Jim”, “Betsy”, “Shelly”, “Jones”, “Patel”, “Hicks”, “Fisher”]
51) What is the resulting value of this code snippet?
sum([1, 2, 3, 4, 5])
A) 15
52) What is the resulting content of the list values after this code snippet?
values = [1991, 1875, 1980, 1965, 2000]
values.sort()
A) [1875, 1965, 1980, 1991, 2000]
53) What is the resulting content of the list letters after this code snippet?
letters = list("abcdefg")
A) letters contains a list with the contents: ["a", "b", "c", "d", "e", "f", "g"]
54) What is stored in pos when the following code segment executes?
values = ["J", "Q", "X", "Z"]
pos = values.find("Y")
A) No value is stored because a runtime exception occurs
55) What is output by the following code segment?
values = ["Q", "W", "E", "R", "T", "Y"]
print(values[2 : 4])
A) [“E”, “R”]
56) Consider the following code segment:
x = [5, 3, 7]
y = x.pop()
print(y)
What is displayed when it executes?
A) 7
57) Which of the following statements opens a text file for reading?
A) infile = open(“myfile.txt”, “r”)
58) Which statement is used to close the file object opened with the following statement?
infile = open("test.txt", "r")
A) infile.close()
59) Consider the following code segment:
line = "hello world!"
parts = line.split()
print(parts)
What is displayed when this code segment runs?
A) [“hello”, “world!”]
60) What type of value is returned by the expression ord("A")?
A) Integer
61) 60) What type of value is returned by the expression ord("A")?
A) Integer
61) In the following code snippet, what does the "r" represent?
infile = open("input.txt", "r")
A) read
62) What will be stored in substrings after the following code snippet has run?
states = "Michigan,Maine,Minnesota,Montana,Mississippi"
substrings = states.split(",")
A) [“Michigan”, “Maine”, “Minnesota”, “Montana”, “Mississippi”]
63) Suppose the input file contains a person’s last name and age on the same line separated by a space. Which statements extract this information correctly?
A) record = inputFile.readline()
data = record.split()
name = data[0].rstrip()
age = int(data[1])
64) Which of the following code segments will display all of the lines in the file object named infile, assuming that it has successfully been opened for reading?
A) for line in infile :
print(line)
65) What is wrong with the following code snippet:
file = open("lyrics.txt", "w")
line = file.readline()
words = line.split()
print(words)
file.close()
A) The file has only been opened for writing, not reading.
66) What portable file format is commonly used to export data from a spreadsheet so that it can be read and processed by a Python program?
A) Comma-Separated Values (CSV)
67) In the following code snippet, what does the "w" represent?
outfile = open("output.txt", "w")
A) write
68) Which of the following is a possible output after the following code snippet is executed?
names = set(["Jane", "Joe", "Amy", "Lisa"])
names.add("Amber")
names.add("Zoe")
names.discard("Joe")
print(names)
A) {‘Amy’, ‘Lisa’, ‘Jane’, ‘Zoe’, ‘Amber’}
69) In the following code snippet, what is true about these sets?
names = set(["Jane", "Joe", "Amy", "Lisa"])
names1 = set(["Joe", "Amy", "Lisa"])
names2 = set(["Jane", "Joe"])
A) names2 is a subset of names
70) Given the following code snippet, which statement tests to see if all three sets are equal?
fruit = set(["apple", "banana", "grapes", "kiwi"])
fruit2 = set(["apple", "banana", "grapes", "kiwi"]
fruit3 = set(["apple", "banana", "pears", "kiwi"])
A) if fruit == fruit2 and fruit == fruit3 :
71) What is in the set fruit after the following code snippet?
fruit2 = set(["blueberry", "lemon", "grapes"])
fruit3 = set(["apple", "banana", "pears", "kiwi"])
fruit = fruit2.union(fruit3)
A) {‘blueberry’, ‘lemon’, ‘grapes’, ‘apple’, ‘banana’, ‘pears’, ‘kiwi’}
72) What method is used to produce a new set with the elements that are contained in both sets?
A) intersection()
73) One key difference between a set and a list is:
A) Set elements are not stored in any particular order
74) What method is used to produce a new set with the elements that belong to the first set but not the second?
A) difference()
75) What is the output of the following code snippet?
fibonacci = {1, 1, 2, 3, 5, 8}
primes = {2, 3, 5, 7, 11}
both = fibonacci.intersection(primes)
print(both)
A) {2, 3, 5}
76) What is the value of x after the following code segment executes?
x = {1, 2, 3}
x.add(1)
A) {1, 2, 3}
77) Consider the following code segment:
primes = {2, 3, 5, 7}
odds = {1, 3, 5, 7}
Which line of code will result in x containing {1, 2, 3, 5, 7}?
A) x = primes.union(odds)
78) What is stored in x at the end of this code segment?
primes = {2, 3, 5, 7}
odds = {1, 3, 5, 7}
x = primes.intersection(odds)
A) {3, 5, 7}
79) Consider the following code segment:
primes = {2, 3, 5, 7}
odds = {1, 3, 5, 7}
Which line of code will result in x containing {1}?
A) x = odds.difference(primes)
80) Which statement correctly creates a set named rainbow that contains the 7 colors in a rainbow?
A) colors = [“red”, “orange”, “yellow”, “green”, “blue”, “indigo”, “violet”]
rainbow = set(colors)
81) Which statement about lists and sets is correct?
A) In a list the elements are stored in order. In a set they are not stored in any particular order.
82) Which of the following statements creates a dictionary of favorite foods?
A) favoriteFoods = {“Peg”: “burgers”, “Cy”: “hotdogs”, “Bob”: “apple pie”}
83) Which operator tests to see if a key exists in a dictionary?
A) in
84) How do you add items to a dictionary?
A) [] operator
85) What are the keys in the following dictionary?
fruit = {"Apple": "Green", "Banana": "Yellow"}
A) Apple and Banana
86) What are the values in the following dictionary?
numbers = {1: 5.5, 2.0: 77, 3: 33}
A) 5.5, 77 and 33
87) Consider the following code segment:
fruit = {"Apple": "Green", "Banana": "Yellow"}
fruit["Plum"] = "Purple"
After it executes, what is the value of fruit?
A) {“Apple”: “Green”, “Banana”: “Yellow”, “Plum”: “Purple”}
88) Consider the following code segment:
fruit = {"Apple": "Green", "Banana": "Yellow"}
fruit["Apple"] = "Red"
After it executes, what is the value of fruit?
A) {“Apple”: “Red”, “Banana”: “Yellow”}
89) Which of the following statements checks to see if the key Apple is already in the dictionary fruit?
A) if “Apple” in fruit :
90) Assume that a dictionary has been initialized as shown below:
fruit = {"Apple": "Green", "Banana": "Yellow", "Plum": "Purple"}
Which statement prints the color of a banana?
A) print(fruit[“Banana”])
91) This function is supposed to recursively compute x to the power n, where x and n are both non-negative integers:
1. def power(x, n) :
2. if n == 0 :
3. ________________________________
4. else :
5. return x * power(x, n - 1)
What code should be placed in the blank to accomplish this goal?
A) return 1
92) Complete the code for the myFactorial recursive function shown below, which is intended to compute the factorial of the value passed to the function:
1. def myFactorial(n) :
2. if n == 1 :
3. return 1
4. else :
5. _______________________
A) return n * myFactorial(n – 1)
93) Complete the code for the myFactorial recursive function shown below, which is intended to compute the factorial of the value passed to the function:
1. def myFactorial(n) :
2. if _____________________________ :
3. return 1
4. else :
5. return n * myFactorial(n - 1)
A) n == 1
94) Complete the code for the myFactorial recursive function shown below, which is intended to compute the factorial of the value passed to the function:
1. def myFactorial(n) :
2. if n == 1 :
3. _____________________________
4. else :
5. return n * myFactorial(n - 1)
A) return 1
95) Complete the code for the calcPower recursive function shown below, which is intended to raise the base number passed into the function to the exponent power passed into the function:
1. def calcPower(base, exponent) :
2. answer = 0
3. if exponent == 0 :
4. answer = 1
5. else :
6. _____________________________
7. return answer
A) answer = base * calcPower(base, exponent – 1)
96) Consider the following code snippet for calculating Fibonacci numbers recursively:
1. def fib(n) :
2. # assumes n >= 0
3. if n <= 1 :
4. return n
5. else :
6. return fib(n - 1) + fib(n - 2)
Identify the terminating condition in this recursive function.
A) n <= 1
