124-introduction-to-programming Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Programming Paradigms

A
  • way of thinking. Different approaches to using a
    programming language to solve a problem.
  • Different programming languages have similar underlying concepts.
  • The paradigm used depends on the type of problem that needs solving.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why do we need different programming paradigms

A

-different paradigms better suited for different problem types
-allows to choose the most appropriate one to create efficient and effective code
- each has its own strengths and weaknesses
-different levels of abstraction.
-different trade-offs between readability, maintainability, and performance
- some languages support only one paradigm e.g Small Basic, some support multiple e.g Python

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

Using multiple paradigms

A
  • can lead more robust and versatile code - by combining them, developers can take the advantage of each one, improving readability, maintainability, and reusability.
  • using multiple paradigms helps reduce the language barrier between different systems and applications - easier to work together
  • each paradigm can be used in multiple programming languages, so developers can communicate more effectively and collaborate on projects that use different languages
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Declarative Language

A
  • focus on stating the desired result, rather than the exact series of instructions to get to the result.
  • The programming language determines how to obtain the result, and the details are abstracted from the user.
  • can be split into functional and logic programming
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Functional programming

A
  • Functions form the core of the program
  • Code consisting of function calls combined with each other
  • Closely linked to mathematics
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Logic programming

A

-uses code to define facts and rules based on the problem, with queries used to find answers.

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

Imperative Languages:

A
  • Uses statements to execute in a specific sequence, conditionally, or repeatedly, altering the program’s state.
  • code specifies actions clearly, directing what to do, how to operate, and in what order.
  • Focuses on giving specific instructions step-by-step code development to achieve the desired result.
  • can be split into procedural and object-oriented approaches.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Procedural Programming:

A
  • uses predefined and well-structured subroutines to build a program
  • by specifying all steps that a computer must take to reach a desired state or output
  • procedures or functions are implemented on data and variables to perform a task
  • characteristics include instructions, sequence, selection, iteration, procedures, functions
  • follow a systematic order of statements, functions and procedures to solve a problem or complete a task.
  • can be applied to a variety of problems and are easy to write and interpret.
  • program instructions are executed step-by-step.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Object-Oriented Programming:

A
  • develops a solution which consists of objects which have been modelled on the real world, objects then interact with eachother
  • characteristics: instantiation, encapsulation, inheritance, polymorphism, methods, attributes
    -focuses on a modular approach using objects, classes, and encapsulation
  • involve solutions being constructed by means of objects that interact with each other
  • Applicable to certain types of problems with lots of reusable components
  • Built on entities (objects formed from classes) which have attributes and methods
  • Focuses on making programs that are reusable and easy to update and maintain
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Comparison of Procedural vs Object-Oriented Paradigms:

A
  • OOP uses classes, methods, and instances for program structure, while procedural programming uses procedures and functions.
  • Both use sequence, selection, and iteration.
  • OOP has inheritance allowing the creation of new classes based on existing classes, while procedural programming does not but can use libaries or modules to reuse code.
  • OOP uses encapsulation to store data in attributes and conceal it from other parts of the program, while procedural programming uses local variables inside subroutines, or global variables that can be accessed from any part of the program.
    Procedural programming is relatively easy to write and interpret, while OOP is useful for reusable components with similar characteristics.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

High level languages

A
  • Designed to be easier for humans to use and are more abstracted from hardware than low-level languages.
  • Have built-in functions and libraries that allow for complex operations without understanding computer hardware.
  • 2 main categories of high-level languages: Imperative and Declarative.
  • Examples include Python, Java, C++, and Ruby.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

pros of High level language

A
  • Easy to learn and read: simpler syntax and closer to natural language, beginners. Intuitive
  • Portability: can be compiled or interpreted into machine code for execution on different systems. Can be recompiled for different architectures
  • come in variety if paradigms, programmer can choose according to their preference
  • Efficient coding: higher-level abstractions and functions, classes, and libraries, allows programmers to write more complex programs in less time - developed faster, and easier to find programmers who specialise in high-level languages
  • Less prone to errors: designed to be user-friendly, easier for programmers to write code with fewer errors + easier to debug / maintain / improve
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

cons of high level languages

A
  • slower execution time - requires translation, not suited for real-time applications w/ speed and precision.
  • less control over hardware, cannot communicate directly with hardware, limited access to low-level functionality- abstracts hardware details, so challenging to control hardware + certain hardware or system functions require low-level programming knowledge
  • not memory efficient, requires more memory resources to run
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Assembly language

A
  • low level language
  • each assembly mnemonic corresponds to one machine code instruction
  • each mnemonic represents an opcode and an operand
  • used in embedded systems to interact directly with hardware
  • must be translated by an assembler before it can be executed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Pros of assembly language

A

