You are on page 1of 8

SIMAD UNIVERSITY ‫جــامــعـــة ســـيــمــد‬

JAAMACADDA SIMAD

October 14, 2021


Name:

Faculty:

Program:

Class:

ID No. :

Phone No. :

Subject : Pearson Starting Out with Python 5rd Tony Gaddis (2021)

October 14, 2021


Prepared by: mohammedkhaliffarah@gmail.com 1
Q1) What is a repetition structure?
October 14, 2021
A1) A structure that causes a section of code to repeat

Q2) What is a condition-controlled loop?

A2) A loop that uses a true/false condition to control the number of times that it repeats

Q3) What is a count-controlled loop?

A3) A loop that repeats a specific number of times

Q4) What is a loop iteration?

A4) An execution of the statements in the body of the loop

Q5) Does the while loop test its condition before or after it performs an iteration?

A5) Before

Q6) How many times will 'Hello World' be printed in the following program?

count = 10

while count < 1:

print('Hello World')

A6) None. The condition count < 0 will be false to begin with.

Q7) What is an infinite loop?

A7) A loop that has no way of stopping and repeats until the program is interrupted.

Q8) Rewrite the following code so it calls the range function instead of using the list

[0, 1, 2, 3, 4, 5]:

for x in [0, 1, 2, 3, 4, 5]:

print('I love to program!')

A8) for x in range(6):

print('I love to program!')

October 14, 2021


Prepared by: mohammedkhaliffarah@gmail.com 2
Q9) What will the following code display?

for number in range(6):

print(number)

A9) 0

1
October 14, 2021
2

Q10) What will the following code display?

for number in range(2, 6):

print(number)

A10) 2

Q11) What will the following code display?

for number in range(0, 501, 100):

print(number)

A11) 0

100

200

300

400

500

October 14, 2021


Prepared by: mohammedkhaliffarah@gmail.com 3
Q12) What will the following code display?

for number in range(10, 5, −1):

print(number)

A12) 10

9
October 14, 2021
8

Q13) What is an accumulator?

A13) A variable that is used to accumulate the total of a series of numbers

Q14) Should an accumulator be initialized to any specific value? Why or why not?

A14) Yes, it should be initialized with the value 0. This is because values are added to the accumulator by
a loop. If the accumulator does not start at the value 0, it will not contain the correct total of the
numbers that were added to it when the loop ends.

Q15) What will the following code display?

total = 0

for count in range(1, 6):

total = total + count

print(total)

A15) 15

Q16) What will the following code display?

number 1 = 10

number 2 = 5

number 1 = number 1 + number 2

print(number1)

print(number2)

A16) 15

October 14, 2021


Prepared by: mohammedkhaliffarah@gmail.com 4
Q17) Rewrite the following statements using augmented assignment operators:

a) quantity = quantity + 1

b) days_left = days_left − 5

c) price = price * 10

d) price = price / 2
October 14, 2021
A17) a) quantity += 1

b) days_left −= 5

c) price *= 10

d) price /= 2

Q18) What is a sentinel?

A18) A sentinel is a special value that marks the end of a list of items.

Q19) Why should you take care to choose a distinctive value as a sentinel?

A19) A sentinel value must be unique enough that it will not be mistaken as a regular value in the list.

Q20) What does the phrase “garbage in, garbage out” mean?

A20) It means that if bad data (garbage) is provided as input to a program, the program will produce bad
data (garbage) as output.

Q21) Give a general description of the input validation process.

A21) When input is given to a program, it should be inspected before it is processed. If the input is
invalid, then it should be discarded and the user should be prompted to enter the correct data.

Q22) Describe the steps that are generally taken when an input validation loop is used to validate
data.

A22) The input is read, then a pretest loop is executed. If the input data is invalid, the body of the loop
executes. In the body of the loop, an error message is displayed so the user will know that the input was
invalid, and then the input read again. The loop repeats as long as the input is invalid.

Q23) What is a priming read? What is its purpose?

A23) It is the input operation that takes place just before an input validation loop. The purpose of the
priming read is to get the first input value.

