Week 2 Imperative Language Flashcards

(18 cards)

1
Q

Variables (properties)

A

Name: String of characters
Type: (data type) represents the range of values that are allowed and the operations available
Address: memory address to which the variable is associated (l-value)
Value: contents of the memory location associated with the variable (r value)
Lifetime: The time in which the variable is allocated a specific memory location

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

Lifetime

A

time variable associated with specific memory address

in short- time between memory allocation and deallocation

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

Untyped

A

There is no type checking at all and hence the language enforces no type constraints

something like “hello” + 1 would pass without failure

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

Statically typed

A

Type checked before execution (at compile time ) to ensure type constraints are met

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

Dynamically typed

A

Type checked at runtime to ensure type constraints are met

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

Static Variable (lifetime)

A

. memory allocated before program execution (at compile time) and deallocated after program finishes executing
. memory reserved with that value for program duration -> cannot reuse storage

Advantage-
Effcient - memory addressing resolved at compile time
No allocation / deallocation at runtime

disadvantage:
cant reuse storage , not suitable for recursion which requires dynamic variables

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

Stack dynamic variable (lifetime)

eg local variables of recursive procedure

A

memory allocated when declaration of variable processed at run time

vital extra info ( but main bit was at top)
memory deallocated when function that declared them finishes execution
each function call gets own memory (so local variables inside it doesnt interfere with other function calls)
This why recursion works -> each function call gets fresh memory for its variables allowing states to be tracked independently

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

Why does recursion require stack dynamic variables (why cant we use static variables)

A

Number of calls is unknown in advance so we cant assign memory in advance (at compile time) because we dont know how many calls aka how much memory will be required
Furthermore static variables persists across function calls so it would lead to interference

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

Explicit heap dynamic variables (lifetime)

A

(nameless) memory cells allocated and deallocated by program instructions using pointers

eg :
int * intnode = new int // memory allocated on heap
.
.
.
In languages like c++ we must manually deallocate when out of scope (using delete keyword to prevent memory leaks)

eg: delete int node

Java -> objects are explict heap dynamic -> reference variables used to access them -> automatic dealloacation when object goes out of scope by java’s implicit garbage collection

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

implicit heap dynamic (lifetime)

A

memory allocated when variable assigned value

(IMPORTANT NOTE VARIABLES CREATED AT RUNTIME WITHOUT EXPLICIT DECLARATION) important to understand for advantages and disadvantages

advantages:
flexibility( variable types can change during runtime for variable - no explicit declaration and hence type)

disadvantage:
difficult error detection - variables created at runtime without explicit declaration so something like a typo program assumes its just a new variable you are creating and not an error

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

Scope

A

Range of Instructions in which variable visible

local - variable declared in specific block of code we are looking at

global - variable declared outside specific block of code we are looking at but it is still visible

languages usually have either:
static scoping rules
dynamic scoping rules

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

Static scope

A

scope determined at compile time
when variable referenced compiler looks for declaration in same block and if not there looks in parent block and …
if no declaration found compiler detects error

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

Dynamic scope

A

determined by the calling sequence of subprograms

when function references a variable first you look at the local variables and if declaration not there you look at function that called it -> if not there look at function that called that … until you find a matching declaration

Disadvantage:
No static type checking - as the actual variable being reference ( which we know the type from ) is determined at runtime

. program becomes hard to understand

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

Just read

A

Programs in imperative are sequences of instructions executed in order UNLESS CONTROL STATEMENTS used

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

What are the 3 control statements?

A

Selection construct( if else)
Looping construct ( while , for)
branching construct (go to )

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

pre test loop

A

test for condition done before body of loop executed

17
Q

post test loop

A

test for condition done after body of loop executed (hence will always run at least once )