Stanford Programming Paradigms Flashcards

1
Q

Name a feature that distinguishes C++ from C

A

The presence of classes; object-orientation.

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

C was written by ___ in ___, C++ was written by ___ in ___.

A

Dennis Ritchie (w/ Bell Labs) wrote C in 1972, Bjarne Stroustrup expanded it to C++ in 1985.

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

Who designed Java and when did it appear?

A

James Gosling w/ Sun Microsystems designed Java in 1995.

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

Assembly is to C/C++ as ___ is to Java

A

Java bytecode.

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

In C, when using malloc to set aside 160 bytes in the heap, what actually happens?

A

A small pre-node header is added in front of the 160 bytes with meta information about the memory being allocated, e.g. it’s length, and a pointer is returned to the actual node. 164 or 168 bytes are actually set aside. You must be aware of this when returning to free the memory later.

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

Because C does not have automatic garbage collection, if you lose track of a piece of data (e.g. lose reference to it) it is referred to as a ___.

A

memory leak.

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

For efficiency and speed, the implementations of malloc and realloc do not perform ___.

A

error checking, this is the client’s responsibility to write error-free code.

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

In one implementation of storing dynamic data in the heap, the heap can be organized how? What is a different heuristic?

A

Into blocks of specific size and data, depending on its size, is always allocated a certain space, e.g. always 8 bytes or always 64 bytes, despite its actual size. Another implementation is to maintain a linked-list of blobs from each end of a piece of data, to search for a large enough free space.

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

Sometimes in C, an implementation of free may postpone execution until

A

malloc or realloc are called again, or free memory is actually needed.

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

In a CPU, local data that is being processed is stored in a

A

processor register.

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

The fastest data access in a CPU is provided by

A

processor registers.

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

Describe how arithmetic operations are conducted in a CPU?

A

data is loaded into registers where then it can be manipulated or tested by machine instructions in the arithmetic logic unit, a combinatorial digital electronic circuit.

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

In compiler optimization, ___ ___ is used to assign locations of program variables for access during execution.

A

register allocation. Access to registers is much faster than access to RAM.

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

A single unit of memory is a ___.
A byte is ___.
A KB is ___.

A

1 bit.
8 bits = 1 byte.
1024 bytes = 1 KB.

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

Is memory access in the stack or heap faster?

A

The stack is faster because memory management in the heap is much more involved.

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

The stack and heap are both stored in

A

main memory, RAM.

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

A very efficient way to implement an associative array as a data structure is to use a

A

hash table, which uses a hash function to map keys to specific integer values which can then be used for direct lookup. The hash function is the challenge, because all the data needs to be mapped evenly by the function (to minimize collisions). The hash function basically computers the index to an array of buckets or slots from the key value. Hash tables tend to be more efficient than search trees or other table structures.

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

A priority queue is

A

an abstract data type based on a queue but in which items have priority. Higher priority items will be served before lower priority items, and items of equal priority will be served based on their order in the queue.

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

Describe a space optimized way of storing a lexicon as a data structure

A

A radix tree/radix trie/prefix tree which is a tree where each node is part of a prefix of a word and every path through the tree from parent to child represents a valid word.

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

A binary tree is

A

A version of the tree data structure in which each parent node has only two children, a left and right child. Binary trees can be used for efficient searching and sorting.

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

In machine code, how is the type of operation to be performed on a piece of data indicated?

A

By an opcode which usually precedes the actual data.

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

The data structure the composes the call stack is referred to as ___ and includes information about

A

the activation record (stack frame), contains information about local variables, return address, and callee parameters.

23
Q

Explain what a buffer overflow is and how it can be used as a security exploit?

A

A buffer overflow occurs when some allocated portion of memory is exceeded, e.g. a string of length 15 is submitted to a string variable with defined length 10, or a 10 index array is accessed at index 50. A buffer overflow attack can occur where the overflow actually rewrites memory which holds values pertaining to another portion of the program execution in a way that changes future program behavior in an unwanted way. For example, overflowing a buffer on a password entry could overwrite another value used to test the validity of input and allow the user to proceed without inputting the correct password. They can otherwise simply cause the program to crash or elicit unexpected behavior.

24
Q

In C/C++, * and & are

A
  • is used for pointer declarations, to point to an address, whereas & is used as the actual address in memory, the physical location in memory. Addresses cannot be reassigned, pointers can.
25
Q

