Programming practice Flashcards
(24 cards)
What are the 4 main data types used in programming?
- String
- Boolean
- Integer
- Real (decimals)
Note that the syllabus also includes:
* Date and time - these are handled by dedicated libraries.
* Single precision floating point - this is a specific way to represent real numbers.
What are the 3 types of errors in programming?
- Syntax error - using incorrect instructions and formatting.
- Runtime error - occur during execution and results from an illegal operation
- Logic error - a result of code that does not correctly solve the problem.
How do most computers internally represent time?
As the number of seconds since the Unix epoch (1/1/1970).
You don’t need to know the exact date of the epoch.
What are the 5 flowchart symbols?
- Terminator (begin/end) - rounded rectangle
- Process - rectangle
- Decision - diamond
- Input/output - parallelogram
- Subroutine - rectangle with tracks (“doubles tennis court”)
Identify two reasons why pseudocode and flowcharts might be used to represent algorithms instead of simply coding the solution.
- The algorithm might be easier to understand since pseudocode rules are not as strict as programming languages, and flowcharts show a more visual representation of program flow.
- The algorithm might need to be implemented in different languages, or the developer might not know ahead of time which language it is going to be implemented in.
What are the 4 control structures, used to control program flow.
- Sequence
- Decision (branching)
- Looping (iteration)
- Subroutines (functions)
What is the main way that control structures in pseudocode differ from their implementation in Python?
Both pseudocode and Python use similar keywords for many control structures (e.g. if
, else
and while
). However, pseudocode control structures come in pairs (opening and closing) while Python relies on indentation to indicate the end of a control structure.
How are binary decisions represented in pseudocode and flowcharts?
Using keywords IF, ELSE and ENDIF
What are the two ways that multiway decisions are represented in pseudocode and flowcharts?
- Using IF, ELSE IFs, and ENDIF
- Using CASEWHERE and END CASE.
What are the two types of conditional loops in pseudocode and flowcharts?
- Pre-test loop (WHILE… ENDWHILE)
- Post-test loop (REPEAT… UNTIL)
How do counting loops work in pseudocode and how can they be implemented using flowcharts?
Using the for
keyword, but not in the same way as in Python.
What is a subroutine?
A named block of code that performs a particular operation. Might also be referred to as a subprogram, procedure or function.
Subroutines might use:
* parameters - information passed to the subroutine.
* return values - information passed back to the calling code.
Why are subroutines used in code?
- For re-use: a subroutine can be called multiple times, often by different parts of the program.
- For more modular code: assisting with testing and maintaining.
Compare and contrast arrays and hash maps (lists and dictionaries in Python)
Arrays
* Items are accessed/modified using numerical indexes.
* In pseudocode, round brackets are used with indexes starting at 1.
Hash maps
* Each item is accessed/modified with a key.
In Python, arrays are implemented with the list data type and records with the dictionary data data type.
Write a subroutine in pseudocode to print out all items in an array called Names
.
This highlights that a counting loop should be used (or a while loop with extra steps), that indexes start at 1, and that the length of an array is not found with a dedicated function like len() in Python.
Write a subroutine in pseudocode to print out all values in a hash map.
Trick question! There is no way to do this in pseudocode, you need to know each individual key to access the corresponding value (using “dot-notation”).
What is a desk check?
A manual process to track the values of variables and output through the execution of an algorithm. It is set out as a table with a column for each variable. Each time a variable’s value is modified, its new value is updated in the table.
Conditions and their results are also optional columns in the table and may assist in understanding algorithms with complex conditions (e.g. in loops).
What are boundary values?
Boundary values are on the limit of comparisons in code. It is essential to include boundary values when testing algorithms.
What is path coverage?
Refers to choosing test data that ensure that all pathways through an algorithm are followed.
What test data should be chosen when testing algorithms?
- Above and below boundary values
- Boundary values
- Unexpected inputs
What is recursion?
A programming technique where a function calls itself. It is often used in backtracking and divide-and-conquer approaches, and is a key component in functional programming.
What is backtracking?
A problem solving technique that involves systematically exploring different options to solve a problem.
Two examples investigated in class were the N-queens problem and the Sudoku puzzle.
What is the divide and conquer approach?
A problem solving approach that consists of the following 3 stages:
1. Progressively breaks down a complex problem into smaller, more manageable subproblems.
2. Solves each of these subproblems independently.
3. Combines the subsolutions to obtain the final solution.
In class, we looked at the Merge sort algorithm as an example of a divide and conquer approach.
What are the key aspects of each of the 4 programming paradigms covered in class?
- Imperative - variables and functions are separate.
- Object-oriented - functionality and data are combined into objects.
- Functional - Only pure functions are used (no side effects), new variables can be created but not changed.
- Logic - data is represented as facts and rules using formal logic which can then be queried.