Exam 2 CSCI 256 Flashcards

1
Q

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?

A

<=
>=

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can “less than or equal” be written using the > (greater than) sign? For example,
re-write x <= 10 using >.

A

not(x > 10)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

If x = True, y = 10, z = 20, evaluate not x or (y == z/2)

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

If x = True, y = 10, z = 20, evaluate x and not (y*2 <= z)

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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)?

A

x = int(input(‘Enter a number:’))
if x > 5
elif x < 5
else x = 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to write down the condition if number is in the range 1 to 10, inclusive?

A

if 1 <= number <= 10:

OR

x >= 1 and x <= 10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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)

A

myString[2:5] = cde

myString[3:] = defg

myString[:3] = abc

myString[:] = abcdefg

myString[-1:-3:-1] = gf

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

A while-loop is a ___-controlled loop

A

Condition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

A for-loop is a ____-controlled loop

A

Count

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

One execution of a loop block is called what?

A

Iteration

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

A while-loop that never evaluates to false is said to be in what?

A

Infinite loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

True or false: A for-loop executes a fixed number of times?

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Which type of loop – for-loop or while-loop – is used when we don’t know how many iterations are to be performed?

A

While-loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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)?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

A

0 iterations because the condition is false!

x = 0
while x > 11:
x = x + 3

You’re saying zero is equal to eleven?!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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

A

While y < 10 is infinite

17
Q

Construct a while-loop that adds numbers from a user. Use -1 as the sentinel value to exit.

A

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)

18
Q

Construct a for-loop that prompts the user for 4 numbers and outputs whether the number is a multiple of 3.

A

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)

19
Q

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.

A

temp = int(input(‘Enter a temperature: ‘))

if temp > 90:
print(‘Hot’)
elif temp < 55:
print(‘Cold’)
else:
print(‘Normal’)

20
Q
  1. 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
A

D

You start at zero and go by the multiples of four. 12 IS NOT INCLUDED

21
Q

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

22
Q

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)

23
Q

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)

A

for i in range (0, 51, 2):
print(‘%d” % i)