Description
State whether true or false:-
-
A Python while implements a definite loop.
-
The counted loop pattern uses a definite loop.
-
A sentinel loop asks the user whether to continue on each iteration.
-
A sentinel loop should not actually process the sentinel value.
-
The easiest way to iterate through the lines of a file in Python is to use a while loop.
-
The Boolean operator or returns True when both of its operands are true.
-
a and (b or c) == (a and b) or (a and c)
-
not(a or b) == (not a) or not(b)
-
True or False
Multiple Choice
-
A loop pattern that asks the user whether to continue on each iteration is called a(n)
-
interactive loop
-
end-of-file loop
-
sentinel loop
-
infinite loop
-
-
A loop pattern that continues until a special value is input is called a(n)
-
interactive loop
-
-
-
end-of-file loop
-
sentinel loop
-
infinite loop
-
-
A loop structure that tests the loop condition after executing the loop body is called a
-
pre-test loop
-
loop and a half
-
sentinel loop
-
post -test loop
-
-
A priming read is part of the pattern for a(n)
-
interactive loop
-
end-of-file loop
-
sentinel loop
-
infinite loop
-
-
What statement can be executed in the body of a loop to cause it to terminate?
-
-
if
-
input
-
break
-
exit
-
-
Which of the following is not a valid rule of Boolean algebra?
-
(True or x) == True
-
(False and x) == False
-
not(a and b)== not(a) and not(b)
-
(True or False) == True
-
-
A loop that never terminates is called
-
Busy
-
-
-
Indefinite
-
Tight
-
infinite
-
-
Which line would not be found in a truth table for and?
-
T T T
-
T F T
-
F T F
-
F F F
-
-
Which line would not be found in a truth table for or?
-
T T T
-
T F T
-
F T F
-
-
-
F F F
-
-
The term for an operator that may not evaluate one of its subexpressions IS
-
short –circuit
-
faulty
-
exclusive
-
indefinite
-
Discussion:
-
Compare and contrast the following pairs of terms:
-
definite loop vs. indefinite loop
-
for loop vs. while loop
-
-
-
interactive loop vs. sentinel loop
-
sentinel loop vs. end -of-file loop
-
-
Give a truth table that shows the Boolean value of each of the following Boolean expressions, for every possible combination of “input” values. Hint: Including columns for intermediate expressions is helpful.
-
-
not (P and Q)
-
(not P) and Q
-
-
-
(not P) or (not Q)
-
(P and Q) or R
-
-
Show the sequence of numbers that would be generated by each of the following range expressions.
-
-
range (5)
-
range (3, 10)
-
range (4, 13, 3)
-
range (15, 5, -2)
-
range (5, 3)
-
-
Show the output that would be generated by each of the following program fragments. You must show step by step.
a) for i in range (1, 11) : print (i*i)
-
for i in [1,3,5,7,9]: print (i, “:”, i**3)
print (i)
-
x = 2 y = 10
for j in range (0, y, x) : print (j, end=””) print (x + y)
print (“done”)
-
ans = 0
for i in range (1, 11) : ans = ans + i*i print (i)
print (ans)
-
Write a while loop fragment that calculates the following values:
-
Sum of the first n counting numbers: 1 + 2 + 3 + . . . + n
-
Sum of the first n odd numbers: 1 + 3 + 5 + . . . + 2n – 1
-
-
-
Sum of a series of numbers entered by the user until the value 999 is entered. Note: 999 should not be part of the sum.
-
-
-
The number of times a whole number n can be divided by 2 (using integer division) before reaching 1.
-
Programming Exercises:
-
The Fibonacci sequence starts 1, 1, 2, 3, 5, 8,. … Each number in the sequence (after the first two) is the sum of the previous two. Write a program that
computes and outputs the nth Fibonacci number, where n is a value entered by the user.
-
Write a program that uses a while loop to determine how long it takes for an investment to double at a given interest rate. The input will be an annualized interest rate, and the output is the number of years it takes an investment to double. Note: The amount of the initial investment does not matter; you can use $ 1.
3. A positive whole number n > 2 is prime if no number between 2 and √ |
(inclusive) |
|
evenly divides n. Write a program that accepts a value of n as |
input and |
determines if the value is prime. If n is not prime, your program should quit as soon as it finds a value that evenly divides n.
-
Modify the previous program to find every prime number less than or equal to n.
-
Write a program to find the maximum and minimum of n numbers entered by the user.
-
Evaluate the following program manually (step by step) for the following cases:-
-
= int(input(‘Enter x>’))
-
= int(input(‘Enter y>’))
-
if x > y:
smaller = y
else:
smaller = x
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):
hcf = i
print(hcf)
x |
10 |
10 |
23 |
1000 |
||
y |
50 |
53 |
17 |
1010 |
||
__________________________________________________________________________ |