programming midterm review

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Which of the following would be the best variable name to store the value of your English number grade? -NG -n -1grade -number_Grade

-number_Grade Explanation: This is descriptive and also follows all variable name rules and etiquette

Which of the following controls computer memory? -cpu -input -compiler -operating system

-operating system Explanation: OS is the software that supports a computer's basic functionality, like keeping track of other software, and organizing and allocatingthe computer's memory.

The following code could be rewritten into a single if statement that checks for either condition to be true using: if (x > 12): if (x < 34): -None of the above -and -or -not

-or Explanation: This is correct because only one of the conditions need to be true

Information sent to a function is a? -sum -loop control variable -count variable -parameter

-parameter Explanation: A parameter is information sent to a function through the function header, like print(x) , range (x,y,z) and so on. These elements can be accessed within the function, allowing the code to perform its functions on different values.

All of the following statements need their code to be indented except? -else statements -elif statements -print statements -if statements

-print statements Explanation: print statements do not contain multiple lines of code and do not need to be indented

Which line of code outputs the decimal portion of a float stored in the variable y? -print(y) -print(y - int(y)) -print(y % 100) -print(y / 100)

-print(y - int(y)) Explanation: This subtracts the integer value of y from the total value, leaving only the decimal portion

Consider the following code: print("python" + "program") What is output? -Nothing, there is an error - you cannot add words. -python program -pythonprogram -pyhton program

-pythonprogram Explanation: There would be no space between the words because there was no space signified in the print statement

Consider the following code: start = int(input("Enter the starting number: ")) stop = int(input("Enter the ending number: ")) x = 4 for i in range (start, stop, x): print (i, end=" ") What is output if the user enters 20 then 32? 20 24 28 32 20 24 28 24 28 32 24 28

20 24 28 Explanation: This has the correct starting value, 20, steps by 4 until before 32 which is 28.

Consider the following code: start = int(input("Enter the starting number: ")) stop = int(input("Enter the ending number: ")) x = 3 sum = 0 for i in range (start, stop, x): sum = sum + i print (sum) What is output if the user enters 10 then 15? 10 15 23 39

23 Explanation: Substituting in the variables, our range function is range(10, 15, 3). This would be theseries 10, 13 at which point it stops since 16 > 15. Since the output is a sum of all the numbers, the output is 23.

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

2 Explanation: Will be the output if x is less than 10 and 8 is less than 10

Which of the following calculates to 10? 2 * 6 - 2 / 2 2 * (6 - 2 / 2) (2 * 6 - 2) / 2 (2 * 6) - 2 / 2

2 * (6 - 2 / 2) Explanation: Through simplification this would be 2 * (6 - 1) → 2 * 5 → 10

Consider the following code: x = 7 x = x * 4 print(x) What is the output? 14 7 8 28

28 Explanation: The variable x begins as 7, and is then updated to be its old value multiplied by 4, which would be 7 * 4, and thus 28

What is output? print(3 % 15) 1 3 5 0

3 Explanation: If the dividend is smaller than the divior, the dividend is printed.

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

3 Explanation: Will be the output if x is less than 2 and 1 is less than 2

Which of the following calculations would evaluate to 12? (3 * 6) + 2 / 2 3 * ((6 + 2) / 2) (3 * 6 + 2) / 2 3 * 6 + 2 / 2

3 * ((6 + 2) / 2) Explanation: Always follows PEMDAS

What is the output by the following code: for x in range (1, 10, 2): print (x + 2, end=" ") What is the output? 3 5 7 9 11 3 5 7 9 11 13 15 17 19 3 4 5 6 7 8 9 10 11 11 9 7 5 3

3 5 7 9 11 Explanation: The print statement adds 2 to every number in range and the range counts the odd numbers from 1 to 10. Thus, it will print the numbers 1 3 5 7 9 but incremented by 2.

Consider the following: calc = 3 * 5 + 4 ** 2 - 1 What is the base for the exponent? 4 3 8 5

4

Which of the following numbers might this code generate: random.randint(3, 11)? 5 1 12 5.5

5 Explanation: this is the only integer value between 3 and 11.

What is output? x = -2 y = -3 print(x * y + 2) 2 8 10 4

8

