1) To store a value for later use in Python, the programmer needs to create a:
A) variable
2) How is a value stored in a variable?
A) an assignment statement
3) What is the value of the variable num after the following code snippet?
num = 5
num2 = 6
num = num2 + 3
A) 9
4) What is the right way to assign the value of num + 10 to num2?
A) num2 = num + 10
5) What is wrong with the following code snippet?
2ndNum = 78
A) The 2ndNum variable is not a valid variable name
6) What is a variable called that should remain unchanged throughout your program?
A) a constant variable
7) Which of the following variables should be coded as a constant in Python?
A) pi: 3.14159
8) Which of the following variable names follows the Python naming convention for constants?
A) MAXSIZE
9) Why is it important to follow Python naming standards for variables representing constants?
A) it is good programming style
10) Which of the following is an appropriate constant name to represent the number of pencils in a pack?
A) NUM_PENCILS_PER_PACK = 12
11) A variable is:
A) A storage location with a name
12) What is wrong with this assignment statement?
num + 10 = num2
A) The left hand side of an assignment statement cannot include an operator.
13) What is wrong with the following code snippet?
num = 78A
A) 78A is not a valid value in a Python program
14) Which line of code creates a variable named x and initializes it to the integer 5?
A) x = 5
15) Which of the following items is an example of a floating-point literal?
A) 100000.0
16) Which of the following names is not a legal variable name?
A) bottle-volume
17) Which of the following statements about variable names is not correct?
A) Variable names can begin with a letter, an underscore or a number.
18) Which of the following names is the best for a constant variable holding the price of a can of soda?
A) SODA_PRICE
19) What convention is normally used when naming constants in a Python program?
A) Constant names are normally written in all capital letters.
20) What symbol is used to begin a comment in a Python program?
A) #
21) A numeric constant that appears in your code without explanation is known as a:
A) magic number
22) Which of the following statements correctly multiplies num1 times num2?
A) num1 * num2
23) 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
24) What will be the values of the variables num1 and num2 after the given set of assignments?
num1 = 20
num2 = 10
num1 = num1 + num2 / 2
num2 = num1
A) num1 = 25.0, num2 = 25.0
25) 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
26) What is the value of result after the following code snippet?
num1 = 20
num2 = 10
num3 = 2
result = num1 // num2 // num3
print(result)
A) 1
27) What is the value of result after the following code snippet?
num1 = 20
num2 = 10
num3 = 2
result = num1 // num2 / num3
print(result)
A) 1.0
28) Which code snippet is the correct Python equivalent to the following Algebraic expression?c=(a2+b2)
A) sqrt(a ** 2 + b ** 2)
29) What symbol is used to find remainder of a floor division?
A) %
30) A(n) _____________________ is a collection of programming instructions that carry out a particular task.
A) function
31) What is the value of 4 ** 3?
A) 64
32) What is returned by the function: round(x) if x = 5.64?
A) 6
33) What is returned by the function: abs(x) if x = 5.64?
A) 5.64
34) What is returned by the function: max(1, 4, 15, 2, 3, 24)?
A) 24
35) What is returned by the function: round(3.14159, 2)?
A) 3.14
36) What must be done first before you can use a function from the standard library?
A) the function must be imported
37) What is returned by the function: sqrt(64)?
A) 8.0
38) What is wrong with the following code snippet?
((num1 + num2) * num3 / 2 * (1 - num4)
A) there is an extra parenthesis
39) Which of the following statements correctly calculates the average of three numbers: num1, num2, and num3?
A) ( num1 + num2 + num3 ) / 3
40) Which of the following suggestions is the best way to make code easier for other programmers to understand?
A) Give each variable a name that explains its purpose.
41) Assume that you have an integer variable, pennies, that currently contains an integer number of pennies. Which statement determines the number of dollars and cents for that number of pennies?
A) dollars = pennies // 100
cents = pennies % 100
42) What is wrong with the following code snippet?
result = num1 // num2 / num3
num1 = 20
num2 = 10
num3 = 2
print(result)
A) A variable is used before it is assigned a value.
43) A(n) _____________________ is a collection of code that has been written by someone else that is ready for you to use in your program.
A) library
44) Consider the following code segment:
x = 5
y = 7
z = x - y * 2
After this code segment executes, the value of z is:
A) -9
45) The Python code that represents the formula c=(a/b)3 is:
A) c = (a / b) ** 3
46) 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
47) Which function call will cause Python to report an error?
A) abs(1, 2)
48) Which statement computes the square root of 5 and stores it in the variable, r? Assume that the math module has already been imported.
A) r = math.sqrt(5)
49) Which of the following statements computes the minimum of the variables a, b, c and d, and stores it in x?
A) x = min(min(a, b), min(c, d))
50) What is the value of length after this statement: length = len("Good Morning")?
A) 12
51) Which statement correctly creates a new variable by combining the two string variables: firstName and lastName?
A) name = firstName + lastName
52) What is printed from the following code snippet:
message = "ho.."
print(message * 3)
A) ho..ho..ho..
53) What is printed by the following code snippet:
street = " Main Street"
address = 123 + street
print(address)
A) nothing is printed, this code snippet causes an error
54) The following code snippet has an error, how can this be corrected so it prints: 123 Main Street?
1. street = " Main Street"
2. address = 123 + street
3. print(address)
A) change the value ‘123’ in line 2 to a string using the str function
55) What is printed by the following code snippet?
num = int("45") * float("1.5")
print(num)
A) 67.5
56) What is the index value of the letter 'h' in the string below ?
message = "hello"
A) 0
57) Given the code snippet below, what code is needed to print the person’s initials?
firstName = "Pamela"
middleName = "Rose"
lastName = "Smith"
A) print(firstName[0], middleName[0], lastName[0])
58) Which statement finds the last letter of the string variable name?
A) last = name[len(name) – 1]
59) A ___________________________ is a collection of programming instructions that can be appllied to an object.
A) method
60) A sequence of characters is referred to as a:
A) string
61) What is it called when you join two strings together in Python?
A) concatenation
62) What functions can be used to convert a string into a number?
A) int and float
63) What output is generated by the following code snippet?
firstName = "Pamela"
middleName = "Rose"
lastName = "Smith"
print(firstName[0], middleName[0], lastName[5])
A) nothing, this causes an index of bounds error
64) What is printed by the following code snippet?
name = "Robert"
formalName = name.upper()
print(formalName)
A) ROBERT
65) What is printed by the following code snippet?
name = "Robert"
formalName = name.lower()
print(formalName)
A) robert
66) What is printed by the following code snippet?
name = "today is thursday"
name.replace("t", "T")
name.replace("i", "I")
print(name)
A) today is thursday
67) What is printed by the following code snippet?
name = "today is thursday"
newName = name.replace("t", "T")
print(newName)
A) Today is Thursday
68) What is the value of x after the following code segment?
x = len("Hello World!")
A) 12
69) Assume that s is an arbitrary string containing at least 2 characters. What is displayed by the following code segment?
print(s[0], s[len(s) - 1])
A) The first character of s, followed by a space, followed by the last character of s.
70) Which of the following symbols can be used to begin a string literal in Python?
A) “
71) What is the value of words after the following code segment?
words = "Hello" + "World" * 3
A) “HelloWorldWorldWorld”
72) Which of the following statements causes Python to report an error?
A) x = 17 + “18.4”
73) What letter is displayed by the following code segment?
title = "Python for Everyone"
print(title[3])
A) h
74) Consider the following code segment:
product = "Cookies"
product = product.lower()
After this code segment executes, the value of the product variable is:
A) “cookies”
75) Consider the following code segment:
title = "Python for Everyone"
newTitle = title.replace("e", "*")
After this code runs, the value stored in newTitle is:
A) “Python for Ev*ryon*”
76) What is displayed by the following code segment?
print("\"Hello World!\"")
A) “Hello World!”
77) Which statement causes A and B to be printed on different lines?
A) print(“A\nB”)
78) Which statement correctly saves the price in the variable cost?
userInput = input("Please enter the price:")
A) cost = float(userInput)
79) Which statement correctly saves the number of items in the variable quantity?
userInput = input("Please enter the quantity:")
A) quantity = int(userInput)
80) What is printed by the following code snippet?
cost = 25.45378
print("%.2f" % cost)
A) 25.45
81) Which output format string correctly allows for 5 positions before and two digits after the decimal point?
A) “%8.2f”
82) Which output format correctly prints an item description left justified with up to 10 letters?
A) “%-10s”
83) What is the output for the following code snippet:
area = 25
print("The area is %05d" % area)
A) The area is 00025
84) What function is used to read a value from the keyboard?
A) input
85) The message used to tell the user what input is expected is known as a(n):
A) prompt
86) What is the data type of the value returned by the input function?
A) string
87) Consider the following code segment:
a = input("Enter the value of a: ")
b = input("Enter the value of b: ")
print(a + b)
When this code segment is run the user enters 1 at the first prompt and 5 at the second prompt. The output displayed is:
A) 15
88) The line of code which reads a value from the user and stores it in a variable named x as a floating-point value is:
A) x = float(input(“Enter the value of x: “))
89) The line of code that displays the floating point number stored in the variable x using 3 decimal places is:
A) print(“%.3f” % x)
90) What output is generated by the following code segment?
a = 10.0
b = 0.50
print("The total is %.2f and the tax is %.2f." % (a, b))
A) The total is 10.00 and the tax is 0.50
91) Consider the following code segment:
x = 12
print("%d%%" % x)
The output generated by this code segment is:
A) 12%
92) A graphics application shows information inside a ____________________
A) window
93) Which statement draws a square on the canvas?
A) canvas.drawRect(0, 0, 50, 50)
94) Which statement sets the fill color when drawing shapes on the canvas?
A) canvas.setFill(“black”)
95) Which statement writes the word Hello on the canvas?
A) canvas.drawText(10, 10, “Hello”)
96) Which of the given print statements generates the following output?
ABCDE"\
A) print(“ABCDE\”\\”)
97) Which statement imports the entire contents of the sympy module?
A) from sympy import *
98) Which statement creates an expression in SymPy form?
A) f = sympify(“x ** 2”)
99) Which SymPy function is used to display a graph of a mathematical function?
A) plot
100) Consider the following code segment:
from graphics import GraphicsWindow
win = GraphicsWindow(400, 200)
canvas = win.canvas()
The line of code that should be added to the end of the code segment above to draw a diagonal line connecting the upper left corner to the lower right corner is:
A) canvas.drawLine(400, 200, 0, 0)
101) The statement that sets the fill color to red is:
A) canvas.setFill(128, 0, 0)
102) Which of the following statements draws a circle?
A) canvas.drawOval(200, 100, 200, 200)
