Sub-Programs Assessment

P06d

Question 1

P06d

Define the term ‘array’. (2)

An array is a collection (1) of elements
that are all homogenous/the same data type (1).

Question 2

P06d

Python does not have built-in arrays. Give the name of the data structure that Python uses to implement arrays? (1)

List

Question 3

P06d

Give the line(s) of code needed to create (declare/initialise) a list of integers named myNumbers, from 5 to 10. (2)

myNumbers = [ 5, 6, 7, 8, 9, 10]
myNumbers = (1)
[5, 6, 7,8, 9, 10] (1)

Question 4

P06d

Load file ‘Q04_Student’ into your programming environment. 

  • Add a ‘for in list’ loop to print each item in ‘fruit’ on a separate line. 
  • Save your file as ‘Q04_Finished’. (2)
for item in fruits: (1)
    print (item) (1)

Question 5

P06d

Load file ‘Q05_Student’ into your programming environment. 

  • Add a ‘for in range’ loop to print each item in ‘fruit’ on a separate line, in reverse order. 
  • Save your file as ‘Q05_Finished’. (4)
for index in range (len(fruits) - 1, -1, -1):
    print (fruits[index])
  • for <index> in range (1).
  • len (fruits) (1).
  • correct start, end, and step (1).
  • print line using range variable (1).

Question 6

P06d

Subprograms may or may not return results. Give two other features of subprograms. (2)

One mark for each of the following up to a maximum of (2):

  • blocks of code defined outside the main program (1)
  • use the keyword ‘def’ (1)
  • may or may not take parameters (1).​

Question 7

  • P06d

State the type of subprogram that always returns a result. (1)

Function

  • Question 8

  • P06d
  • Load file‘Q08_Student’ into your programming environment. (6)
  • Amend the code to:
  • include a subprogram to simulate the roll of a die
  • include a call to the subprogram in the main program
  • display the result to the user in the main program.
  • Save your completed code as ‘Q08_Finished’
  • There will be many different solutions. One mark each, up to a maximum of (6), for the following answers:
  • importing of random library (1)
  • definition line for a suitably named subprogram (1)
  • use of a random function to generate a number between 1 and 6, inclusive (1)
  • return the value of the roll to the caller (1)
  • calling the subprogram in the main program (1)
  • printing the result in the main program (1).
  • import random
    theRoll = 0
    def rollTheDie ():
        roll = 0
        roll = random.randint(1,6)
        return (roll)
    theRoll = rollTheDie ()
    print (theRoll)
  • Question 9

  • P06d
  • Load ‘Q09_Student’ into your development environment. 
  • Amend the code so that it meets these requirements: 
  • includes a subprogram named ‘displaySky’
  • the subprogram takes two lists as its input parameters 
  • the subprogram should display a star name, followed by a single space, and the star’s distance from Earth
  • each star should be displayed on a separate line 
  • the main program should only have two lines of code 
  • the program should display ‘Goodnight’ when finished.
  • Save your completed code as ‘Q09_Finished’. (10)
  • Question 9

  • P06d
  • One mark for each of the following up to a maximum of (10):
  • definition line for ‘displaySky’ has two input parameters (1)
  • parameter names are different to global variables (1)
  • loop to process each item in lists (1)
  • for in range loop used to find index (1)
  • uses input parameters for the data structure being indexed (1)
  • output line constructed with string concatenation (1)
  • conversion of real distances to string (1)
  • call in main program sends in two lists, in the same order as subprogram definition (1)
  • main program displays ‘Goodnight’ (1)
  • fully functional and runs (1).

Question 9

  • P06d
  • # ------------------------------------------------------------
    # Global variables
    # ------------------------------------------------------------
    starNames = ["Rigel", "Canopus", "Sirius", "Betelgeuse", "Vega"]
    starDistance = [860.0, 74.0 , 8.6, 1500.0, 25.0]
    
    # ------------------------------------------------------------
    # Subprograms
    # ------------------------------------------------------------
    def displaySky (pNames, pDistance):
        for index in range (0, len(pNames)):
            print (pNames[index] + " " + str(pDistance[index]))
    
    # ------------------------------------------------------------
    # Main program
    # ------------------------------------------------------------
    displaySky (starNames, starDistance)
    print ("Goodnight")
# ------------------------------------------------------------
# Global variables
# ------------------------------------------------------------
starNames = ["Rigel", "Canopus", "Sirius", "Betelgeuse", "Vega"]
starDistance = [860.0, 74.0 , 8.6, 1500.0, 25.0]

# ------------------------------------------------------------
# Subprograms
# ------------------------------------------------------------
def displaySky (pNames, pDistance):
    for index in range (0, len(pNames)):
        print (pNames[index] + " " + str(pDistance[index]))

# ------------------------------------------------------------
# Main program
# ------------------------------------------------------------
displaySky (starNames, starDistance)
print ("Goodnight")

ct03f Assessment

By David James

ct03f Assessment

  • 520