Computer science

Ace your homework & exams now with Quizwiz!

Which of the following calculates to 10?

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

. What is output? x = 10 if (x > 10): print(1) elif (x < 10): print(2) elif (x >= 10): print(3) else: print(4)

3 → 10 is greater than or equal to 10, so 3 will be the output

Consider the following code: x = 19 y = 5 print (x % y) What is output?

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.

. What is output? x = 8 % 2 if (x > 10): print(1) elif (x > 5): print(2) elif (x > 1 ): print(3) else: print(4)

4 → (8 % 2) = 0 and 0 is not larger than 10, 5, or 1 so the else case will run

. Consider the following code: x = 9 y = -2 z = 2 print (x + y * z) What is output?

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.

Why can it be helpful to perform mathematical calculations using programming? Choose the best answer

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

What is the correct order for the steps the compiler uses in translating code?

Check for mistakes, translate commands to machine language, run the commands

Which of the following about computers is NOT true?

Computing devices translate digital to analog information in order to process the information

For if statements to work in Python you must ________________.

indent their code → Python requires indents when using statements such as IF, FOR, and WHILE to know which lines of code are included in the statement

what command is used to change a string into a number

int

Which of the following would be the best variable name to store the value of your English letter grade?

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

What is true about the main and secondary memory

Main memory is short term memory used by the CPU in processing commands, secondary memory is more permanent and used for storage .

What is the output for the following? x = 7 print ("The number is: " + x * 2)

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

What is not an input device

Printer

what is input?

Putting information into the computer.....input is information from a source outside the computer being fed into it

What is one downside of converting analog information to a digital format?

Some information may be lost when it is converted

What are two basic categories of variable data types

Strings and numbers--->there base datas types, words/strings and numbers, encompass the majority of data

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)

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

Which of the following is used to show not equals?

!= → This is used to check if two values are not equal

The symbol used to create a comment is:

# → This is the correct comment symbol

Consider the following code: x = int(input("Enter a number: ")) print (x) What happens if the user types in A?

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

When should you use an ELIF statement?

To make a follow-up True/ False decision → IF statements use conditions to decide whether or not to run lines of code, where if the condition is True, then the lines of code will run d. To get user input → An ELIF statement does not prompt a user for input.

Three of the following values could be stored as strings. When would you NOT use a string command?

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

When do you use an else statement?

To tell what will happen when an if-statement is false → The else case will run when all of the other conditions checked are false

Which of the following is true? a = 1 b = 10

a < b and a != b → This is true because 1 is smaller than 10 and 1 does not equal 10. Both conditions need to be true for the whole statement to be true

Consider the following code: x = input("What year is it?") print(x) The value stored in x is:

a String → All data that comes in by way of the input() keyword is automatically a string

Select all the mistakes in the following: There may be more than one. if (count = 10): print ("Hello") elseif (count > 100): print ("Good-bye") else print("WAIT!")

a. The = should be == → = is used to assign values but == is used to compare values which is what is needed in an IF statement b. elseif should be elif → Python uses elif statements to check for more conditions d. The else should have a : → A colon is needed for each if, elif, and else statement in Python

The following code could be rewritten using: if (x > 12): if (x < 34):

and → This is correct because both conditions need to be true

2. Consider the following code: x = "apple" y = x z = "banana" print(x + " " + y + "\n" + z)

apple apple banana →The answer is: x "apple", followed by a space " ", followed by y (which is equal to x and therefore also "apple"), followed by a newline "\n" followed by z "banana"

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

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

Mark all the mistakes in the following code: IF (name1 = "sue") :

b. IF should be if → Keywords used by Python such as if, while, and for all need to be lower case d. = should be == → = assigns a value to a variable and == is used to compare two values, so == is needed for the condition

Which of the following are true about algorithms? (You may select more than one)

b. produce a result → All algorithms will produce some result c. can be done by a computer → All algorithms can be written and ran on a computer d. can perform logical operations → All algorithms can use logical operations

Write the code to test if the number 78 is stored in the variable text1:

c. if (text1 == 78): → This correctly tests if text1 equals the number 78

