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:

  All of the above

  Sequence

  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

  1.5 + 35 is added to the extra hours rate

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

  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

  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

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

  None of the above

 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

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

  None of the above

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

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

 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.

  Algorithmic Thinking

  Pseudocode

  English Code (EC)

  Assembly language

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

  Masting

  Coling

  Casting

  Pseudolanguage

 8. The output of the following algorithm/code (assume it is Python) is that it prints "Hi" 8 times.
for i = 0 to 9
    print("Hi")

  True

  False

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

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

  "CdRtp45@" must be entered else, the program repeats

  A prompt asking the user to enter: "CdRtp45@ is displayed

  Error

 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

  While loops

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

  IF statements

  For 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 final value of x = a

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

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

  30

  20

  10

  40

 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

  TRUE

  FALSE

 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:

  4

  1

  2

  3

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

  10

  5

  15

  20

 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”

  None of the above

  The while loop will stop if the condition is True

  The while loop will keep looping while its 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 prints "lol" exactly once

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

  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

 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

  TRUE

  FALSE