Unit 4 - Test
Consider the following code: val = 0 while val > 10: val = val + 1 print(val) If the goal of the code is to print the numbers 1 - 10, then what is the error of the code?
It should be val < 10
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
Consider the following code: for x in range(10 28): print(x) What is wrong with this program?
The first line should be: for x in range(10, 28):
Consider the following code that is meant to print out 7, 8 and 9 on separate lines: for x in (7, 10): print(x) What is wrong with the program?
The first line should be: for x in range(7, 10):
Which of the following is true of algorithms?
They have an order
When do you use a while loop INSTEAD of a for loop?
To get input from the user until they input 'stop'. When there is an unknown number of iterations needed.
For a loop to stop, the loop control variable must _______________.
be a certain value
Which loop prints the numbers 1, 3, 5, 7, ...99?
c = 1 while c <= 99: print(c) c = c + 2
In a(n) _________________ loop, you know ahead of time how many times the loop will repeat.
count variable
What is the output by the following code? for x in range(3): for y in range(4): print("*", end=" ") print(" ")
* * * * * * * * * * * *
Which three of the following will be printed? c = 7 while c > 0: print(c) c = c-3
1, 4 , 7
What is output by the following code: for x in range(5, 10): print(x * 3, end=" ")
15 18 21 24 27
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
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 enter 10 then 15?
23
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
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 number: ") c = 0 while c < num: print(name) c = c + 1
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 = 0 while c <= num: print(c) c = c + 1
Information sent to a function is a?
parameter
Which range function will print all odd numbers between 11 and 33?
range(11, 34, 2)
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 = 10 y = 20