SLR 23 - PROGRAMMING TECHNIQUES Flashcards

1
Q

What are the 3 programming constructs?

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

Give an example of each programming construct

A
  • sequence:
    a = 5
    b = 6
    c = a x b
    print(c x 2)
  • selection:
    a = 5
    b = 6
    if a > 0:
    print(a x b)
    else:
    print(“a = 0”)
  • iteration:
    a = 5
    while a > 0:
    a = a -1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is recursion?

A

a programming subroutine where a section of code calls itself until a base case is met

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

What are the 3 conditions for a recursive statement?

A
  • contains a stopping condition (base case)
  • for any input value other than the stopping condition, the subroutine should call itself (recursion)
  • the stopping condition should be reachable within a finite number of times
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Which is more efficient in terms of memory usage (recursion/iteration) and why?

A
  • iteration
  • recursion is not very memory-efficient. Every time a recursive function calls itself, the processor needs to remember where it was before it jumps to the new copy so it knows where to return later​
  • the processor also needs to remember the values of all the previous variables, as they are local to those copies of the recursive function – this is done using stacks, which takes up space in memory
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a global variable?

A

a variable that is:
- “typically” declared at at the top of a program outside of any subroutine
- accessible throughout the program
- created when the program starts
- destroyed when the program ends

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

What is a local variable?

A

a variable that is:
- declared inside a subroutine
- only accessible by that subroutine
- created when the subroutine is called
- destroyed when the subroutine ends

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

Why is use of global variables generally considered poor programming practice?

A

excessive, unnecessary use of global variables can make programs hard to test, debug and maintain

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

What is modularity?

A
  • the concept of breaking down a large program/problem down into smaller chunks
  • the goal is to design a program in such a way that each module carries out a single, specific task
  • modules may be referred to by different names:​
    • subroutines​
    • procedures​
    • functions​
    • methods​
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a procedure?

A

a subroutine that takes in zero, one or more parameters and performs a set task

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

What is a function?

A

a subroutine that takes in zero, one or more parameters and performs a set task and returns a value

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

What are the 2 methods of passing data into a subroutine?

A
  • passing by value (byVal)
  • passing by reference (byRef)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does passing a parameter by reference mean?

A

passing the address or pointer of the required value into a procedure

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

What does passing a parameter by value mean?

A

creating a temporary local copy of the actual value of a variable and passing it into the procedure

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