To define a variable named number that contains 123.4567 as a decimal number, not a floating-point number, which block of code would you use?
A)
from decimal import Decimal
number = new Decimal(123.4567)
B)
import decimal
number = new Decimal(123.4567)
C)
from decimal import Decimal
number = Decimal(123.4567)
D)
import decimal
number = Decimal(123.4567)
To round a decimal number that's stored in a variable named number to 2 decimal places with normal business rounding, which statement would you use?
A) number = number.quantize(Decimal("1.00"))
B) number = number.quantize(Decimal("d.2"))
C) number = number.quantize(Decimal("1.00"), ROUND_HALF_UP)
D) number = number.quantize(Decimal("d.2"), ROUND_HALF_UP)
Which of the following will produce the same result as this code?
import math as m
area = m.pi * radius**2
A) area = m.pi * pow(radius)
B) area = m.pi * m.pow(radius)
C) area = m.pi * pow(radius, 2))
D) area = m.pi * m.pow(radius, 2)
If number equals 3,237,945.76, which of the following will display it as: 3,237,945.76
A) print("{:.2f}".format(number))
B) print("{:25,.9f}".format(number))
C) print("{:,.2f}".format(number))
D) print("{:,.4f}".format(number))
Code Example 9-1
print("{:12} {:>10} {:>5}".format("Item", "Price", "Qty"))
print("{:12} {:10.2f} {:5d}".format("laptop", 499.99, 1))
print("{:12} {:10.2f} {:5d}".format("charger", 29.95, 3))
print("{:12} {:10.2f} {:5d}".format("leather case", 125.00, 1))
Refer to Code Example 9-1. What does :5d specify in the print statements of the last three lines? The value in this column
A) is an integer that consists of 5 digits.
B) is an integer with 5 spaces allowed for its display.
C) should be a decimal number.
D) should be a floating-point number.
Which of the following modules lets you work with decimal numbers instead of floating-point numbers?
A) currency
B) locale
C) decimal
D) math
Which of the following is not a floating-point number?
A) 234.34
B) -683
C) 0.0
D) 2.343e-8
Code Example 9-2
number = float(input("Enter a number: "))
result1 = number * .15
result2 = result1 / 3
result3 = result1 + result2
print(result3)
Refer to Code Example 9-2. When this code is executed, which of the variables might contain approximate results instead of accurate results?
A) result1
B) result1 and result2
C) result1, result2, and result3
D) result2 and result3
What will be displayed when this code is executed?
name = "Name"
ID = "ID"
print("{:10} {:>5}".format(name, ID))
print("{:10} {:7d}".format("Liz", 234))
print("{:10} {:7d}".format("Mike", 23456))
A)
Name ID
Liz 234
Mike 23456
B)
Name ID
Liz 234
Mike 23456
C)
Name ID
Liz 234
Mike 23456
D)
Name ID
Liz 234
Mike 23456
One of the ways to deal with the inaccuracies that may result from floating-point arithmetic operations is to
A) use the decimal() method of the decimal module to round the results
B) do the math with Decimal objects instead of floating-point numbers
C) use the format() method of the decimal module to round the results
D) convert the floating-point results to Decimal objects
What will be displayed after the following code executes?
guess = 19
if guess < 19:
print("Too low")
elif guess > 19:
print("Too high")
A) Too low
B) Too high
C) nothing will display
The and operator has
A) higher precedence than the or operator, but lower precedence than the not operator
B) higher precedence than the or and not operators
C) lower precedence than the or operator, but higher precedence than the not operator
D) lower precedence than the or and not operators
Which of the following can use the range() function to loop through a block of statements?
A) a for statement
B) an if statement
C) a while statement
D) a break statement
For the following code, what will the result be if the user enters 5 at the prompt?
sum = 0
end_value = int(input("Enter a number: "))
for i in range(1, end_value):
sum = sum + i
print(sum, end=", ")
A) 10,
B) 1, 3, 6, 10,
C) 1, 3, 6, 10, 16,
D) 1, 2, 3, 4, 5,
If you want to code an if clause, but you don't want to perform any action, you can code
A) an end statement
B) a pass statement
C) a break statement
D) a skip statement
Which of the following begins by testing a condition defined by a Boolean expression and then executes a block of statements if the condition is true?
A) a for statement
B) a switch statement
C) a while statement
D) a continue statement
How many times will "Hi again!" be displayed after the following code executes?
for i in range(0, 12, 2):
print("Hi, again!")
A) 2
B) 12
C) 5
D) 6
For the following code, what will the result be if the user enters 4 at the prompt?
product = 1
end_value = int(input("Enter a number: "))
for i in range(1, end_value):
product = product * i
i += 1
print("The product is ", product)
A) The product is 1
B) The product is 4
C) The product is 6
D) The product is 24
Given the following code, select the compound condition that makes sure that the input is an integer greater than 0 and less than 1,000.
my_num = input("Enter a number between 1 and 999:")
check_num = int(my_num)
while _________________________:
my_num = input("Try again. The number must be between 1 and 999")
check_num = int(my_num)
A) check_num <= 0 and check_num != 1000
B) check_num < 0 and > 1000
C) check_num <= 0 or check_num > 1000
D) check_num <= 0 or check_num >= 1000
Python will sort the strings that follow in this sequence:
Peach
peach
1peach
10Peaches
A) Peach, peach, 1peach, 10Peaches
B) 1peach, 10Peaches, Peach, peach
C) 10Peaches, 1peach, Peach, peach
D) 10peaches, 1peach, peach, Peach
Which of the following doesn't follow the best naming practices for variables?
A) first_name
B) firstName
C) pay_rate
D) pRate
Which type of errors must be fixed before the program can be compiled? Question 22 options:
A) syntax errors
B) logical errors
C) violations
D) exceptions
What will display after the following print() function is executed? print("Welcome!\\nNow that you have learned about", "input\\nand output, you may be wondering,\\"What\\nis", "next?\\"")
A)
Welcome!
Now that you have learned about
input
and output, you may be wondering,
"What
is next?"
B)
Welcome! Now that you have learned about
input and output, you may be wondering, "What is
next?"
C)
Welcome!\nNow that you have learned about",
"input\nand output, you may be wondering,\"What\nis",
"next?\"
D)
Welcome!
Now that you have learned about input
and output, you may be wondering, "What
is next?"
The goal of __________ is to find all the errors in a program.
A) editing
B) testing
C) debugging
D) interpreting
A runtime error is also known as:
A) a syntax error
B) a logical error
C) a violation
D) an exception
Given: x = 23 , y = 15 What is the value of new_num after the following statement executes?
new_num = x // y
A) 1
B) 1.533333
C) 0.533333
D) 8
What, if anything, is wrong with this code?
rating = input("Enter the rating for this product: ")
rating = rating + 2
print("The adjusted rating is " + rating + ".")
A) nothing is wrong with this code
B) a string variable is used in an arithmetic expression
C) the coding in the print() function contains illegal plus signs
D) the input() function should be an int() function
What is the argument of the print() function in the following Python statement?
print("My student ID is " + str(123456))
A) 123456
B) str(123456)
C) "My student ID is "
D) "My student ID is " + str(123456)
The goal of __________ is to fix all the errors in a program.
A) editing
B) testing
C) debugging
D) interpreting
The __________ software for a computer provides the software that's needed for running applications.
A) application
B) systems
C) operation
D) GUI
To retrieve the fourth character in a string that's stored in a variable named city, you can use this code:
A) city(3)
B) city[3]
C) city(4)
D) city[4]
What is the value of s3 after the code that follows is executed?
s1 = "abc def ghi";
s2 = s1[1:5]
s3 = s2.replace('b', 'z')
print(s3)
A) bc d
B) zc d
C) abc d
D) azc d
E) zc de
What is the value of the variable named result after this code is executed?
email = "marytechknowsolve.com"
result = email.find("@")
A) true
B) false
C) 0
D) -1
The isdigit() method of a string returns
A) the digits that are in the string
B) the string if it only contains digits
C) true if the string contains only digits
D) true if the string contains only digits and a decimal point
Which of the following statements will modify the string that's stored in a variable named s? Question 35 options:
A) s.strip()
B) s.replace('a', 'A')
C) s[1] = "A"
D) You can't modify a string.
Given the following code, what will be displayed after the code executes?
name = "Mervin the Magician"
words = name.split()
print(words[0] + ", you are quite a " + words[2].lower())
A)
Mervin
, you are quite a
magician
B)
Mervin, you are quite a Magician
C)
Mervin, you are quite a magician
D)
Mervin
, you are quite
the magician
What is the value of s2 after the code that follows is executed?
s1 = "118-45-9271"
s2 = ""
for i in s1:
if i != '-':
s2 += i
s1.replace("-", ".")
A) 118-45-9271
B) 118 45 9271
C) 118459271
D) 118.45.9271
Which of the following will display this result?
B = 66
A) print("B = ", char("B"))
B) print("B = ", ord("B"))
C) print("B = ", ascii("B"))
D) print(ord(66))
What will be displayed after the following code executes?
book_name = "a tale for the knight"
book = book_name.title()
print(book)
A) A Tale For The Knight
B) A Tale for the Knight
C) A Tale for the knight
D) A tale for the knight
If word = "a horse", which of the following snippets of Python code will display this result? a horse! a horse! My kingdom for a horse!
A) print((word * 2) + "! My kingdom for " + word + "!")
B) print((word + "! " + " My kingdom for " + word + "!") * 2)
C) print(word * 2 + " My kingdom for " + word + "!")
D) print((word + "! ") * 2 + " My kingdom for " + word + "!")
Which statement would you use to call the print_name() function from a module named address that has been imported with this statement?
import address as a
A) address.print_name(name)
B) a.print_name(name)
C) print_name(name)
D) global.print_name(name)
Code Example 4-2
def get_volume(width, height, length=2):
volume = width * height * length
return volume
def main():
l = 3
w = 4
h = 5
v = get_volume(l, w, h)
print(v)
if __name__ == "__main__":
main()
Refer to Code Example 4-2: If you add the following code to the end of the main() method, what does it print to the console?
print(get_volume(10, 2))
A) 20
B) 40
C) 60
D) Nothing, it causes an error
Code Example 4-4
main program:
import arithmetic as a
def multiply(num1, num2):
product = num1 * num2
result = a.add(product, product)
return result
def main():
num1 = 4
num2 = 3
answer = multiply(num1, num2)
print("The answer is", answer)
if __name__ == "__main__":
main()
arithmetic module:
def add(x, y):
z = x + y
return z
Refer to Code Example 4-4: What values are in x and y after the code runs?
A) 4, 3
B) 5, 6
C) 12, 12
D) 24, 24
Code Example 4-2
def get_volume(width, height, length=2):
volume = width * height * length
return volume
def main():
l = 3
w = 4
h = 5
v = get_volume(l, w, h)
print(v)
if __name__ == "__main__":
main()
Refer to Code Example 4-2: When this program runs, what does it print to the console?
A) 24
B) 40
C) 60
D) v
A return statement
A) must be coded within every function
B) can be used to return a local variable to the calling function
C) can be used to allow the function to modify the value of a global variable
D) can only be used once in each function
Which of the following is not true of hierarchy charts?
A) Function names should start with a verb and indicate what the functions do.
B) Each function should do only what is related to the function name.
C) The top level box should be for the main() function.
D) Related functions should be combined into a single function.
To call a function, you code the function name and
A) a set of parentheses
B) a set of parentheses that contains zero or more arguments
C) a set of parentheses that contains one or more arguments
D) a set of parentheses that contains a list of the local variables
Code Example 4-3
main program:
import arithmetic as a
def main():
num1 = 5
num2 = 6
result = a.add(num1, num2)
print("The sum is", result)
if __name__ == "__main__":
main()
arithmetic module:
def add(x = 4, y = 2):
z = x + y
return z
Refer to Code Example 4-3: What values are in x and y after the code runs?
A) 9, 8
B) 5, 6
C) 4, 2
D) 20, 12
Code Example 4-2
def get_volume(width, height, length=2):
volume = width * height * length
return volume
def main():
l = 3
w = 4
h = 5
v = get_volume(l, w, h)
print(v)
if __name__ == "__main__":
main()
Refer to Code Example 4-2: What value is passed to the height argument by the call to the get_volume() function?
A) 2
B) 3
C) 4
D) 5
A global variable
A) is defined inside the main() function
B) cannot be modified inside a function
C) cannot be accessed from within a function
D) is defined outside of all functions