What is output by the following? Assume the user enters Sally and John. x = input("What is your name? ") y = input("What is your friend's name? ") print("Hi " + x + " and " + y) -Hi + x + and + y -Hi + Sally + and + John -Hi x and y -Hi Sally and John

Hi Sally and John

___________ is what we use to communicate day to day. Algorithms Natural Language Formal Language Code

Natural Language

Instructions written in code that a computer follows are called: -Input -Compiler -Operating System -Program

Program

The programs that run on a computer are called: -Hardware -Comments -Software -Browser

Software

What is the CPU? -The central programming unit, it translates high level language and commands that we type in into machine code. -The computer programming unit, it stores the computer's operating system. -The central processing unit, it acts as the brain of the computer and carries out commands. -Computer peripheral unit, it handles all input and output devices.

The central processing unit, it acts as the brain of the computer and carries out commands.

What is the output for the following? x = 7 print("The number is: " + str(x * 2)) 14 The number is: x * 2 none, there is an error The number is: 14

The number is: 14

A name given to a spot in memory is called: -Input -Variable -Print -Output

Variable

Which of the following correctly stores 45 squared in the variable x? x = 45 x = x ^ 2 x = 45 x = x % 2 x = 45 x = x ** 2 x = 45 x = x * 2

x = 45 x = x ** 2

The symbol used to create a comment is: % / - #

# Explanation: This is the correct comment symbol

Which of the following is NOT a legal variable name? -grade3 -%information -appointment_hour -firstName

%information

What is the output by the following code? for x in range (3): for y in range (4): print("*", end=" ") print("") **** **** **** *** *** *** *** **** **** **** **** *** *** ***

**** **** ****

All of the following could be true about an algorithm except? (Choose one answer) -A never ending series of code -Using IF and ELSE statements in order to gather input and print outputs -A set of precise steps used to perform a task, typically on a computer -Steps used to complete a goal

-A never ending series of code Explanation: Algorithms have to end after a finite number of steps.

Which of the following are TRUE about all algorithms? There may be more than one. -All algorithms have an order. -All algorithms have clear instructions -All algorithms stop in a finite amount of time. -All algorithms can only do number calculations

-All algorithms have an order. Explanation: All algorithms will have an order that theinstructions are arranged in -All algorithms have clear instructions Explanation: All algorithms will have clear instructions -All algorithms stop in a finite amount of time. Explanation: No algorithm will run forever

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

-Because computers can quickly do calculations much larger and complex than humans. Explanation: It is much more efficient to use computers, and they can compute things even humanscannot

What is the correct order for the steps the compiler uses in translating code? -Translate commands to machine language, Run the commands, Check for mistakes -Translate commands to machine language, Check for mistakes, Run the commands -Check for mistakes, Translate commands to machine language, Run the commands -Check for mistakes, Run the commands, Translate commands to machine language

-Check for mistakes, Translate commands to machine language, Run the commands Explanation: Checking for mistakes is done first because it makes sure the computer does not attempt any commands that might be damaging or impossible. Now that it knows the code is alright, it can safely translate it into machine language. It cannot run the commands before this step, because the computer can only actually work in machine language. The final step is to then execute the commands.

What command is used to change a string into a number? -str() -int() -num() -char()

-int()

Consider the following code: val = 0 while (val > 10): val = val + 1 print (val) What is the error? -The loop will not stop because val never changes. -It should be val < 10 -The loop will not stop since val is counting the wrong direction. -There is no error.

-It should be val < 10 Explanation: The condition checks if val > 10. val starts at 0 and so this loop would not run since the condition is false. It should be a < sign so that the loop runs until val is >= 10.

Which of the following is false about main and secondary memory? -Main memory is more permanent and used for storage, secondary memory is short term memory is used by the CPU in processing commands. -Secondary memory is not lost when the device's power is turned off. -Main memory is faster than secondary memory. -Main memory is short term memory used by the CPU in processing commands, secondary memory is more permanent and used for storage.

-Main memory is more permanent and used for storage, secondary memory is short term memory is used by the CPU in processing commands Explanation: Main memory is faster than secondary memory, but there is a smaller amount of it and it is also volatile, meaning that it is lost when the device is powered off. Because it is fast, main memory is what the CPU uses for processing commands and storing information that is currently needed when the device is in use. Secondary memory is slower, there is more of it, and the information within is less frequently used than main memory. It is also more stable, and not deleted when a computer is shut off. It is used for backup information and for permanent information storage.

