Problem solving and programming Flashcards
Give a subsection of procedural programming
Structured programming
Sequence is a type of programming construct
What does it do?
Code is run line by line
Branching is a type of programming construct
What does it do?
Branching/selection
-Certain block of code is run if a SPECIFIC condition is met
(IF statements)
Iteration is a type of programming construct
What does it do?
Code is executed a certain number of times (countcontrolled) or while a condition is met (condition controlled)
Recursion is a programming construct
TRUE OR FALSE
TRUE
Give advantages of using recursion
-Problem can be solved with fewer lines of code
(perhaps making it less prone to errors)
What is essential for recursive subroutines?
They need to be clearly defined so that a base case is reached after a finite number of function calls
What does a stack frame contain?
-Parameters
-Local variables
-Return addresses
This info allows the subroutine to return to that particular point when that subroutine call is finished
Give disadvantages of recursion
-Stack overflow(call stack runs out of memory) if the base case isnt reached
-Extra function call overhead. Each recursive call involves storing the state of the function, including parameters and local variables, which consumes time and memory
-Can be hard to trace (specially when there is loads of function calls in a program) because the program’s flow involves multiple function calls.
-An iterative solution is easier to understand for humans
-Need well defined base case or could enter an infinite recursion
Recursive subroutines need to be clearly defined so that a base case is reached after a finite number of function calls
Can lead to stack overflow (call stack runs out of memory)
Why is tail recursion good?
Form of recursion which is implemented in a more efficient way by not requiring additional stack frames. (so avoids stack overflow)
Why are local variables considered good programming practices?
They have limited scope meaning they can only be accessed within the block of code they are defined in:
Ensures that subroutines are self contained so no code outside the subroutine will affect these variables
What is the difference between local and global variables?
Local= limited scope= can only be accessed within the block of code they are defined in
-Require less memory as they are deleted once subroutine terminates
Global= can be accessed accross whole program
-More memory is requeired as they are deleted once whole program terminates
In the event of a local variable within a subroutine with the same name as a global variable, the global variable will take precedence
TRUE OR FALSE
FALSE
Local variable will take precendence
Top down approach in Modular programming
A technique used to modularise programs
-The program is continually split into subproblems until each can be represented as an individual, selfcontained which performs a specific task
Subtasks are organised in a hierarchy from the most abstract (high-level) to the most detailed (low-level).
What is modular programming?
A programming technique used to split large and complex programs into smaller self contained modules
Give advantages of modular programming
-Makes code easier to understand
-Can divide a problem between a team as each component can be delt with individually and independently from the rest
-Once tested, components can be reused
-Easier to test (as it can be tested indepently of other components)
-Parts of program can be developed in parallel (so it takes less time for program to be delivered)
Classes can be used to create objects
What is this called?
Instantiation
What is meant by instantiation?
When an object from a class is created
An object can also be called…
an instance of a class
In OPP, how are attributes declared as private altered?
Using public methods
What is meant by encapsulation?
The process of combining attributes and methods that operate on data into a class. Protecting the integrity of data using access modifiers
What is a public access modifier?
Public:
Accessible from anywhere (inside and outside the class).
What is a private access modifier?
Private:
Accessible only within the class.
Used to protect data from being directly accessed or modified.