2.2.1 Programming Techniques Flashcards

1
Q

What is programming

A
  • Is the process of creating a set of instructions that can be executed by the computer
  • There are many different programming paradigms (models), including object-oriented, procedural, functional and declarative
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a Sequence

A
  • Sequence means that each instruction is executed once in the order given before moving onto the next Instructions = statements
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is Branching

A
  • Sequence is a vital programming construct, but there are many situations in which you wont want the computer to execute every instruction given.
  • There are 3 implemetations of branching commonly available in programming languages
    • IF statements
    • ELIF (ELSE IF statements)
    • ELSE (IF ELSE statements)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are IF statements

A
  • IF statements are the most basic form of branching
  • They check if a condition has been met, and if it has its value is set to TRUE and the program will set execute the set of instructions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is IF ELSE statements

A
  • Similar to IF statement but if the condition necessary is not met, the instructions within the ELSE statements are executed instead.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are ELIF (ELSE IF) statements

A
  • This combines IF and IF ELSE statements.
  • If the condition necessary to execute the instructions of IF statement, ELIF is checked
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is SELECT CASE

A
  • Another common form of selection.
  • SELECT case focuses on the content of a variable and has a separate CASE for each outcome
  • Another important part is CASE ELSE statement at the end of SELECT CASE. This will be executed if none other cases apply.
  • Using SELECT CASE avoids the need to use multiple nested IF statements - difficult to read.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is Iteration

A
  • Iteration is used to execute the same instruction a same number of times or until a condition is met.
  • Most programming languages support 2 types: WHILE and FOR loops.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are FOR loops

A
  • FOR loops execute an instruction a set number of times. This is count controlled. We create a count and set it to 0.
  • Every time the code is executed the count is increased by 1 - when count reaches value set by programmer, the computer stops executing the code
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are WHILE loops

A
  • Are used to repeat an instruction until a condition is met - known as condition controlled loops.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is Nesting

A
  • Is the name given to the practice of placing one construct within another.
  • Example IF statement inside an IF statement
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is Recursion

A
  • A recursive function is one that repeatedly calls itself until a base case is met within a function
  • Can be used to calculate factorial of a given whole number - calculated repeatedly
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the advantages of recursion over iteration?

A
  • More natural to read (1)
  • Quicker to write / less lines of code. (1)
  • As some functions are naturally recursive (1) Suited to certain problems (1)
  • For example those using trees (1)
  • Can reduce the size of a problem with each call.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are examples of data types

A
  • Char - A single character
  • String - A series of letters and numbers
  • Boolean - Either TRUE or FALSE
  • Integer - A whole number
  • Float - A decimal number
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a Variable

A
  • Identifier/name of a …
  • Memory location used to store data
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is Modularity

A
  • When a sequence of instructions is going to be repeated a numberat different points, it is useful to put them in a separate subroutine - this can be called at relevant points - code is easier to maintain, less eror prone and easy to modify.
17
Q

What are Local Variables

A
  • Defined within one module…
  • … accessible only in that module
  • Can be used as parameters
  • Data is lost at end of module
  • Can overwrite global variables (with the same name)
18
Q

What are Global Variables

A
  • Global Variables are available throughout the program to any subroutine.
  • Where in a program a variable can be accessed is called the scope. It is good practuce to limit the scope of many variables - this is to aid maintainability and code reuse.
  • Global variables should be declared at the start of the program after any imported libraries
19
Q

What is a subroutine

A
  • A subroutine is defined as a named block of code that carries out a specific task.
  • Most programming languages make use of 2 types of subroutine: functions and procedures
20
Q

What is a function

A
  • A function is a named section of program (1) that performs a specific task (1). It returns a value (1), it is often called inline (1).
21
Q

What is a procedure

A
  • Procedures are subroutines that dont return a value
  • They can display information to a user but dont return any information back to the main program.
22
Q

What is passing parameters by value

A
  • Parameters can be used to pass values into a subroutine..
  • Example procArea (x,y) replaces the word height with a copy of the value stored in x. 42 is displayed
  • Parameters should be used in preference to global variables as they can be easier to read and maintain
23
Q

What is passing parameters by reference

A
  • An alternative way of passing parameters into subroutines is to pass by reference rather than making a copy of the variable, the subroutine is passed the address of the variable .
  • The main difference is that any change made within the subroutine will also affect the variable outside the subroutine.
24
Q

What are IDEs