-requires less memory and same execution efficiency as machine code. useful in device drivers when fast execution is required
-complete control of the code, individual statements can manipulate specific hardware components and can work directly on memory locations
- direct control of hardware and how memory is used via addressing mode
- provides direct access to system-level features w/o having to go through an abstraction layer provided by a software interface (e.g library/OS/framework - e.g open( ) for file system abstraction)
- easier to optimise and is optimised for a specific architecture
- programmers can choose the exact instructions so can write code that is highly efficient
- can make use of special machine-dependent instructions e.g. in the instruction set for the chip.
- best suited for when compilers or interpreters don’t exist for target CpU, & embedded systems highest possible performance is critical as memory limited

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

cons of assembly language

A
  • tedious to write and very prone to bugs - larger code/more lines to perform same task of high few lines. and programmer has to manage memory and hardware resources directly
  • difficult to understand and debug, making it hard to modify and maintain - no high-level abstraction to help identify errors
  • machine-dependent and code is difficult to port, specific to a particular processor architecture and os
  • programmers who can write efficient assembly code are rare as difficult to learn
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Little man computing

A
  • conceptual computer used in education theory to teach assembly language programming.
  • 11 simple instructions (assembly mnemonics) in its instruction set
  • abbreviations used to represent machine code instructions: ADD, SUB, STA, LDA, BRA, BRZ, BRP, INP, OUT, HLT, and DAT.
  • uses label names as substitution for addresses in code.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Little Man Computing Instructions/Assembly Mneumonics

A

0 - HLT - Stop program
1 - ADD - Add the contents of the memory address specified to the accumulator
2 - SUB - Subtract the contents of the memory address specified from the accumulator
3 - STA/STO - Store the value currently stored in the accumulator in the memory address specified
4 - Unused instruction that generates an error message
5 - LDA - Load the contents of the memory address into the accumulator
6 - BRA - jump always to the designated memory address/point in program
7 - BRZ - jump to the designated memory address/point in program if the accumulator is zero
8 - BRP - jump to the designated memory address/point in program if the accumulator is zero or positive
9 - INP - allows the user to input a value which will be held in the accumulator
10 - OUT - prints the value held in the accumulator
11 - DAT - reserves the memory address followed by DAT for storing data when the instruction is compiled

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

Little Man Computing example (uses branching and looping, end and loop are labels made myself)

A

inp
sta x
loop lda x
out
sub one
sta x
brz end
bra loop
end out x
hlt
x dat
one dat 1

20
Q

Modes of Addressing Memory

A
  • part of the opcode and specifies how the operand should be interpreted.
  • used to address locations in main memory where information is stored during a computer’s operation.
  • Immediate, Direct, Indirect, and Indexed are examples of addressing modes that can be used.
21
Q

Immediate addressing

A
  • The operand itself contains the data to be used by the operator.
  • “ADD 45,” 45 is just a number and does not refer to any memory location. and so would add 45 to the accumulator
  • Does not require accessing memory once operand is called. Simplest and fastest
    -requires data to be used directly in instruction which can be error-prone and time consuming
  • used to add constants like pi
22
Q

Direct addressing

A
  • operand refers to a memory location in which where the data to be used is stored
  • “ADD 45” refers to memory location 45, which contains the number 342 that is added to the accumulator.
  • fast but requires the same memory locations to be available every time the program is run, which is not always possible as might want to relocate program to different computers.
  • number of addresses limited by the size of the address field.
    -in LMC and used for accessing specific memory locations or variables in a program
23
Q

Indirect addressing

A
  • uses the address field as a vector or pointer to access the memory location to be used by the operator.
  • “ADD 45” would look first in memory address 45, which contains address 23 where the value to be used is stored.
  • increases the range of addresses to be accessed and used, increasing the flexibility of the program.
  • need to fetch addresses twice
    -often used to access library routines
24
Q

Indexed addressing

A
  • adds a number from the index register to the address in the instruction to modify the address used by the operator.
  • if the array is relocated, only the base address needs to change and the index numbers can stay the same.
  • requires a base address, which is the starting point of the data chunk, and an offset stored in the index register which increments/decrements
  • Increased memory usage and slower execution time
  • used for accessing data stored contiguously in memory, like arrays.
25
Q

Class

A
  • type definition/template for creating objects
  • defines their attributes (state) and behaviours (methods).
  • Ensures all data about a single instance of an object remains tied to that instance and not spread all round your program
  • can create multiple instances of object, easy code reuse, classes can be used across multiple projects
26
Q

Object

A

An instance of a class that has state and behaviour.
Behaviour - defined by its methods.
state -The values of an object’s attributes, which define its current condition.

27
Q

Attributes

A
  • data value (characteristic/property) stored in a variable associated with an object
  • describe the state of an object
  • Public attributes can be accessed from outside, while private ones are only accessed from within.
  • private attributes that can only be accessed or modified by methods of the same object.
28
Q

Methods

A
  • subroutines that form the actions that an object can perform.
  • used to perform specific tasks or operations on the object or class.
29
Q

declaring methods and attributes

