Exam 2 CSCI 256 Flashcards
What operator is used for “less than or equal to” in the condition of a loop?
What operator is used for “greater than or equal to” in the condition of a loop?
<=
>=
How can “less than or equal” be written using the > (greater than) sign? For example,
re-write x <= 10 using >.
not(x > 10)
If x = True, y = 10, z = 20, evaluate not x or (y == z/2)
True
If x = True, y = 10, z = 20, evaluate x and not (y*2 <= z)
False
What is the syntax of an if-elif-else statement (compare integer x to 5 as an example.
Output whether x is greater than, less than, or equal to 5)?
x = int(input(‘Enter a number:’))
if x > 5
elif x < 5
else x = 5
How to write down the condition if number is in the range 1 to 10, inclusive?
if 1 <= number <= 10:
OR
x >= 1 and x <= 10
myString == ‘abcdefg’, what is the following? myString[2:5] myString[3:] myString[:3] myString[:] and myString[-1:-3:-1]
(HINT: a is 0 and the second number is NOT included)
myString[2:5] = cde
myString[3:] = defg
myString[:3] = abc
myString[:] = abcdefg
myString[-1:-3:-1] = gf
A while-loop is a ___-controlled loop
Condition
A for-loop is a ____-controlled loop
Count
One execution of a loop block is called what?
Iteration
A while-loop that never evaluates to false is said to be in what?
Infinite loop
True or false: A for-loop executes a fixed number of times?
True
Which type of loop – for-loop or while-loop – is used when we don’t know how many iterations are to be performed?
While-loop
for i in range (0,20) executes how many iterations?
What about for i in range(0, 20, 2)?
for i in range (20, 2, -3)?
for i in range (0,20): 20 times (0 - 19)
for i in range(0, 20, 2): 10 iterations
for i in range(20, 2, -3): 6 iterations
Assuming x is initialized to 0, how many iterations of the while-loop are executed? What is the final value of x? while x >11: x = x + 3
0 iterations because the condition is false!
x = 0
while x > 11:
x = x + 3
You’re saying zero is equal to eleven?!
Assuming x = 10 and y = 0, how many iterations of the while loops are executed? while y < 10: while y< 10 x = x + y, x = x + y, y = y + 2
While y < 10 is infinite
Construct a while-loop that adds numbers from a user. Use -1 as the sentinel value to exit.
total = 0
num = int(input(‘Enter a number (-1 to exit): ‘))
while num != -1:
total += num
num = int(input(‘Enter a number (-1 to exit): ‘))
print(‘The total is %d’ % total)
Construct a for-loop that prompts the user for 4 numbers and outputs whether the number is a multiple of 3.
for i in range(4):
num = int(input(“Enter number %d: “ % (i+1)))
if num % 3 == 0:
print(‘%d is a multiple of 3.’ % num)
else:
print(‘%d is not a multiple of 3.’ % num)
Construct an if-statement that outputs ‘hot’ if the temperature is above 90-degrees and ‘cold’ if the temperature is below 55-degrees. Use the integer temp as your temperature variable.
temp = int(input(‘Enter a temperature: ‘))
if temp > 90:
print(‘Hot’)
elif temp < 55:
print(‘Cold’)
else:
print(‘Normal’)
- What is the output of the following fragment?
for i in range(0, 12):
if (i % 4) == 0:
print (‘%d’ % i)
a. 0 2 4 6 8 10 12
b. 0 4 8 12
c. 4 8 12
d. 0 4 8
D
You start at zero and go by the multiples of four. 12 IS NOT INCLUDED
Consider the following code that assigns a letter grade to a student’s score:
if score >= 90:
grade = ‘A’
if score >= 80:
grade = ‘B’
if score >= 70:
grade = ‘C’
if score >= 60:
grade = ‘D’
else:
grade = ‘F’
a. This code will work correctly in all cases
b. This code will work correctly if grade >= 60
c. This code will work correctly if grade < 60
d. This code never works correctly
C
What is the output from the code segment? Which of the following for-loops produces the
equivalent result to this code segment?
total = 0
for i in range(2, 20, 2):
total += i
print(total)
a. for i in range(20, 1, -1)
b. for i in range(19, 1, -1)
c. for i in range(20, 2, -2)
d. for i in range(18, 0, -2)
D
Rewrite the following code fragment using a for loop instead of a while loop.
i = 0
while i <= 50:
print(‘%d ‘ % i)
i += 2
(HINT: The numbers are in order, but the second must be added by one as that’s the end value)
for i in range (0, 51, 2):
print(‘%d” % i)