Make sure to go through each type of each thing in the slides
Test Format
- bunch of multiple choice questions
- some calculation questions
First question slide is the only one used for first test
Programming Languages Midterm Study Guide
1. Aliasing
Effects
- Intended Use: Sometimes useful for optimizing performance by reducing unnecessary data copies
- Unintended Side Effects: Changes made to one variable affect all other aliases, potentially causing unpredictable behaviour
Examples:
Aliasing with Pointers (C/C++)
int x = 10;
int *p1 = &x;
int *p2 = p1; // p1 and p2 both point to the same memory location
 
*p1 = 20;  // Modifies x
printf("%d\n", *p2);  // Output: 20- Here, p1andp2both referencex, so changing*p1also effects*p2.
Aliasing with References (C++)
int a = 5;
int &b = a;  // 'b' is an alias for 'a'
b = 10;
std::cout << a;  // Output: 10- bis just another name for- a, meaning both share the same memory.
Aliasing in Python (Mutable Objects)
lst1 = [1, 2, 3]
lst2 = lst1  # Both reference the same list
 
lst2.append(4)
print(lst1)  # Output: [1, 2, 3, 4]- Since lists are mutable, modifying lst2also changesls1
Problems Caused by Aliasing
- Unintended modifications: if a function modifies a variable, all aliases reflect the change
- Difficult debugging: hard to track which variable is modifying the data
- Concurrency issues: in multithreading, two threads modifying aliased variables can lead to race conditions
2. Programming Language Categories
1. Imperative – Uses statements to change program state (e.g., C, Python).
2. Object-Oriented – Organizes code around objects and classes (e.g., Java, C++).
3. Functional – Treats computation as evaluation of mathematical functions (e.g., Haskell, Lisp).
4. Logic – Based on formal logic, uses rules and facts (e.g., Prolog).
3. Keywords vs. Reserved Words
- Keyword: A word with special meaning in specific contexts (e.g., int, if, for in C).
- Reserved Word: A word that cannot be used as an identifier (variable name) in a language.
4. Attributes of a Variable
- Name – The identifier used to refer to the variable.
- Address – The memory location where the variable is stored.
- Value – The data stored in the variable at a given time.
- Type – Determines what kind of data the variable holds and how it behaves.
5. Static Variables and Recursion
- Static variables retain their value across function calls.
- Because they persist in memory and do not reset, they cannot be used for recursion (since recursive calls require fresh memory allocations).
6. Arrays: Different Storage Strategies
| Type | Storage Duration | Size Flexibility | 
|---|---|---|
| Static | Allocated at compile-time | Fixed size | 
| Fixed-Stack Dynamic | Allocated at runtime on the stack | Fixed size, set at declaration | 
| Stack-Dynamic | Allocated at runtime on the stack | Size can change during execution | 
| Fixed Heap-Dynamic | Allocated at runtime on the heap | Fixed size, once allocated | 
| Heap-Dynamic | Allocated at runtime on the heap | Size can grow/shrink dynamically | 
7. Reference Types vs. Pointers
- Reference Types: Act as aliases for existing variables, do not allow arithmetic operations.
- Pointers: Store memory addresses and allow arithmetic operations (e.g., pointer arithmetic in C).
8. Dangling Pointers
- A pointer that still points to memory that has been deallocated.
- Can cause undefined behavior, crashes, or security vulnerabilities.
9. Activation Record of a Subprogram
- Stores execution-related information when a function/subprogram is called.
- Includes:
- Local Variables – Variables defined within the function.
- Parameters – Inputs passed to the function.
- Dynamic Link – Pointer to the previous activation record (used for nested calls).
- Return Address – Where execution should resume after function call ends.
Tips for Studying
- Understand real-world examples for each concept.
- Practice coding problems related to arrays, pointers, and recursion.
- Try drawing memory diagrams to visualize aliasing, activation records, and pointer behavior.
- Compare different programming paradigms with code snippets.
Personal Practice
Static Scope
Example 1
procedure main is
    x, y, z : Integer;
 
    procedure sub1 is
        x, y : Integer;
 
        procedure sub2 is
            x : Integer;
            begin
                x := 7;
                y := x + z;
            end;
 
        procedure sub3 is
            z : Integer;
            begin
                z := 5;
                x := z * y;
            end;
 
        begin
            x := 2;
            y := 3;
            sub2(); // LOCAL x -> 7, y -> 7 + 3 = 10
            sub3(); // LOCAL z -> 5, x -> 5 * 10 = 50
            z := x + y; // z -> 60
        end;
 
    procedure sub4 is
        y, z : Integer;
        begin
            y := 4;
            z := 6;
            x := y + z; // -> 10
        end;
 
begin
    x := 1;
    y := 2;
    z := 3;
    sub1(); // z -> 60
    sub4(); // x -> 10
    // x = 10
    // y = 2
    // z = 60
end;Dynamic Scope
Example 1
procedure main is
    a, b, c : Integer;
 
    procedure sub1 is
        a, c : Integer;
 
        procedure sub2 is
            b : Integer;
            begin
                b := 6; -- a=2, LOCAL b=6, c=3
                c := b + a; -- c=8
            end;
        procedure sub3 is
            a : Integer;
            begin
                a := 4; -- LOCAL a=4
                b := a * c; -- b=32
            end;
 
        begin
            a := 2;
            c := 3;
            sub2(); -- a=2, b=2, c=3 --> a=2, b=2, c=8
            sub3(); -- a=2, b=2, c=8 --> a=2, b=32, c=8
            a := a + b + c; 
            print(a);
        end;
 
    procedure sub4 is
        b, c : Integer;
        begin
            b := 5;
            c := 7;
            a := b + c; -- a=12
        end;
 
begin
    a := 1;
    b := 2;
    c := 3;
    sub1(); -- a=1, b=2, c=3 --> a=1, b=32, c=3
    sub4(); -- a=1, b=32, c=3 --> a=12, b=32, c=3
    // a=12
    // b=32
    // c=3
end;