comparative Flashcards

1
Q

imperative programming

A

the programmer
gives an explicit sequence of steps which
are done in order. (e.g. C++, Python, PHP,
Rust)

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

declarative

A

the
programmer describes the result they
need, and the language/compiler decides
how to best produce it. (e.g. Haskell)

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

high vs low level

A

Higher-level: more like what people want to
write/understand. (e.g. Python, Haskell)
Lower-level: more like what the computer
understands/executes. (e.g. assembly, C

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

is an interpreter purely interpreting code?

A

Most programs that appear to be interpreters
are actually both a compiler and interpreter.

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

what does a virtual machine represent

A

a virtual machine is an
interpreter for bytecode.

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

what are the pros for JIT

A

optimize machine code for the specific CPU architecture (e.g. Skylake vs generic x86);
collect usage stats and optimize output for the way code is actually being used;
create type-specific versions of functions for the types of arguments they actually receive (see “dynamic binding” , later).

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

what is JIT

A

Interpreting (byte)code at run-time always comes with a speed penalty. Even the most clever bytecode needs to be somehow processed while it’s running: that takes instructions and therefore time.
A Just-In-Time compiler starts with bytecode and either…
1. interprets but when it decides that some piece of logic will be used frequently, it…
2. for all code… … during execution, compiles that code to machine code and stores it in memory.
Then, when that code needs to execute (again), it can use the machine code version.

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

cons of JIT

A

But, JITs have to do their work during program execution. That will slow things down (at least momentarily). Performance may be less predictable.

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

are languages restricted to a specifc compilation method?

A

Any programming language*
could be compiled to machine
code, or to a VM, or interpreted
directly.

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

A question: does the compiler know a variable or value’s type when it compiles the program?

A

For statically typed languages, the answer is yes: C, Java, C#, Dart, etc.
Where the types aren’t known until runtime, the language is dynamically typed: Python, JavaScript, Lisp, Ruby, etc.

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

pros of statically typed langauges

A

Static:
In a statically typed language, you can have more confidence that the types are right.

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

pros an cons for dynamic typed langauges:

A

Dynamic:
Pros:
Dynamic typing allows more flexibility and often less code. The programmer doesn’t have to explicitly declare/allocate/type variables, which saves keystrokes/effort.
Cons:
types must be checked every time a line of code is executed.

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

what are 3 things that always have to be checked at run time

A

there are some related things that always have to be checked at runtime: array bounds, division by zero, overflow, etc.

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

what is gradual typing definition

A

where values/ variables can be given static types that can be checked at compile-time, but it’s optional.

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

when do we know which operation has to be applied for:
static
dynamic

A

Statically bound:
the compiler knows exactly which operation is going to happen.
Java
Dynamically bound:
the details have to be determined at run-time
Pyhton

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

benefit of static and cons to dynamic when applying (add) operator

A

executing the processor’s ADD instruction
a type check, symbol table lookup (__add__), method call, then do the ADD.

17
Q

major drawback to python

A

Standard python code compiles to byte code
Python is slow not because it is interpreted (compiled to byte), but it is slow because of dynamic binding

18
Q

describe JIT with if else statement

A

if types match previous calls:
call statically-typed machine code
else:
interpret dynamically-typed bytecode,
or maybe compile a version for the new types.

Maybe you get a statically-typed implementation of your dynamically-typed code.

19
Q

when does a static typed code run dynamically

A

polymorphism on java, but not on C++

20
Q

what type in Haskell is an example of a Generic

A

This will work on any arguments in the Eq typeclass, and Haskell can statically-compile any specialized version it needs.

21
Q

what is the purpose of type inference

A

If the compiler can infer types, it can statically bind in its output.

22
Q

strongly typed definition

A

every value has a single well-defined type, so you can do lots of checking.

23
Q

weakly typed definition

A

single values can be treated as different types, so type checking is harder.

24
Q

how to get away with strongly typed

A

pointer casting

C and Java are mostly strongly typed since values are explicitly statically typed, but pointer/​reference casting allows the programmer to do weakly-typed things.

25
Q

what is an example of strongly typed in a langauge

A

Python is strongly typed since the type of each value is tracked by the language (but at runtime since it’s dynamically typed).

26
Q

places where imutable might be importatn

A

In a dictionary/​map/​hash table key: the data structure needs to be able to check the value when it’s inserted and know it doesn’t change while in the collection. (e.g. it always has the same hash value)
As an argument: a mutable object could be modified whenever it is given as an argument to a function. Immutability ensures it won’t be.
Sharing between threads: safe for multiple threads to access it if it can’t be changed.

27
Q

places where mutable is important`

A

Collections: don’t want to rebuild a list of a million elements just to add one more.
State: state of a game (or whatever) changes frequently and could be shared by many parts of the code.
We want to pass an object to a function and have it modified.

`

28
Q

what is static, heap and stack memory

A

Static: memory known to be needed at compile time. e.g. strings in in the program, globals, C static.
Stack: function parameters and functionlocal variables (or similar for other variables scopes).
Heap: dynamically-allocated objects. e.g. malloc in C; new in C++, Java, C#; all objects in Python, Ruby

29
Q

what type of memory does dynamic uses the most

A

In dynamic languages, usually everything is on the heap (but there is a call stack for functions).

30
Q

what are the two ways for garbage collection

A

Reference counting and tracing

31
Q

what is reference counting and when can it be checked

A

garbage collection keeps track of the number of references to each object. When the number decreases to zero, delete. Can’t handle cyclic data structures; requires space and time to maintain the counters.
C++ example:
Assuming we keep the unique_ptr on the stack, it will be deleted as appropriate. When the unique_ptr is deleted, it will automatically delete the object it refers to: the pointer was unique, so “reference counting” is easy and can be done at compile-time

32
Q

what is tracing

A

garbage collection looks for which objects are reachable from references available in the program: everything else is garbage. There are many strategies to do this quickly and without stopping execution while it happen

33
Q

what is a finalizer

A

A garbage collector is part of a language’s runtime environment that looks for objects on the heap that can no longer be accessed (garbage) an frees them (after calling their finalizers, in languages that have the concept).

34
Q

what are the 3 types of tracking heap ownership

A

Manual memory management
Run-time garbage collection
Compile time ownership tracking

35
Q

what is concurrent programming

A

A concurrent program is one where several parts of the program are running together (possibly taking turns, or possibly on different cores).

36
Q

what is parallel programming

A

A parallel program is a concurrent program where different processes or (kernel) threads allow different parts of the code to run at the same time on multiple cores.

37
Q

what is a process

A

an instance of a running program.

38
Q

what is a thread

A

A single process can have several points where it is executing concurrently: each is a thread.

39
Q

what is thread safe

A

is thread safe if it can be used by multiple concurrent threads without anything going wrong.