What is output by the following? print("Mary, \thad a, \tlittle lamb") -Mary, had a, little lamb -Mary, \thad a, \tlittle lamb -Mary, had a, little lamb (there's extra spaces after the commas) -Mary, had a, little lamb

-Mary, had a, little lamb (there's extra spaces after the commas) Explanation: Correctly taking into account that "\t" creates a tab

What is the output for the following? x = 5 print("The number is: " + x * 3) -The number is: 15 -15 -The number is: x -None, there is an error.

-None, there is an error. Explanation: The error comes from trying to concatenate (with the plus sign) string data and integer data. Need to cast to str first: str(x * 3)

What does the following loop do? val = 0 total = 0 while (val < 10): val = val + 1 total = total + val print(val) -Print the numbers forward from 1 to 10. -Print the sum of the numbers from 1 to 10. -Finds the average of the numbers between 1 and 10. -Print the numbers from 1 to 10 along with the sum up to that number.

-Print the numbers forward from 1 to 10. Explanation: val and total both start at 0. In each iteration of the loop, val is incremented by 1 and then printed. total will contain the sum of the numbers from 1 to 10 but is never printed.

What is input? -Compiling code. -Audio played from a speaker. -Putting information into the computer. -Storing data in secondary memory.

-Putting information into the computer. Explanation: 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? -Limits the technology available -Some information may be lost during the conversion process -It takes up too much space in secondary memory -When the power is turned off the information in main memory is lost

-Some information may be lost during the conversion process Explanation: This always happens, it is the nature of conversions

The two basic categories of variable data types are: -Strings and numbers -Decimals and integers -Output and input -Numbers and words

-Strings and numbers Explanation: These base data 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: ") a = float(input("Enter a number: ")) b = float(input("Enter a number: ")) c = float(input("Enter a number: ")) print(a + b + c / 3) - a, b, and c should use the str command - Nothing, it correctly finds the sum of the numbers. -The code is missing parentheses around a + b + c in the last line. -Nothing, it correctly finds the average of the numbers.

-The code is missing parentheses around a + b + c in the last line. Explanation: For the averageto be correctly computed, the order of operations should be changed by placing a "("in front of a and a ")" after c. This makes sure that the addition all happens before the division

Consider the following code: for x in range (10 28): print(x) What is wrong with this program? -The range function should have three parameters. -The first line should be for x in range(10, 28) = x: -The first line should be for x in range(10, 28): -The loop is missing x = 0

-The first line should be for x in range(10, 28): Explanation: The , is a valuable piece of syntax to separate the first parameter from the second, and the loop will not run without it.

Consider the following code that is meant to print out 7,8,9: for x in (7, 10): print (x) What is wrong with the program? -The first line should be for x = range(7, 10): -The first line should be for x in range(7, 10): -The first line should be for x in 7 to 10: -This program does not have errors.

-The first line should be for x in range(7, 10): Explanation: range() is needed for Python to perform something using the parameters (7,10)

Which of the following about computers is true? -The operating system is an analog program. -The CPU is an input device. -The memory uses binary numbers. -Computing devices translate digital to analog information in order to process the information.

-The memory uses binary numbers.

Which of the following is true for algorithms? -They may not produce a result. -They have an order. -They repeat indefinitely. -They have ambiguous instructions.

-They have an order. Explanation: If they have no order, then there would be no consistency to the lines of code, and they may not make sense.

Consider the following code: x = input("Enter a number: ") print(int(x)) What happens if the user types in A? -This would cause an error: float() should be used -It prints A -This would cause an error: a string cannot be typecast to an integer -It prints 24

-This would cause an error: a string cannot be typecast to an integer Explanation: Because A is a string and the code attempts to typecast it as an int variable, this error will be thrown

Which of the following is not true about algorithms? (Select one answer) -must output a string -can be done by a computer -can produce a result -can perform logical operations

-must output a string Explanation: Algorithms are not required to output a string

When do you use an elif statement? -To check multiple expressions for TRUE and execute a block of code as soon as one of the conditions evaluates to TRUE. -To handle string values -To end an IF statement -To Input a variable

-To check multiple expressions for TRUE and execute a block of code as soon as one of the conditions evaluates to TRUE.

When should you use an else statement? -To check one of many expressions for TRUE -To check the first expression for TRUE -To execute code when one of the other expressions were TRUE -To execute code if none of the other expressions were TRUE

-To execute code if none of the other expressions were TRUE

When do you use a while loop INSTEAD of a for loop? (Choose the best two answers.) -To get input from the user until they input 'stop'. -To do number calculations. -To repeat code. -When there is an unknown number of iterations needed.

-To get input from the user until they input 'stop'. Explanation: A while loop allows the user to keep inputting until they want to stop. -When there is an unknown number of iterations needed. Explanation: While loops can affect their control variable within the loop which makes it useful for loops where the number of times the loop needed to be run is unknown.

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

-To store a decimal number such as 3.14 Explanation: Decimal values should be stored with floats not strings

When do we use strings instead of numbers? -To store words like someone's last name -To hold integers -When you need to do calculations -To store large amounts of data

-To store words like someone's last name

What is output by the following? Assume the user enters 3 and 9. a = input("Enter a number: ") b = input("Enter a number: ") print("Values: " + a + b) -12 -Values: 39 -Values: 12 -39

-Values: 39 Explanation: This correctly accounts for the string data type of a and b (how they would be pushedtogether, not added) and also correctly includes the "Values: " title

Which two of the following are true? a = 1 b = 1 -a < b and a == b -b <= a or a >= b -a > b or a == b -a < b and a != b

-a > b or a == b Explanation: This is true because 1 is equal to 1. One of the conditions need to be true for the whole statement to be true -b <= a or a >= b

Consider the following code: x = input("What year is it?") print(x) The value stored in x is: -a program -an int -stored in secondary memory -a string

-a string Explanation: All data that comes in by way of the input() keyword is automatically a string

Consider the following code: print(min("who", "milk", "apple", "prime", "banana", "car", "angel")) What is the output? -apple -angel -banana -who

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

For a loop to stop, the loop control variable must ______________. -decrease in value -be a certain specified input -be a boolean -be a certain value

-be a certain value Explanation: The loop control variable must change inside the loop and meet the previously set condition for the loop to stop.

Consider the following code: x = "cat" y = x z = "dog" print(x + " " + y + "\n" + z) What is output? -cat dog -cat cat dog -cat cat dog -cat cat dog

-cat cat dog Explanation: The answer is: x, "cat", followed by a space " ", followed by y (which is equal to x and therefore also "cat"), followed by a newline "\n" followed by z, "dog"

In a(n) _______________ loop, you know ahead of time how many times the loop will repeat. -count variable -initialization -condition -user input

-count variable Explanation: In a count variable loop, the condition will be based on the value of that variable and since it merely counts up or down it is simple to calculate how many times the loop will repeat.

Which of the following is NOT a data type in Python? -integer -float -string -decimal

-decimal Explanation: Decimal is not a data type in Python, float should be used instead

You need to make sure a number the user types in is non-negative. The number can be an int or a float. Which of the following functions would you use? -int() -fabs() -abs() -sqrt()

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

Which of the following is a legal variable name? -21 -7counter -my var -first_name

-first_name Explanation: Doesn't break any rules

Which of the following is correction for: if (96 <= x <= 101): -if (96 < = x or <= 101): -if (96 <= x and x <= 101): -if (96 <= x or x <= 101): -if (96 <= x and <= 101):

-if (96 <= x and x <= 101): Explanation: This correctly tests if 96 is less than or equal to x and tests if x is less than or equal to 101

Write the code to test if the variables num1 and num2 are the same. -if (num1 != num2): -if (num1 < num2): -if (equal(num1 == num2)): -if (num1 == num2):

-if (num1 == num2): Explanation: This correctly tests if num1 equals num2

Write the code to test if the number in the variable text1 is greater or equal to than 15. -if (text1 < "15"): -if (text1 > 15): -if (text1 >= 15): -if (text1 > "15"):

-if (text1 >= 15): Explanation: This tests if text1 is greater than or equal to the number 15

Write the code to test if the number 33 is stored in the variable var1: -if (str(var1) == 33): -if ( var1 == "33"): -if ("var1" == 33): -if (var1 == 33):

-if (var1 == 33): Explanation: This correctly tests if var1 equals the number 33

Write the code to test if the word "HOUSE" is stored in the variable word1. -if (word1 == "HOUSE"): -if (word1 == str(HOUSE)): -if (int(word1) == HOUSE): -if (word1 == HOUSE):

-if (word1 == "HOUSE"): Explanation: This correctly tests if word1 is equal to the string "HOUSE"

Write the code to test if x is less than or equal to 2. -if (x >= 2): -if (x < 2): -if (x <= 2): -if (x > 2):

-if (x <= 2): Explanation: This correctly checks if x less than or equal to the number 2

For if statements to work in Python you must ____________. -add an else to them -make decisions about them -indent their code block -use them with input commands

-indent their code block

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

-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 -5 to 5 you would use: -random.random()*10 + -5 -random.random(-5, 5) -random.randint(-5, 5) -randint(-5, 5)

-random.randint(-5, 5) Explanation: This would generate numbers between and including -5 to 5

Which range function will print all odd numbers between 11 and 33? -range (11, 34) -range (11, 34, 2) -range (11, 33, 2) -range (11, 33)

-range (11, 34, 2) Explanation: This starts at the correct number, 11, increments by 2 to only include the odd numbers, and ends at a point where it still includes 33.

What is output by the following? print("she sells \nseashells") print("by the seashore") -she sells seashells by the seashore -she sells seashells by the seashore -she sells seashells by the seashore -she sells seashells by the seashore

-she sells seashells by the seashore

Which of the following is NOT an input device? -camera -speaker -mouse microphone

-speaker Explanation: A speaker outputs audio information from the computer as sound

You need to find the square root of a value. Which of the following functions would you use? -sqrt() -fabs() -pow() -str()

-sqrt() Explanation: This returns the square root of a value

Examine the following code segment: data = 3.16 new_data = CAST(data) print("Length of Data:", len()) What data type (if any) should replace the word CAST on line 2 to ensure the code segment runs without error? -float -int -str -Nothing should be changed

-str Explanation (Why it's not float): "float" is a valid data type but if new_data is of the type float an error will be thrown when len() is called on it in line 3

What is NOT a built-in function in python? -string() -print() -fabs() -sqrt()

-string() Explanation: string() is not a function in Python.

Consider the following code: for i in range (x, y): print (i, end=" ") What values for x and y will output the code below? 10 11 12 13 14 15 16 17 18 19 -x = 19 y = 9 -x = 10 y = 19 -x = 10 y = 20 -x = 19 y = 10

-x = 10 y = 20 Explanation: Correct, 10 is the first number, and this prints up to 19.

Which of the following correctly inputs two numbers and prints the sum? -x = int(input("Enter a number: ")) y = int(input("Enter a number: ")) print("The answer is: " + num(x + y)) -x = input("Enter a number: ") y = input("Enter a number: ") -x = input("Enter a number: ") y = input("Enter a number: ") print("The answer is: " + int(x + y)) -x = int(input("Enter a number: ")) y = int(input("Enter a number: ")) print("The answer is: " + str(x + y))

-x = int(input("Enter a number: ")) y = int(input("Enter a number: ")) print("The answer is: " + str(x + y)) Explanation: 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 is output by the following? print("/\\") print("\\/") /\\ \\/ /\\\\/ /\ \/ /\\/

/\ \/

Consider the following code: x = 10 y = -3 z = 3 print(x + y * z) What is the output? 1 4 16 7

1 Explanation: If we substitute in the variables from the print statement, we have ( 10 + -3 * 3 ). Following the order of operations,the first calculation would be -3 * 3 = -9. Then the addition would be 10 + -9 resulting in 1.

Which three of the following will be printed? c = 7 while (c > 0): print(c) c = c - 3 0 1 3 4 5 6 7

1 Explanation: In the third iteration of the loop, c has been decremented from 4 to 1 and this value is printed 4 Explanation: In the second iteration of the loop, c has been decremented from 6 to 4 and this value of c is printed. 7 Explanation: In the first iteration of the loop, c is its initial value of 7 and this is printed as the print statement comes before c = c - 3.

What is the output? print(13 % 3) 1 10 4 0

1 Explanation: The modulo operation (%) is used for collecting the remainder after division is performed. 13 / 3 is 1, 3 * 4 is 12,and the difference between what we have (12), and the total value (113), is the remainder, so the remainder is 1.

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

12

What is output by the following code: for x in range (5, 10): print (x * 3, end=" ") 8 9 10 11 12 13 15 18 21 24 27 30 15 18 21 24 27 5 6 7 8 9

15 18 21 24 27 Explanation: This is the correct printed value of x*3 when x starts at 5 and ends at 9.

Consider the following code: x = 17 y = 5 print(x % y) What is output? 1 3.4 3 2

2

Consider the following code: x = 17 y = 3 print(x % y) What is the output? 0 14 2 3

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

Consider the following code: a = 2 b = 3 print(a ** b) What is the output? 5 8 4 6

8 Explanation: Substituting in the variables of the print statement gives us (2 ** 3). The (x ** y) format is the operation forexponentiation. The value on the left of the operation is the base, and the value on the right of the operation is theexponent. Thus, (2 ** 3) is equivalent to (2 * 2 * 2) which equals 9.

To test if x is less than or equal to y: if (x ___ y): <= < > >=

<= Explanation: This is used to check if x is less than or equal to y

Find the mistake in the following code: if (name1 = "sue") : The variable "name1" must be capitalized "sue" is spelled wrong There should not be ( ) = should be ==

= should be == Explanation: = assigns a value to a variable and == is used to compare two values, so == is needed for the condition

Which of the following is used to show two values are equal? != =/ =- ==

== Explanation: This is used to check if two values are equal

Which of the following terms means "storing a value in a variable"? Assignment Operator Exponent Variable

Assignment

Which of the following is NOT an output device? -Speakers -Camera -Printer -Screen

Camera

Which variable name is the best to hold the area of a rectangle? area AREA 1A a

area

Which loop prints the numbers 1, 3, 5, 7, ..., 99? c = 1 while (c <= 99): c = c + 2 print(c) c = 1 while (c < 99): c = c + 1 print(c) c = 1 while (c <= 99): print(c) c = c + 2 c = 1 while (c < 99): print(c) c = c + 1

c = 1 while (c <= 99): print(c) c = c + 2 This loop starts at c = 1 and goes up to and including c = 99. It also correctlyincrements c with c = c + 2 after printing it, thus printing 1, 3, 5, 7, ..., 99.

Write a program that asks the user for their name and how many times to print it. If I enter Bob and 4 it should print: Bob Bob Bob Bob Which loop correctly does this? name = input("Enter your name: ") num = int(input("Enter a positive number: ")) c = 0 while (c > num): print (name) c = c + 1 name = input("Enter your name: ") num = int(input("Enter a number: ")) c = 0 while (c < num): print(name) c = c + 1 name = input("Enter your name: ") num = int (input("Enter a number: ")) c = 1 while (num <= name): print (name) c = c + 1 name = input("Enter your name: ") num = int(input("Enter a number: ")) c = 0 while(c <= num): print (name)

name = input("Enter your name: ") num = int(input("Enter a number: ")) c = 0 while (c < num): print(name) c = c + 1 Explanation: This loop sets c = 1 and checks c < num, incrementing c upward and printing the name each time. This correctly prints the name num times.

Ask the user to input a number less than 100. Print all the numbers from 0 to that number. Which loop correctly does this? num = int (input("Enter a number between 1 and 100: ")) c = num while (num <= c): print (c) c = c + 1 num = int (input("Enter a number between 1 and 100: ")) c = 0 while (num <= 0): print (c) c = c + 1 num = int (input("Enter a number between 1 and 100: ")) c = 0 while (c <= num): print (c) c = c + 1 num = int (input("Enter a number between 1 and 100: ")) c = 0 while (num >= 0): print (c) c = c + 1

num = int (input("Enter a number between 1 and 100: ")) c = 0 while (c <= num): print (c) c = c + 1 Explanation: This loop sets c = 0 and prints c as long as c <= num, incrementing upward. Thiscorrectly prints the numbers from 0 to num (the user input).


संबंधित स्टडी सेट्स

Week 2: Surgery and Nutrition support

View Set

Lifestyle and Career Development

View Set

Chapter 5 - Project Scope Management

View Set

THE HISTORY OF THE ENGLISH LANGUAGE QUIZ 1

View Set

Chapter 5 - Equal Rights: Struggling Towards Fairness

View Set

Strategic Management Exam 1, 1. Strategic Management and Strategic Competitiveness, Strategic Leadership, Strategic management Chapter 2, Strategic Management - Chapter 3

View Set