Q24) If the input that is read by the priming read is valid, how many times will the input validation
loop iterate

A24) None

October 14, 2021


Prepared by: mohammedkhaliffarah@gmail.com 5
Review Questions
Multiple Choice

1. A __________ -controlled loop uses a true/false condition to control the number of times that it
repeats.

a. Boolean b. condition c. decision d. count


October 14, 2021
2. A __________ -controlled loop repeats a specific number of times.

a. Boolean b. condition c. decision d. count

3. Each repetition of a loop is known as a(n) __________.

a. cycle b. revolution c. orbit d. iteration

4. The while loop is a __________ type of loop.

a. pretest b. no-test c. prequalified d. post-iterative

5. A(n) __________ loop has no way of ending and repeats until the program is interrupted.

a. indeterminate b. interminable c. infinite d. timeless

6. The -= operator is an example of a(n) __________ operator.

a. relational b. augmented assignment c. complex assignment d. reverse assignment

7. A(n) __________ variable keeps a running total.

a. sentinel b. sum c. total d. accumulator

8. A(n) __________ is a special value that signals when there are no more items from a list of items to
be processed. This value cannot be mistaken as an item from the list.

a. sentinel b. flag c. signal d. accumulator

9. GIGO stands for .

a. great input, great output b. garbage in, garbage out c. GIGahertz Output d. GIGabyte Operation

10. The integrity of a program’s output is only as good as the integrity of the program’s .

a. compiler b. programming language c. input d. debugger

11. The input operation that appears just before a validation loop is known as the .

a. prevalidation read b. primordial read c. initialization read d. priming read

12. Validation loops are also known as .

a. error traps b. doomsday loops c. error avoidance loops d. defensive loops

October 14, 2021


Prepared by: mohammedkhaliffarah@gmail.com 6
True or False

False 1. A condition-controlled loop always repeats a specific number of times.

False 2. The while loop is a pretest loop.

False 3. The following statement subtracts 1 from x: x = x − 1.

True 4. It is not necessary to initialize accumulator variables.

5. In a nested loop, the inner loop goes through all of its iterations for every single itera- tion of
True
the outer loop.

True 6. To calculate the total number of iterations of a nested loop, add the number of itera- tions of
all the loops.

7. The process of input validation works as follows: when the user of a program enters invalid
True
data, the program should ask the user “Are you sure you meant to enter that?” If the user
answers “yes,” the program should accept the data.

Short Answer

Q1) What is a condition-controlled loop?

A1) A condition-controlled loop uses a true/false condition to control the number of times that it
repeats.

Q2) What is a count-controlled loop?

A2) A count-controlled loop repeats a specific number of times.

Q3) What is an infinite loop? Write the code for an infinite loop.

A3) An infinite loop continues to repeat until the program is interrupted. Infinite loops usually occur
when the programmer forgets to write code inside the loop that make test condition false. Example:
x = 99
While 》 0
Display x
End While

Q4) Why is it critical that accumulator variables are properly initialized?

A4) If the accumulator starts with any value other than 0, it will not contain the correct total when the
loop finishes.

Q5) What is the advantage of using a sentinel?

A5) You can write a loop that processes a list of data items when you do not know the number of data
items in the list, and (1) you do not want to ask the user if there are more items at the end of each loop
iteration, and (2) you do now want to require the user to know the number of items in the list in
advance.

October 14, 2021


Prepared by: mohammedkhaliffarah@gmail.com 7
Q6) Why must the value chosen for use as a sentinel be carefully selected?

A6) A sentinel value must be unique enough that it will not be mistaken as a regular value in the list.

Q7) What does the phrase “garbage in, garbage out” mean?

A7) It means that if bad data (garbage) is provided as input to a program, the program will produce bad
data (garbage) as output.

Q8) Give a general description of the input validation process.

A8) When input is given to a program, it should be inspected before it processed. If the input is invalid,
then it should be discarded and the user should be prompted to enter the correct data.

October 14, 2021


Prepared by: mohammedkhaliffarah@gmail.com 8

You might also like