Programming techniques Flashcards

(52 cards)

1
Q

what are some common programming constructs used in structured programming?

A
  • sequence
  • iteration
  • branching
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

define “sequence”

A

code is executed line by line, from top to bottom

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what is selection?

A

when a question is asked which has a Boolean answer, and depending on the answer the program takes 1 of 2 courses of action, after which the program moves onto the next event
ie. an if… then… else… statement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

when are nested if statements used and what do they look like?

A

used when it isn’t enough to ask one question:

if….
….
if…..
…..

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what are case statements?

A

use these to ask lots of questions (in Python this is an elif statement)

Example:

case k of:
1: print(“gold”)
2: print(“silver”)
3: print(“bronze”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what is iteration?

A

a process wherein a set of instructions or structures are repeated in a sequence a specified number of times or until a condition is met

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what are the 2 major groups of loops?

A
  • count-controlled
  • condition-controlled
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what is a count controlled loop?

A

a loop which repeats the instructions within it until the counter reaches a certain value
eg. a for… next loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what is a condition controlled loop?

A

a loop which repeats the instructions within until a condition is met (this may still be a value)
eg. a while… loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what is a post test iteration? give an example

A

tests the condition at the end of the sequence so will always be run once

example:

repeat
print “hello world”
x = x+1
until x = 10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what is a pre test iteration?

A

condition is checked before entry into the iteration so if the condition isn’t met, it won’t run

Example:

while x < 10:
print “hello world”
x = x+1
endwhile

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what is branching?

A

when control is passed to a different sequence of instructions, rather than completing the next line
eg. in assembly: BRA, BRZ, BRP
used for functional + procedural calls

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

how does branching work?

A

uses a stack to keep track of each branch call, so that it can retrace its steps back to the main program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what is a function?

A

a subprogram that can be called from elsewhere in the program - interrupting the natural sequence of the program
returns a value to the main program
can have parameters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what is a procedure?

A

almost the same as a function but doesn’t return a value on completion

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

what is a library?

A

a collection of functions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

what are the basic rules that apply to loop structures?

A
  • must have a terminating condition
  • must have a sequence of code that achieves a task
  • can only be nested within themselves
  • cannot overlap - must be wholly contained within another
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

what are some advantages of using the case structure over nested if statements?

A
  • easier to read - structured nature makes clear which statement is being checked and what actions taken based on conditions
  • more efficient
  • code is more organised + easier to maintain
  • handles complex logic better than nested if, especially if there are many cases to consider
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

what are some advantages of using nested if statements over the case structure?

A
  • more versatile
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

what is a recursive function?

A
  • a function that calls itself
  • continues to call itself until stopping condition is reached
  • hypothetically easier to program - less code = less to debug
  • every recursive can be written as iterative
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

how does recursion work?

A
  • each time the function calls itself a new stack frame is created within call stack - stores parameters, local variables + return addresses
  • finite no of stack frames are created until base case/stopping condition is reached
  • recursions are then resolved and stacks created are removed
22
Q

what is a stack overflow error?

A

call stack running out of memory to contain stack frames created during recursion

23
Q

what are the advantages of recursion?

A
  • functions using it can be represented using fewer lines of code - less prone to errors
  • but only good if base case is clearly defined so stopping condition is reached after finite number of function calls
24
Q

what are the disadvantages of recursion?

A
  • inefficient use of memory - creating stack is memory intensive
  • stack overflow would cause the program to crash
  • difficult to trace esp with more function calls
    (also i just really hate it i dont get it)
25
what is a scope?
the section of code in which a variable is available
26
what is a local variable?
a variable declared inside a subroutine + can only be accessed by the subroutine + data contained in it is destroyed at the end of the subroutine takes precedence over a global variable with the same name
27
why are local variables good?
- variables with the same name can be used in different subroutines and be unaffected by each other - subroutines are kept self-contained - variables can't be affected by code outside the subroutine
28
what is a global variable?
a variable that is defined at the start of a program + exists throughout including in functions + procedures - allows data to be shared between modules overriden by local variables with the same name
29
why is using global variables not recommended?
- can be unintentionally overwritten/edited - requires more memory than local variables - it is only deleted when the program terminates, not as soon as the subroutine is completed
30
what are the 2 methods of parameter passing?
- passing by value - passing by reference
31
what is parameter passing by value?
the variable being used to transfer the value to the procedure does not get altered itself, only a copy of its value is passed + discarded at the end of the subroutine - kind of treated like a local variable
32
what is parameter passing by reference?
the variable being used to pass the parameter gets referenced and therefore irrevocably altered by the procedure address of parameter is given to subroutine -> value of parameter is updated here
33
are functions passed by value or reference in an exam by default? what is the notation if its something different?
- by default it will be passed by value unless specified - the format is: function multiply(x:byVal, y:byRef)
34
what is modularity?
the process of breaking down a problem into component tasks, so that each module carries out a specific task
35
what are the advantages of modularity?
- breaks the problem down into manageable chunks - can divide tasks amongst a team - tasks can be achieved concurrently - maintenance is much easier - modules can be reused easier (libraries, functions, procedures) - modules are pretested
36
what is an IDE and what is it used for?
it is an Integrated Development Environment - provides a set of tools to make it easier for programmers to write, develop + debug code
37
what are some tools available in an IDE to identify logic errors?
- setting breakpoints - setting a watch on a variable - step through the program
38
what is the breakpoint feature of an IDE?
- This allows the user to set a point in the program at which it will stop - can be a line or a condition - you can see whether the program executes correctly up to that line - helps pinpoint the error
39
what is the variable watch feature of an IDE?
displays the value of a variable each time it changes so can observe how it changes in real time through the execution of the program - useful to pinpoint errors
40
what is stepping through the program using an IDE?
this allows you to monitor the effect of each individual line of code by executing a single line at a time
41
what is the source code editor?
aims to make the coding process easier by providing features such as autocompletion of words, indentation, syntax highlighting and automatic bracket completion
42
what is another debugging tool provided by the IDE?
run-time detection of errors - provides a guide to where in the code the error might be through line numbers and highlighting
43
what is the purpose of testing?
to try and uncover undetected errors
44
how should a program work if its been done perfectly?
- should work correctly whatever data is inputted - valid data = everything is smooth - invalid data = detect + report this + ask user to enter valid data - some valid data might cause the program to crash if you haven't allowed certain values
45
what are the 3 types of test data?
- normal data - data within the range that you would expect + of the data type you would expect - boundary data - data at the ends of the expected range - test these to make sure these give the expected results - erroneous data - data that is either just outside the expected data range or is of the wrong data type
46
what is a class in object-oriented programming?
a template for an object which defines the state + behaviour of an object
47
what is the state of an object in OOP?
it is given by attributes which give an objects properties
48
how are behaviours of an object defined in OOP?
they are defined by the methods associated with the class which describes the actions it can perform
49
what is instantiation?
the process by which classes can be used to create objects
50
what is an object?
a particular instance of a class - there can be multiple instances of a class with the same set of attributes + methods
51
what is encapsulation?
attributes are declared as private so can only be altered by public methods used to implement the principle of information hiding
52
what is the principle of information hiding?
when programs are made less complex by protecting data from being accidentally edited by other parts of the program