A
  • Attributes are normally declared as private. Methods (procedures) are generally declared as public
  • to ensure that attributes cannot be changed except by executing a method
  • When an attribute is made private (so it can’t be directly accessed or changed from outside the class)
  • Public methods are used to read/amend the attribute’s value
30
Q

Common types of methods in OOP

A
  • constructor, getters, setters are methods
    others include…
  • Instance methods: regular methods that can be called on an instance of a class, and operate on the data of that instance.
  • Class methods: methods that operate on the class as a whole, rather than on a specific instance of the class.
  • Static methods: methods that are associated with a class, but do not operate on the class itself or any of its instances.
31
Q

Constructor

A
  • Constructors: special methods used to create and initialize objects when they are first created.
    used to create an instance of an object of an class
32
Q

Getters and Setters

A
  • Accessor methods (getters): used to access(retrieve) the values of an object’s attributes.
  • Mutator methods (setters): used to modify the values of an object’s attributes or sets the value of a particular attribute
    ^ can control how the attributes are accessed and modified, which can help with encapsulation and prevent unintended modifications to the object’s state.
33
Q

Public Methods and Private Methods

A
  • Public methods are accessible to any code that can access the object. They can be called from outside the object and are often used to provide controlled access to an object’s state through encapsulation.
  • Private methods can only be accessed within the object or class that defines them. They are often used to implement internal functionality of an object or to enforce encapsulation by preventing direct access to an object’s state.
  • The use of public and private methods can help to control the behavior of an object and to ensure that its state is accessed and modified in a safe and controlled way.
  • In OOP, a class’s methods are often classified as either public or private depending on whether they are intended to be used by external code or are internal implementation details.
  • The visibility of a method can be controlled using access modifiers such as public, private, and protected.
34
Q

Top-Down Design

A

A design principle that implements encapsulation by making each module (e.g. class) self-contained and protecting its internal data from being accidentally edited by other parts of the program.

35
Q

Instantiation

A

The process of creating an object from a class template.

36
Q

Encapsulation

A
  • practice of bundling data (attributes) with the methods that operate on them, and restricting direct access to the data from outside the object.
  • objects only interact in the way intended and prevents unexpected changed to attributes having unforeseen consequences. This means there are likely to be fewer issues as the team combines their code.
  • allows programmer to control how the object’s state is modified, and prevents accidental changes or errors.
37
Q

Polymorphism

A
  • Being able to allow methods in a subclass to override methods of the same name from their inheritance
    allows different objects to respond to the same message or method call in different ways, based on their individual attributes and behaviours. This allows for more flexible and extensible code.
    that means objects can
    behave differently depending on their class.
  • This allows the same method to be applied to objects of different classes. The code written is able to handle different objects in the same way which reduces the volume of code produced
38
Q

overriding and overloading

A

Overriding is redefining a method within a subclass and altering the code so that it
functions differently and produces a different output.
Overloading is passing in different parameters into a method.

39
Q

Inheritance

A
  • when a subclass takes on the attributes and methods from a parent (super) class.
  • can be used to create more specialized classes that share common attributes and behaviours; one class can be coded and used as the base for similar objects, saves time
40
Q

Pros of OOP

A
  • High level of reusability through inheritance and polymorphism - useful for projects where there are multiple components with
    similar properties
  • Encapsulation protects attributes from direct access and increases code reliability
  • Modular structure makes maintenance and updates easier
  • Code for different classes can be produced independently
  • Allows for high level of abstraction, saving time and effort
41
Q

Cons of OOP

A
  • Requires alternative style of thinking, can be difficult to learn for some programmers
  • Not suited to all types of problems and may result in longer, more inefficient programs
  • Considered a bloated system that can be difficult to maintain, especially with large projects
  • Giving way to functional programming, which produces safer, more stable, and easier to maintain code
42
Q

inheritance pseudocode

A

class Pet
private name
public procedure new(pName)
name=pName
colour=pColour
endprocedure
end class
class Dog inherits Pet
private breed
public procedure new(pName, pBreed)
super.new(pName, pColour)
breed=pBreed
endprocedure
endclass

43
Q

Creating a instance of an object (instantiation) pseudocode

A

declaredname = new classname(parameters)
e.g myDog = new Dog(“Fido”,”Scottish Terrier”)

44
Q

Defining a class, and constructor method pseudocode -
constructor: public procedure new(parameters)

A

Class dog
Private name
Prviate colour
Public procedure new(pName, pColour)
Name = pName
Colour =pColour
End procedure
Public procedure bark(barkTimes)
For n = 1 to barktimes
Print(“Woof!”)
Next n
Endprocedure
Endclass

45
Q

setter pseudocode

A

public procedure SetName(pName)
name = pName

46
Q

getter pseudocode

A

public function getName()
return name
end function

47
Q

polymorphism overriding pseudocode

A

class Animal
public procedure make_sound()
print(“Generic animal sound”)
end procedure
end class

class Dog inherits Animal
public procedure make_sound()
print(“Woof”)
end procedure
end class