Test 2: Online Version - Solution

Ace your homework & exams now with Quizwiz!

Consider the following code: x = 5 x = x * 3 print (x) What is output? A. 5 B. 15 C. 27 D. 75

B. 15 → The variable x begins as 5, and is then updated to be its old value multiplied by 3, which would be 5 * 3, and thus 15

The following code is intended to find the average of three numbers entered from the keyboard. What is wrong with the code? print ("Enter 3 numbers: ") n1 = float(input ("Enter a number: ")) n2 = float(input ("Enter a number: ")) n3 = float(input ("Enter a number: ")) print (n1 + n2 + n3 /3) A. n1 , n2 , and n3 should use the str command B. The code is missing parentheses around n1 + n2 + n3 in the last line C. Nothing, it correctly finds the average of the numbers D. Nothing, it correctly finds the sum of the numbers

B. The code is missing parentheses around n1 + n2 + n3 in the last line. → For the average to be correctly computed, the order of operations should be changed by placing a "(" in front of n1 and a ")" after n3. This makes sure that the addition all happens before the division

Three of the following values could be stored as strings. When would you NOT use a string command? a. To store a list of colors b. To store decimal values c. To store a word. d. To store values NOT used for calculations

B. To store decimal values → Decimal values should be stored with floats not strings

You need to make sure a number the user types in is non-negative. Which of the following functions would you use? A. abs B. fabs C. pos D. int

B. fabs → This converts the number to a float (if possible) and returns the absolute float value

You need to find the square root of a value. Which of the following functions would you use? A. int B. sqrt C. fabs D. pow

B. sqrt → This returns the square root of a value

What is NOT a built-in function in python? A. sqrt() B. string() C. fabs() D. print()

B. string() → this is a simple command, but is not a function.

Which of the following calculates to 10? A. 3 * 6 + 2 /2 B. (3 * 6) + 2 /2 C. (3 * 6 + 2) /2 D. 3 * (6 + 2 /2)

C. (3 * 6 + 2) /2 → Through simplification this would be (18 + 2) 2 → 20 / 2 → 10

What is output? print (12 % 5) A. 0 B. 1 C. 2 D. 5

C. 2 The modulo operation (%) is used for collecting the remainder after division is performed. 12 / 5 is 2, 5 * 2 is 10, and the difference between what we have (10), and the total value (12), is the remainder, so the remainder is 2

Consider the following code: x = 19 y = 5 print (x % y) What is output? A. 2 B. 3 C. 4 D. 3.8

C. 4 The modulo operation (%) is used for collecting the remainder after division is performed. 19 // 5 is 3, and 5 * 3 is 15. The difference between what we have 15, and the total value, 19, is the remainder, so the remainder is 4

Consider the following code: x = int(input("Enter a number: ")) print (x) What happens if the user types in A? A. It prints 65 B. It prints A C. This would cause an error: an integer variable cannot store a string. D. This would cause an error: int and input cannot be used together in the same line.

C. This would cause an error: an integer variable cannot store a string. → Because A is a string and the code attempts to store it as an int variable, this error will be thrown

Which of the following is NOT a data type in Python? A. float B. string C. decimal D. integer

C. decimal → Decimal is not a data type in Python, Float should be used instead

Which of the following would be the best variable name to store the value of your English letter grade? A. g B. LG C. 1grade D. letter_Grade

D. letter_Grade → This is descriptive and also follows all variable name rules and etiquette

Which line of code outputs the decimal portion of a float stored in the variable x? A. print (x / 1000) B. print (x - int(x)) C. print (x) D. print (x % 1000)

D. print (x - int(x)) → This would subtract the integer value of x from the total value of x, leaving behind only the decimal portion

To generate numbers between and including -10 to 10 you would use: A. random.random()*20 + -10 B. randint(-10, 10) C. random.random(-10, 10) D. random.randint(-10, 10)

D. random.randint(-10, 10) → This would generate numbers between and including -10 to 10

Which of the following numbers might this code generate: random.randint(1,9) ? A. 10. B. 1 C. 11 D. 0

B. 1 → t his is the only value between 1 and 9

Consider the following code: x = 9 y = -2 z = 2 print (x + y * z) What is output? A. 5 B. 9 C. 13 D. 14

A. 5 If we substitute in the variables from the print statement, we have ( 9 + -2 * 2 ). Following the order of operations, the first calculation would be -2 *2 = - 4. Then the addition would be 9 + -4 resulting in 5

Consider the following code: print (min("whom", "carton", "landform", "composite", "apron", "deposit", "become")) What is output? A. apron B. become C. carton D. whom

A. apron When used with string values, the min() function returns that string which would be at the front if all strings were sorted alphabetically

Why can it be helpful to perform mathematical calculations using programming? Choose the best answer. A. Because is an important skill B. Because computers can quickly do calculations much larger and complex than humans C. Because the values stored in variables cannot change D. Because computers are used to store digital data, not analog

B. Because computers can quickly do calculations much larger and complex than humans. → It is much more efficient to use computers, and they can compute things even humans cannot

Consider the following code: a = 3 b = 2 print (a ** b) What is output? A. 1 B. 6 C. 8 D. 9

D. 9 Substituting in the variables of the print statement gives us (3 ** 2). The (x ** y) format is the operation for exponentiation. The value on the left of the operation is the base, and the value on the right of the operation is the exponent. Thus, (3 ** 2) is equivalent to (3^2) which, though simplification becomes (3 * 3) and then finally 9.

What is the output for the following? x = 7 print ("The number is: " + x * 2) A. 14 B. The number is: 14 C. The number is: x D. None, there is an error

D. None, there is an error. → The error comes from trying to concatenate (with the plus sign) string data and integer data


Related study sets

УСРР в період НЕПу (1921-1928)

View Set

Criminal Justice Administration Final

View Set

NCLEX_Musculoskeletal Disorders 222Q no exp

View Set

Cost Accounting - Exam 2 Materials

View Set

Exam 4 Endocrine and Gastrointestinal

View Set