Programming Concepts Quiz
Closures, recursion, Big-O, promises — the concepts that really matter behind modern code.
- Closure
- A function that "captures" variables from its surrounding scope
- Closure = function + its lexical environment. It keeps access to variables from the scope where it was defined — even after that scope has exited. Classic: counter factory.
- Big-O O(1)
- Constant time — independent of input size
- O(1) = constant time. Whether 10 or 10 million elements — the operation takes the same time. Example: array index access, average hash lookup.
- Sort Big-O
- , ,
- Growth hierarchy. Rule of thumb: O(n²) gets slow around 10,000 elements, O(2ⁿ) is unusable already at 30. O(n log n) is the lower bound for comparison-based sorting.
- Algorithm → Complexity
- Zuordnen
- Quicksort & Merge sort are O(n log n) (Quicksort worst-case O(n²)). Subset-sum is NP-complete — all known exact solutions are exponential.
- Recursion — required?
- A base case (termination condition)
- Without a base case → infinite recursion → stack overflow. Example: factorial(n) → if (n <= 1) return 1; else return n * factorial(n-1).
- Promise states
- Lückentext
- pending → fulfilled (resolve) or pending → rejected (reject). Once "settled" (fulfilled/rejected), state cannot change. .then() reacts to fulfilled, .catch() to rejected.
- Pure function
- No side effects AND same input → same output
- Pure = deterministic + side-effect free. Not pure: Math.random(), Date.now(), DOM mutation, console.log, file I/O. Pure functions are testable and cacheable (memoization).
- Language → Type system
- Drag & Drop
- Dynamic: type determined at runtime (JS, Python, Ruby). Static: at compile time (Rust, Java, Go, C, TS). Python has optional type hints but is dynamic.
- == vs === in JavaScript
- == performs type coercion, === compares without type conversion
- 0 == "0" → true (coercion!), but 0 === "0" → false. Best practice: always use ===. Infamous traps: [] == false → true, null == undefined → true.
- Hoisting
- var declarations are conceptually moved to the top of the scope (value stays undefined)
- console.log(x); var x = 5; → prints undefined (not ReferenceError). Declaration is hoisted, assignment is not. let and const are hoisted too but stay in the "temporal dead zone" — accessing before the let line = ReferenceError.
- Event Loop — execution order
- , ,
- Order: synchronous code → microtask queue (promises) → task queue (setTimeout, I/O). Microtasks have priority over macrotasks — even with delay 0.
- Currying
- Converting a function with n arguments into a chain of n functions each taking 1 argument
- add(2, 3) → add(2)(3). Enables partial application: const add5 = add(5); add5(10) → 15. Named after Haskell Curry (mathematician). In Haskell all functions are automatically curried.
- Higher-order function
- It takes functions as arguments and/or returns a function
- map, filter, reduce, forEach — all take a function. setTimeout, addEventListener too. Currying produces HOFs because the result is a new function.
- Pass by value vs reference
- Pass by value (copy) — primitives by value, objects by reference-value (copy of the reference)
- Tricky! Primitives (Number, String, Bool) get copied. For objects, the reference is copied — so .property changes affect the original, but reassignment (obj = ...) is local only.
- Deep recursion — danger?
- Stack overflow — each call uses a stack frame
- Typical stack sizes: 1–8 MB. After thousands of calls → stack overflow. Solutions: iterative variant, trampolining, or tail call optimization (not widely implemented in JS).
- Data structure → typical operation
- Zuordnen
- Choice of data structure = often more important than micro-optimization. Hash map is O(1) amortized — during rehashing a single insert can be O(n), but on average constant.
- Memoization
- Caches function results for known inputs (trade memory for time)
- Classic example: Fibonacci. Naive recursion: O(2ⁿ). With memoization: O(n). Only works for pure functions. React.memo and useMemo build on this.
- this in arrow functions
- Lexically inherited from the surrounding scope (no own this)
- Big difference from function(): arrow functions have no own this, arguments, super or new.target. Therefore bad as methods but great for callbacks (no this loss).
- Concept → description
- Zuordnen
- Reference counting (e.g. Swift, partly Python) struggles with cycles. Tracing GC (JS, Java) handles that, but can cause pauses.
- Side effect
- Any interaction with the outside world other than return (DOM, file, network, logging, mutation)
- Side effects: console.log, fetch, DOM manipulation, global state, parameter mutation. Functional programming pushes side effects to the edges (core pure, edges effectful).
- Paradigm → typical language
- Zuordnen
- Smalltalk shaped OOP principles ("everything is an object"). Erlang runs in telecom — famous for "nine nines" uptime (99.9999999%).