Skeleton Program Flashcards
Explain why a FOR repetition structure was chosen instead of a WHILE repetition structure.
The code is to be repeated a known number of times
Give an example of instantiation from the Skeleton Program
TileQueue = QueueOfTiles(20)
Look at the Main function in the Skeleton Program.
Why has a named constant been used for MaxHandSize instead of the numeric value 20?
Makes the program code easier to understand;
Makes it easier to update the program;
Create a class definition for QueueOfTiles
QueueOfTiles = class Public Function IsEmpty Function Remove Procedure Add Procedure Show Private _Contents: Array _Rear: Integer _MaxSize: Integer
Give an example of an assignment statement from the Skeleton Program where a variable is assigned an empty string.
Choice = “”
State the name of an identifier for: a variable that has a stepper role
Count
for Count in range(self._MaxSize):
State two reasons why subroutines should, ideally, not use global variables
Easier reuse of routines in other programs;
Routine can be included in a library;
Helps to make the program code more understandable;
Ensures that the routine is self-contained // routine is independent of the rest of the program;
(Global variables use memory while a program is running) but local variables use memory for only part of the time a program is running;
reduces possibility of undesirable side effects;
Using global variables makes a program harder to debug
There is a variable called Count in the CreateTileDictionary subroutine.
There is also a variable called Count in the GetStartingHandsubroutine.
Explain why these two different variables can have the same identifier .
Because the scope; of the two variables is different; //
Because they are both local variables; in different subroutines;
A local variable (excluding parameters) within the HaveTurn subroutine
NewTileChoice / ValidChoice / ValidWord / Choice
An attribute within QueueOfTiles
_Contents / _Rear / _MaxSize
Write one line of code from the skeleton program which calls a library subroutine
RandNo = random.randint(0, 25)
Look at the subroutine CreateTileDictionary. Describe the purpose of the following line:
TileDictionary[chr(65 + Count)] = 1
Adds (an entry) to the dictionary
Entry includes character with ASCII value of 65 + ‘count’
Entry includes integer value ‘1’
State and describe the data structure returned by the CreateTileDictionary subroutine.
Dictionary Contains unique keys Keys are letters of the alphabet Each key links to another value Other value is an integer / the score for that letter
Look at the subroutine CheckWordIsInTiles. Explain the role of the variable CopyOfTiles.
Creates a copy of the player’s tiles
Characters are eliminated from the copy
Prevents tiles being eliminated from player’s tiles (since word has not been played yet)
Describe the difference between a procedure and a function. State one example of each from the skeleton code.
A procedure performs a sequence of events but does not return a value, e.g DisplayMenu, DisplayTilesInHand
A function also performs a sequence of events but does return a value, e.g CheckWordIsInTiles
Describe what would happen if, during a call to LoadAllowedWords, the ‘aqawords.txt’ file was not found.
An exception would be thrown
Execution would pass to the ‘except’ block
The code to populate the AllowedWords list would not be executed
The ‘except’ block contains no code/instructions
An empty list would be returned
Describe the actions performed in the following lines of the LoadAllowedWords subroutine:
for Word in WordsFile:
AllowedWords.append(Word.strip().upper())
Goes through the file one line at a time
Assigning the contents of each line to the variable Word
The Word/line is stripped of any trailing spaces and end of line characters/newlines
The Word/variable is converted to upper case
And appended to the list AllowedWords (or list of allowed words)
Describe what is meant by the term ‘constructor’
A constructor is a method called by creating a variable using the class name e.g. variable = Class() Creates a new object based on the class in which it resides Constructor method is called \_\_init\_\_ Ensures that a new object in the class is initialised correctly
Describe in detail the purpose of the subroutine UpdateScoreWithPenalty. Describe any parameters and/or return values in your answer.
Player’s current score, tiles in the player’s hand, TileDictionary that links tiles to points
Player’s updated score
Purpose of function is to subtract value of player’s tiles/hand from their score
Loop is established to iterate through each tile/character in the player’s hand
Value of each tile is determined by looking up in a dictionary
Value is subtracted from player’s score
State the name of an identifier for a variable that has a most wanted holder role.
A variable that keeps track of the lowest or highest value in a set of inputs
Count
Explain the difference between a protected attribute and a private attribute
A protected attribute can be accessed (within its class and) by derived class instances / subclasses; A private attribute can only be accessed within its class;