A
  • Integrated development environment
  • A (single) program (1) used for developing programs (1) made from a number of components (1)
  • IDEs come with a range of tools for helping programmers debug their code, including automatic error checking, error reports, breakpoints and stepping through.
25
Q

What are breakpoints

A
  • Breakpoints allow the programmer to pause the program at a specific point in its execution and look at the contents of variables to diagnose logic problems that are impossible for IDE to diagnose.
  • This will halt the execution there and help the programmer spot the problem.
26
Q

What is stepping through

A
  • Stepping through allows the programmer to place a breakpoint that pauses the execution then click “step into” to execute the next instruction in the sequence, pausing before clicking “step into” again to repeat the process.
  • Equivalent of having a breakpoint placed on every line in your program
  • Once the execution has been paused, the programmer clicks “step into” to execute the next instruction, check contents of variables and “step into”
27
Q

Exam Question: Identify and explain 3 features commonly found in IDEs that help programmers debug their code (6)

A
  • Debugging tools allow inspection of variable values
    • this can allow run-time detection of errors
  • Code can be examined as it is running
    • which allows logical errors to be pinpointed
  • IDE debugging can produce a crash dump
    • which shows the state of variables at the point where an error occurs
  • It can display stack contents
    • which show the sequencing through procedures / modules
  • It can step through code
    • which allows the programmer to watch the effects each line of code
  • The insertion of a break-point
    • allows the program to be stopped at a predetermined point in order to inspect its state
28
Q

Exam Question: Describe a difference between a local and global variable (2)

A
  • Global variable is visible throughout a program / may be accessed from more than one part of the program (1), local variable is visible only in module / construct where it is created / declared (1)
29
Q

Exam Question: Explain using an example why global variables are used (

A
  • A global variable used where a value needs to be accessible from various parts of a program
  • it is the same value
  • Irrespective of the place where it is accessed
  • Any sensible example of a value that will reasonably need to be the same, e.g. today’s date, VAT rate, pi (1 – AO 2.1).
30
Q

Exam Question: Explain why good programming practice generally avoids the use of global variables (2)

A
  • Global variables make it difficult to integrate modules (1)
  • they increase complexity of a program (1)
  • they may cause conflicts with names written by others / in other modules (1)
  • they may be changed inadvertently when program is complex (1).
31
Q

Exam Question: Explain why parameter passing is better than global variables (2)

A
  • When a parameter is passed to a function, only a copy of the data passed may be changed (1)
  • therefore no unforeseen effects will occur in other modules (1).
  • This is the case only if passed by value (1).
32
Q

Exam Question: Compare a program procedure with a function (2)

A
  • A procedure is also a sub-section of a program (that performs a specific task) (1)
  • but it does not necessarily return a value (1).
  • Most programming languages nowadays use functions (1).
33
Q

Exam Question: What are the disadvantages of using recursion over iteration

A
  • Can run out of stack space / memory (1)
  • (due to too many calls (1))
  • causing it to crash (1)
  • More difficult to trace / follow (1)
  • as each frame on the stack has its own set of variables (1)
  • Requires more memory than the equivalent iterative algorithm.
  • Usually slower than iterative methods (1)
  • due to maintainence of the stack (1)
34
Q

Exam Question: Describe 2 features of global variables that distinguish them from local variables

A
  • Defined at start of program
  • Exists throughout program / in all modules
  • Allows data to be shared by modules
35
Q

Discuss the different object oriented techniques that Barney could use

A
  • Encapsulation set and get methods to ensure that the nodes in the linked list are changed in a way that is intended.
  • A class can be used to declare the attributes and methods for the linked list. These will initialise the nodes.
  • Objects can then be used be used to instantiate the class each time a new linked list is needed.
  • Further subclasses may be used by other programs. These can therefore take on the attributes and methods from the base class. These can also be changed or overridden depending on the purpose of the other programs
35
Q

Discuss the different object oriented techniques that Barney could use

A
  • Encapsulation set and get methods to ensure that the nodes in the linked list are changed in a way that is intended.
  • A class can be used to declare the attributes and methods for the linked list. These will initialise the nodes.
  • Objects can then be used be used to instantiate the class each time a new linked list is needed.
  • Further subclasses may be used by other programs. These can therefore take on the attributes and methods from the base class. These can also be changed or overridden depending on the purpose of the other programs
36
Q

Why would using OOP techniques be good?

A
  • Code reusability
  • His linked list can be saved as library and then reused many times leading to less code.
  • OOP also allows programs to be easier to modify and maintain.