Programming Techniques Flashcards
What are the four main structures used in structured programming
- Sequence - code is executed line-by-line
- Selection - a certain block of code is run if a specific condition is met
- Iteration - a block of code is executed a certain number of times or while a condition is met
- Recursion - the function calls itself from within the function
What are the Advantages and Disadvantages of Recursion
Advantges -
- Programmers can be written in fewer lines of code
- Makes code less prone to errors
Disadvantges -
- Its inefficient use of memory
- Stack overflow cam occur which is when the call stack runs out of memory. This would cause the program to crash
- Can be difficult to trace if using many function calls
What are the Differences between Iteration and Recursion
- Recursion uses more memory, Iteration uses less memory
- Recursion uses a new set of variables (previously placed on stack), iteration uses same variables
- Recursion can run out of memory, Iteration will not run out of memory
- Recursion can express a problem elegantly (less lines of code), Iteration can take up more lines of code and is harder to follow
What is meant by the term Scope
The section of code in which a variable is available
What scope does a Local Variable have
- Limited scope
- They can only be accessed within the block of code in which they were defined.
- If a local variable is defined within a subroutine, it can only be accessed within that subroutine.
- Therefore, multiple local variables with the same name can exist in different subroutines and will remain unaffected by each other
Why are using Local Variables a good way to program
It ensures subroutines are self-contained, with no danger of variables being affected by code outside of the subroutine
What is the Scope of a Global Variable
- Can be accessed across the whole program
- All variables used in the main body of a program are automatically declared to be global
- These are useful for values that need to be used by multiple parts of the program
What are the Disadvantages of using Global Variables
- Global Variables can be unintentionally overwritten and edited (Local Variables take priority)
- Global variables are not deleted until the program terminates so they require more memory than local variables which are deleted once the subroutine has been completed
What is Modular Programming
A programming technique used to split large, complex programs into smaller, self-contained modules
What is the benefit of using Modular Programming
- Makes it easier to divide tasks between a team and manage
- Simplifys the process of testing and maintenance, as each component can be dealt with individually
- Improves the reusability of components
- The Top-Down method is an example of Modular Programming
What is Stepwise Refinement
Different name from the Top-Down method
What are the different between a procedure and function
- Procedures do not have to return a value whilst functions always return a value
- Procedures can return multiple values whereas a function must return one single value
- Procedures are typically given data as parameters while functions commonly make use of local variables
What is Passing by Value and Passing by Reference
Passing by Value -
- Sends the actual value being used
- This data is held in a separate memory location and is only available to the subroutine
- Even if the value of the parameter is changed within the subroutine, the value of the original argument is unchanged
Passing by Reference -
- This creates a pointer that contains the memory address of the original variable
- Therefore, any change to the parameter within the subroutine also affects the value of the original variable
What is an IDE (Integrated Development Enviroment)
A program which provides a set of tools to make it easier for programmers to write, develop and debug code
What are some examples of IDE tools
- Stepping
- Variable watch
- Breakpoint
- Source code editor
- Debugging tools
What is Stepping
This allows you to monitor the effect of each individual line of code by executing a single line at a time
What is Variable Watch
Sometimes used to pinpoint errors, this is a useful feature to observe how the contents of a variable change in real-time through the execution of a program
What is a Breakpoint
- IDEs allow users to set a point in the program at which the program will stop
- This can either be based on a condition or set to occur at a specific line
- This can help to pinpoint where an error is occurring.
What is a Source Code Editor
The editor aims to make the coding process easier by providing features such as autocompletion of words, syntax highlighting and automatic bracket completion
What is a Debugger Tool
When the IDEs provides a run-time detection of errors with a guide as to where in the code they are likely to have occurred through line numbers and highlighting
What is an Object-Oriented Paradigm
- Based on objects formed from classes which have attributes and methods
- Focuses on making programs that are reusable and easy to update and maintain
- Suited to problems which can be broken into reusable components with similar characteristics
What is a class
A template for an object which defines the attributes and methods of the object
What are the Components of Object-Oriated Languages
- State - given by attributes which give an object’s properties
- Behaviour - defined by the methods, which describe the actions it can perform
- Classes - templet for objects
- Object - a particular instance of a class
- Setter - a method that sets the value of a particular attribute
- Getter - a special method used in object-oriated langauges which retrieves the value of a given attribute
- Constructor - a method which allows a new object to be created
What is the advantage of using Getters and Setters in object-oriented programming
- Getters and setters ensure attributes cannot be directly accessed and edited
- Attributes can only be accessed by public methods
- This is called encapsulation
What are the two properties of an object-oriented language
Inheritance
Polymorphism
What is Inheritance and Polymorphism
- Inheritance - a class can inherit from another class The subclass will possess all of the methods and attributes of the superclass (or parent class) and can have its own additional properties. This allows programmers to effectively reuse certain components and properties while making some changes
- Polymorphism - objects can behave differently depending on their class. This can result in the same method producing different outputs depending on the object involved.
What are the two different categories of Polymorphism
- Overriding - when a method is redefined within a subclass so that it functions differently and produces a different output
- Overloading - passing in different parameters into a method to produce a different output