Preview

09 - Interpret Correct or Complete Algorithms #1

 1. An algorithm is a process or a set of instructions used to solve a problem

  True

  False

 2. Key algorithm constructs are:

  Sequence

  All of the above

  Iteration

  Selection

 3. In the following algorithm, if more than 35 hours are worked ….
INPUT hours_worked
INPUT pay_rate
IF hours_worked <= 35 THEN
gross_pay = pay_rate x hours_worked
ELSE
gross_pay = (pay_rate x 35)+
(1.5 x pay_rate x
(hours_worked - 35))
ENDIF
OUTPUT gross_pay
END

  time-and-a-half is paid for the extra hours

  1.5 + 35 is added to the extra hours rate

  then 35 is added to the extra hours

  the gross_pay is paid for the extra hours

 4. What does the following algorithm do?
INPUT item_price
INPUT vat_rate
vat_amount = item_price x vat_rate
final_price = item_price + vat_amount
OUTPUT final_price
END

  calculate the Sales Tax (VAT) of an item and then works out the final price of an item

  None of the above

  calculates the final vat amount of any given item with a price of x

  calculates the final price of several items based on their vat rate x the vat amount

 5. How many variables can you count in the following algorithm, and what does it do?
INPUT total_items
SET total_cost = 0
SET count = 0
WHILE count < total_items
INPUT item_price
total_cost = total_cost + item_price
count = count + 1
ENDWHILE
average_price = total_cost / total_items
OUTPUT average_price
END

  None of the above

  five variables - this program calculates the total cost divided by the average price, which gives the final price

  two variables - this program calculates the average price for a number of items

  five variables - this program calculates the average price for a number of items

 6. ______is a detailed yet readable description of what a computer program must do, expressed in a natural language(e.g. english) rather than in a programming language.

  English Code (EC)

  Pseudocode

  Algorithmic Thinking

  Assembly language

 7. All of the following are examples of ________
str(3)
int("3")
float("3.14")

  Pseudolanguage

  Masting

  Coling

  Casting

 8. What is the output of the following algorithm/code -assuming it is in Python?
for i = 0 to 9
print("Hi")

  None of the above

  Outputs "Hi" on the screen 9 times

  Outputs "Hi" on the screen 90 times

  Outputs "Hi" on the screen 8 times

 9. In the following program, if the user enters the password: "open123", what happens?
password = ""
while password != "[email protected]"
password = input("Password?")
endwhile

  A prompt asking the user to enter: "[email protected] is displayed

  "[email protected]" must be entered else, the program repeats

  Error

  The input is accepted and the 'end while' comes into play

 10. What sort of structure/selection is shown in this algorithm?
switch coinResult:
case "heads":
print("You got heads")
case "tails":
print("You got tails")
default:
print("Not valid coin")
endswitch

  IF statements

  For loops

  switch statement (switch/case) - python does not have this and a dictionary could be used to achieve the same thing

  While loops

 11. What is happening in the following algorithm? Specifically, what happens to the variable x?
a = 0
b = 0
procedure move(x: byVal, y: byRef)
x = x + 1
y = y + 1
endprocedure
move(a, b)
print(a)
print(b)

  the variable x will be passed to the procedure directly. Changing x changes the original value

  the variable x will be passed into y and changing x will change the original value

  The final value of x = a

  the variable x will be passed to the procedure by copying the value. Changing x in the procedure won’t change the original value

 12. What is the output here? Careful - it is not entirely obvious!
some_global = 10
def func1():
 some_global = 20
def func2():
 print(some_global)
func1()
func2()

  40

  10

  30

  20

 13. The programmer wants a variable inside a function to be treated as 'global' and the following code achieves this.
def func1():
 global some_global #use global keyword
 my_global = 20

  FALSE

  TRUE

 14. The following statement will evaluate to ……
(10 >= 1) and (1 < 2)

  FALSE

  TRUE

 15. The following statement will evaluate to …..
>>> (1>2) and (9>=1)

  TRUE

  FALSE

 16. In the following algorithm, the elif has _____ conditional tests.
limbs = 4
if limbs == 4:
 print('no one shall pass')
elif limbs > 0 <4:
 print('tis but a scratch')
else:

  2

  3

  4

  1

 17. What is the output of the following code?
n = 0
for num in range(5):
 n = n + num
print (n)

  20

  15

  5

  10

 18. Which of the following statements is correct, in reference to the following code?
while answer!=”computer”
 answer=input(“What is the
password?”)
endwhile
do
 answer=input(“What is the
password?”)
until answer == “computer”

  The while loop will keep looping while its condition is True.

  None of the above

  The while loop will stop if the condition is True

  The while loop will end if the answer is = "open123"

 19. Which of the following statements is true of this algorithm?
while 1 == 1:
 print ("lol")

  It creates an infinite loop because 1 is always equal to 1

  It does not print "lol" at all, because the while loop condition is never met

  It prints "lol" twice and then crashes, as 1 is already equal to 1

  It prints "lol" exactly once

 20. The following algorithm makes use of a condition controlled loop
	01 input b 
02 for x = 1 to 100 
03     print b * x
04 next

  FALSE

  TRUE