T/F — structs and classes are laid down in memory (think assembly) in basically exactly the same way

A

True: the only difference is that in C++ the default access model for structs is public whereas for classes it is private.

26
Q

To a compiler, C and C++ code are essentially the same thing — T/F

A

True.

27
Q

In C, the inclusion of header files, macro expansions, conditional compilation, and line control are handled by the

A

C preprocessor, which is a separate program invoked as the first part of translation during the compilation process.

28
Q

How is an object file produced from C source code?

A

First a preprocessor acts on the source to handle any #include or #define directives, then compilation occurs. In compilation, the code is sent to the assembler which generates assembly code, which is then further converted to machine code in the form of a binary object file. These fully compiled files remain isolated before linkage occurs. A .o file is an object file which is machine code.

29
Q

What is the role of linkage during C compilation?

A

Linkage replaces all the references to other symbols and files in compiled object files with the address to the actual files, to produce the final compilation output file.

30
Q

Compare bus errors to segmentation faults.

A

Bus errors are raised by hardware in the event that a process tries to access memory that the CPU cannot physically address. Segmentation faults occur when a program attempts to access memory it does not have access to. Bus errors are relatively less common than seg faults.

31
Q

Describe two concurrency models on machines with single or multi-processor designs

A

On a machine with a single processor, concurrency is achieved by having multiple threads run on a single processor via time-slicing at a very rapid rate creating the appearance of concurrency. On a multi-core system, multiple threads can execute in parallel simultaneously.

32
Q

In concurrent programming, what abstract data type is used to control access to common resources, e.g. the processor.

A

Semaphores.

33
Q

Name an example of a synchronization mechanism that can be used upon entry and exit to a critical section of code in a concurrent program.

A

Semaphore, it can control access to this part of the code by multiple threads.

34
Q

In a multithreaded environment, when two or more processes block each other / multiple processes from proceeding, this is a

A

deadlock. The program just locks, doesn’t raise an error and does not continue trying to run forever as in an infinite loop.

35
Q

(car (cdr (cdr ‘(1 2 3 4 5) )))

A

3

36
Q

What are the two main dialects of Lisp?

A

Common Lisp and Scheme.

37
Q

Guy Steele and Gerald Sussman designed ___ in the year ___.

A

Scheme in 1970, presented in the Lambda Papers from MIT based on work in Lambda Calculus.

38
Q

What is the basic structured data type in Scheme?

A

The list, which is implemented essentially as a linked list.

39
Q

In Scheme, all type checking is deferred until

A

runtime; Scheme is weakly typed.

40
Q

T/F — Lists in Scheme are immutable data types.

A

True.

41
Q

ML, Haskell, Erlang, and Elm, among other languages, are

A

functional languages.

42
Q

___, created in the year ___, is a purely functional programming language for declaratively creating web browser-based graphical user interfaces.

A

Elm, created in 2012

43
Q

___ is a language framework written in Java that implements the language Scheme.

A

Kawa, the name is derived from the Polish word for “coffee” and is a play on ‘Java’.

44
Q

ML is a ___ ___ version of Scheme

A

strongly typed

45
Q

Scheme uses infix or prefix notation?

A

Prefix.

46
Q

Dennis Ritchie of Bell Labs created the C programming language in part in order to

A

re-implement the Unix Operating System. C provides an abstraction layer above assembly level code.

47
Q

What is the difference between static and dynamic typing?

A

In static languages, the type of all variables is known at compiler time and thus can be error-checked at this stage (C/C++, Java). In dynamically-typed languages, the type is interpreted at runtime (Python, Perl, Ruby, JavaScript).

48
Q

___ is a family of programming languages with a long history of a distinctive, fully parenthesized prefix notation, originally developed in the year ___.

A

Lisp, 1958.

49
Q

Structured Programming refers to the

A

use of block structures and avoidance of goto statements as a stylistic measure to vastly simplify the development and maintenance of computer programs. The term was coined by Dijkstra.

50
Q

The principle data structure in Python is the ___

A

dictionary, which is basically a map implemented as a hash table.

51
Q

How are classes implemented in Python?

A

They are implemented as dictionaries.

52
Q

What is OCaml?

A

OCaml is a member of the ML language family, and extends the core Caml construct with object oriented constructs.

53
Q

Describe the historical lineage behind Haskell?

A

Lisp > ML > Caml > OCaml > Miranda (1985) > Haskell (1990)