Docsity

computer science revision questions, Exercises of Computer science

Toggle sidebar
bg1
Q1.
The figure below shows an algorithm, represented using pseudo-code.
The algorithm calculates the total cost of hiring a hotel for a wedding.
numberOfGuests โ† USERINPUT
numberOfRooms โ† USERINPUT
charge โ† 25
IF numberOfGuests > 50 THEN
totalCost โ† numberOfGuests * 2
ELSE
IF numberOfGuests โ‰ฅ 25 THEN
totalCost โ† numberOfGuests * 4
ELSE
totalCost โ† numberOfGuests * 5
ENDIF
ENDIF
totalCost โ† totalCost + (numberOfRooms * 100)
IF totalCost < 1400 THEN
totalCost โ† totalCost + charge
ENDIF
OUTPUT totalCost
Complete the table below using the algorithm in the figure above.
๎˜
Input value for
numberOfGuests
Input value for
numberOfRooms Output
50 30 3200
20 10 1125
500 5 ๎˜ก1500
(Total 3 marks)
Page 1 of 8
pf3
pf4
pf5
pf8

Partial preview of the text

Download computer science revision questions and more Exercises Computer science in PDF only on Docsity!

Q1.

The figure below shows an algorithm, represented using pseudo-code. The algorithm calculates the total cost of hiring a hotel for a wedding.

numberOfGuests โ† USERINPUT

numberOfRooms โ† USERINPUT

charge โ† 25

IF numberOfGuests > 50 THEN

totalCost โ† numberOfGuests * 2

ELSE IF numberOfGuests โ‰ฅ 25 THEN

totalCost โ† numberOfGuests * 4

ELSE

totalCost โ† numberOfGuests * 5

ENDIF ENDIF

totalCost โ† totalCost + (numberOfRooms * 100)

IF totalCost < 1400 THEN

totalCost โ† totalCost + charge

ENDIF OUTPUT totalCost Complete the table below using the algorithm in the figure above. Input value for numberOfGuests Input value for numberOfRooms Output (^50 30 ) (^20 10 ) (^500 5 ) (Total 3 marks)

Q2.

Explain how the linear search algorithm works.













(Total 3 marks)

Q3.

(a) Define the term abstraction.



(1) (b) State the name for the process of breaking a problem down into sub-problems.


(1) (Total 2 marks)

OUTPUT "Please guess a letter"

letter โ† USERINPUT

(a) Which pseudo-code statement assigns the length of the string film to a variable called value? Shade one lozenge.

A film^ โ†^ LEN(value)

B film^ โ†^ film + value

C value^ โ†^ film

D value^ โ†^ LEN(film)

(1) (b) The POSITION subroutine returns the position of the first occurrence of a character in a string. For example:

  • POSITION (โ€œGodzilla vs. Kongโ€, โ€œoโ€) would return 1
  • POSITION (โ€œGodzilla vs. Kongโ€, โ€œzโ€) would return 3 letter and film are variables used in the algorithm in the figure above. Complete the pseudo-code statement to find the position of the first occurrence of the contents of letter in film and store this position in the variable location. You must use the POSITION subroutine in your answer.

location โ† _______________________________________________________

___________________________________________________________________

(1) (c) Which of the following would be the most suitable data type for the variable year? Shade one lozenge. A Boolean B character C integer D real (1) (Total 3 mark)

Q6.

Describe what is meant by an assignment statement in a program.



(Total 1 mark)

Q7.

Write a Python program that:

  • gets the user to enter the name of a film
  • displays You entered followed by the name of the film entered by the user. The output from the program must be on one line. You should use indentation as appropriate, meaningful variable name(s) and Python syntax in your answer. The answer grid below contains vertical lines to help you indent your code accurately. (Total 2 marks)

Q8.

The figure shows an incomplete Python program for a number guessing game.

  • Line numbers are included but are not part of the program. 1 import random 2 3 print("Enter a number") 4 userNumber = int(input()) 5 while userNumber < 1 or userNumber > 100: 6 print("Invalid number") 7 userNumber = int(input()) 8 print("Valid number entered") 9 if randomNumber == userNumber: 10 print("Number guessed correctly") (a) The program should generate a random number between 1 and 100 (including 1 and 100). This will be the number the user has to guess. Write the Python code that should be used on line 2 in the figure above to:
    • generate a random number between 1 and 100 inclusive
    • assign this number to the appropriate variable from the program. You must use random.randrange(a, b) in your Python code. random.randrange(a, b) generates a random integer in the range a to b starting at a but finishing one before b


     **(2)** 

(b) Complete the test plan in the table below to test the validation of userNumber in the program in the figure above. Test number Test type Test data Expected result 1 Erroneous 150 2 Boundary 3 Normal Valid number entered (2) (c) In an earlier version of the program in Figure 1 , line 5 contained one syntax error and one logic error: whil userNumber < 1 or userNumber >= 100:

Complete the table below to describe the errors in the program on line 5. Error type Description Syntax error Logic error (2) (Total 6 marks)