Consider the following code: print("computer" + "science") What is output?

computer science → There would be no space between the words because there was no space signified in the print statement

Which of the following is NOT a data type in Python?

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

What is NOT a built-in function in python?

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

What is a legal variable name

temperatureAM-->Doesn't break any rules

. Write the code to test if x is greater than or equal to 2.

if (x >= 2): → This correctly checks if x greater than or equal to the number 2

You need to find the square root of a value. Which of the following functions would you use?

sqrt → This returns the square root of a value

Which of the following numbers might this code generate: random.randint(1,9)?

1 → this is the only value between 1 and 9.

Consider the following code: x = 5 x = x * 3 print (x) What is output?

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 c. 27 → This would be the result of inc

What is output? print (12 % 5)

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: a = 3 b = 2 print (a ** b) What is output?

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.

whats the output print(type("95"))

<class 'str'>--->"str" is correct data type

To test if x is greater than or equal to y: if (x ___ y):

>= → This would be used to check if x is greater than or equal to y

What best defines an algorithm?

A set of precise steps used to perform a task, typically on a computer → This is the most comprehensive definition of an algorithm.

Which of the following is NOT true about all algorithms?

All algorithms can only do number calculations → Algorithms are not restricted to only number calculations, they can do other processes as well

what's the output print("Twinkle, \ttwinkle, \tlittle star")

Twinkle twinkle, little star--->correctly taking into account that "\t" creates a tab

What's the output x = input("Enter a number: ") y = input("Enter a number: ") print("Values: " + x + y)

Values: 54 → This correctly accounts for the string data type of x and y (how they would be pushed together, not added) and also correctly includes the "Values: " title

When do we use numbers instead of strings?

When you need to do calculations → We use number data types when the properties of the numbers actually need to be used (like in arithmetic and calculations) as opposed to just reading the data, for which strings are good

You need to make sure a number the user types in is non-negative. Which of the following functions would you use?

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

Which of the following is a correction for: if (90 < = x <= 100):

if (90 < = x and x <= 100): → This correctly tests if 90 is less than or equal to x and tests if x is less than or equal to 100

Write the code to test if the variables num1 and num2 are not the same

if (num1 != num2 ): → This correctly tests if num1 is not equal to num2

Write the code to test if the word "BASIC" is stored in the variable text1.

if (text1 == "BASIC"): → This correctly tests if text1 is equal to the string "BASIC"

Write the code to test if the number in the variable text1 is greater than 15

if (text1 > 15): → This correctly tests if text1 is greater than the number 15

Which of the following correctly inputs two numbers and print the sum? n1 = input("Enter a number: ") n2 = input("Enter a number: ") print("The answer is: " + int(n1+n2)) (n1+n2) will not be added as intended, because they are still of the string data type (if n1 and n2 were 7 and 3 respectively, (n1+n2) would be "73" not 10). Also, you cannot concatenate int data and string data without getting an error, so int(n1+n2) should be str(n1+n2).

n1 = int(input("Enter a number: ")) n2 = int(input("Enter a number: ")) print("The answer is: " + str(n1+n2)) This correctly accounts for needing to cast the string input data as int before addition is done, and then needing to change the data to string after addition in order to concatenate it with the other string.

What controls the computer memory

operating system

Which line of code outputs the decimal portion of a float stored in the variable x?

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

Which of the following will correctly output: I know there's a proverb which that says "To err is human," but a human error is nothing to what a computer can do if it tries. ― Agatha Christie Note: This is a direct quote from Agatha Christie's novel, Hallowe'en Party. The grammar shown here is as it is written in the novel.

quote = "\"To err is human,\"" print("I know there's a proverb which that says \n" + quote + "\nbut") print("a human error is nothing to what a computer") print("can do if it tries.\n\n- Agatha Christie")

To generate numbers between and including -10 to 10 you would use:

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


Related study sets

Pharm prep you chapter 51, 43, 47

View Set

Chapter 53: Female Reproductive and Genital Problems Lewis: Medical-Surgical Nursing Test Bank

View Set

ch. 8, 16 & 18 section quiz sociology

View Set