1/88
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
data type
a classification that specifies the kind of data that can be stored and manipulated within a program
intiger
Whole numbers (e.g., 5, -12, 0)
real/float
Numbers with decimal points (e.g., 3.14, -7.5)
boolean
True or False value
a character
A single symbol or letter (e.g., 'A', '7', '#')
string
A sequence of characters (e.g., "Hello", "123").
date and time
stores date and time
pointer/reference
Memory address of another variable
records
Groups related data of different types.
arrays
Ordered collections of the same data type
user defined data types
custom data structures created by combining built-in types to represent more complex data.
examples of user defined data types
classes, records, and enumerations.
variable declaration
reserves memory to store a value. eg age = 0
constant declaration
a fixed value that doesn’t change. eg pi =3.142 3.14159
assignment
sets or update the value of a variable
iteration
repeated a block of code. eg loops
selection
executes code based on conditions. eg if statements
subroutine (procedure/function)
a reusable block of code that performs a specific task. eg a def statement
what’s definite iteration
iteration where the number of repetitions is knows before. eg for I in range of 5
what is indefinite iteration
iteration where the number of repetitions depends on a condition. eg while loops
what is a loop with a condition at the start
checks the condition before executing the block. If the condition is false, the loop does not run at all. eg while x >0
what’s a post condition loop
A loop that runs at least once before checking the condition.
Differences between pre-condition and post-condition loops
Pre-condition loops may not execute at all, while post-condition loops always run at least once.
when in definite iteration most useful
When the number of repetitions is known in advance, like iterating through a list.hw
When is indefinite iteration most useful?
When the loop must continue until a specific condition is met, such as user input validation.
Why is understanding the placement of conditions important?
It helps design loops that work correctly for the intended logic, ensuring accurate execution.
nested selection
placing one structure inside another used to handle multiple layers of decision making (eg: if else elif inside a if)
nested iteration
placing one loop inside another. to handle operations like working with multi-dimensional arrays or repeated actions within repeated actions.
strength of nested selection
allows for more complex decision trees
nested iteration
great for working with multi-dimensional data or repeated sub-processes.
exponentiation
raises a number to the power of another number
tuncation
removes the decimal part of a number leaving only the integer part eg using int()
What’s the difference between a variable and a constant
Variables are flexible and can be modified, while constants are fixed values used for things that should remain constant throughout a program.
advantages of using named constants
improved readability - using descriptor names for fixed values rather than raw values
easier matinence - if you have to update a constant value you only need to change it in one place instead of whole prog
reduce errors - minimize risk of accidenltly changing value that should remain fixed
code reusability - increases consistency
better debugging - can track where it is easier
concatenation
combines two or more strings together
character -> character code
Converts a character to its ASCII (or Unicode) code. (ord)
character code → character
converts a character code(into) back to a character (chr)
exception handeling
allows a program to respond to runtime errors or unexpected situations in a controlled way without crashing
exceptions
An event that disrupts the normal flow of a program's execution.
try block
The section of code that might throw an exception.
except block
The section of code that runs if an exception occurs in the try block.
finally block
code that will run after the try-except blocks, regardless of whether an exception occurred.(optional)
benefits of exception handling
prevents program from crashing unexpectedly
allows of more detailed error messages helping debugging
you can take specific actions depending on the type of error
common uses of exception handling
handle errors like file not found and file permission issues
network communications like timeouts and unreachable servers
user inputs validation
subroutine
block of code which performs a specific task can be called and executed from different parts of the code
key characteristics of a subroutine
name
parameters - values can be passed to modify behavior
return value p result which can be returned from the subroutine back to the calling code
execution - executes it when is called in the program
advantages of subroutines
reusability
code organization
abstraction
easier for debugging + testing
parameter
variable used to pass data to and from subroutines
types of parameters
positional parameters - order of them matter
keyword parameters - passed by name not position (allows flexibility)
default parameters - have default values if no value provided when called
variable length parameters- used when you don’t know how nat will be passed on
subroutine with interfaces
defines the name, parameters, and return type of the subroutine. It is the "public" part of the subroutine that shows how it can be used in the program.
advantages of using parameters
data encapsulation - doesntt expose the internal workings of the subroutine. keeps the subroutine "self-contained" and easier to manage
flexibility - avoids hard coding values in subroutine
reusability - you can call it multiple times with different arguments making it more modular and reducing redundancy
improved readability - makes easier to understand
local varables
can only be used in that block of code or subroutine
local vs global variable
Scope:
Local Variable: Only accessible within the function/block where it is defined
Global Variable: Accessible throughout the entire program
Lifetime:
Local Variable: Exists only while the function/block is executing.
Global Variable: Exists for the entire duration of the program.
recursion
a function calls itself to solve smaller instances of the same problem. It has a base case to stop the recursive calls and prevent infinite loops.
general case in recursion
part of the recursive function where it calls itself with a simpler or smaller version of the problem, typically moving closer to the base case
base case in recursion
condition under which the recursion stops, returning a value without making further recursive calls. It prevents infinite recursion.
recursion implemented in a program
function calls itself with modified arguments.
Each call adds a new layer to the call stack until the base case is reached, at which point the function returns a result and unravels the recursive calls.
advantage of recursion
simplified code with repetitive subproblems
breaks down complex problems into smaller manageable parts
disadvantage of recursion
can lead to stack overflow
less efficient than iterative solutions
recursion diffrent iteration
Recursion: Function calls itself to solve a problem step-by-step.
Iteration: Uses loops to repeatedly execute code to solve a problem.
procedural programming paradigm
paradigm based on the concept of procedure calls, where the program is structured into procedures (or functions) that operate on data.
key characteristics of procedural programming
Linear Structure: Programs are executed step-by-step.
Modular Design: Code is divided into reusable functions or procedures.
Focus: Emphasizes functions and sequence of actions to manipulate data.
Global State: Data is often stored in global variables, accessed and modified by procedures.
OOP
organizes a program around objects rather than actions, with objects containing both data (attributes) and behavior(methods).
key characteristics
Encapsulation: Data and methods are bundled together inside objects.
Abstraction: Hides complexity by exposing only essential details.
Inheritance: Enables reuse of existing code by creating new classes based on existing ones.
Polymorphism: Allows methods or objects to behave differently based on the context.
Focus: Focuses on objects and their interactions rather than procedures.
eval for procedural prog and OOP
Procedural programming is simpler and better for smaller tasks.
Object-oriented programming is more powerful for managing larger, complex systems by organizing code around objects.
what is a structures approach
divides a program into smaller, manageable modules or functions, each with a specific purpose. It uses a top-down design method and focuses on clarity, reusability, and logical flow.
key principles; es of structured programming
Modularity: Breaking the program into smaller, independent modules.
Top-Down Design: Designing the program starting from the main task, then breaking it into sub-tasks.
Sequence, Selection, Iteration: Using these three basic structures to control program flow.
hierarchy chart
a visual diagram that shows the structure of a program. It represents the breakdown of a program into modules and sub-modules, illustrating their relationships.
advantage of structured programming
Easier Debugging: Errors are easier to isolate and fix in small, independent modules.
Improved Clarity: Programs are more readable and maintainable.
Code Reusability: Modules can be reused in other programs.
Better Collaboration: Teams can work on different modules simultaneously.
Logical Flow: Ensures a clear and systematic program flow.
structured vs unstructured programming
Structured: Uses modular design, clear control structures, and a top-down approach.
Unstructured: Relies on spaghetti code with no clear flow or modularity, making debugging difficult.
steps in structured programming
Identify the main task.
Break the task into smaller sub-tasks (modular design).
Use hierarchy charts to represent the structure.
Write and test code for each module.
modular design
dividing a program into smaller, independent modules, each responsible for a specific task. Modules interact but work independently.
when is structured useful
for large, complex programs where clarity, maintainability, and scalability are important.
what is a class in OOP
a blueprint or template that defines methods and attributes common to all objects of that type.
what’s instantiation
the process of creating an object from a class using a constructor.
what is object in OOP
an instance of a class, representing a specific entity with defined attributes and methods.
encapsulation
e principle of bundling data (attributes) and methods (functions) together and restricting access to some parts using access specifiers (e.g., private, protected, public).
inheritance
allows a class (child) to derive methods and attributes from another class (parent), promoting code reuse.
aggregation
a "has-a" relationship where one class contains a reference to another, but the contained object can exist independently (e.g., a university has departments). Represented by a white diamond in UML.
composition
"part-of" relationship where one class contains another, and the contained object cannot exist independently (e.g., a car has an engine). Represented by a black diamond in UML.
polymorphism
allows methods or objects to behave differently depending on their context, often through method overriding or overloading
what is method overriding
when a subclass provides a specific implementation of a method already defined in its parent class.
What do the symbols +, -, and # represent in UML?
+public
-private
#protected
why OOP
Modularity: Easier to maintain and debug.
Reusability: Code can be reused via inheritance and polymorphism.
Scalability: Better suited for large, complex systems.
Real-world modeling: Closely mirrors real-world entities and relationships.
what’s a abstract method
Declared in a base class but implemented in derived classes
whats a virtual method
Defined in a base class and can be overridden in derived classes.
static method
Belongs to the class rather than an instance and does not require an object to be called.
What does single inheritance look like in a class diagram?
A class diagram shows a child class inheriting from a parent class using a solid line with a hollow trianglepointing to the parent.
How are aggregation and composition represented in UML?
Aggregation: White diamond (e.g., Library ○— Book).
Composition: Black diamond (e.g., Car ◆— Engine).