Programming Fundamentals
Programming is the art and engineering of instructing a computer to perform a task. While the CSS Computer Science paper does not require candidates to write polished software, it does demand a confident grasp of programming concepts, paradigms and execution models.
A finite, unambiguous and effective step-by-step procedure for solving a problem or accomplishing a task. Every program implements one or more algorithms.
Computer programming language hierarchy
Computers execute machine code (binary). Programmers work in higher-level languages and rely on translators.
| Level | Language type | Example | Translator |
|---|---|---|---|
| 1st gen | Machine code | 10110000 01100001 | None |
| 2nd gen | Assembly | MOV AL, 61h | Assembler |
| 3rd gen | High-level procedural | C, Pascal, Fortran | Compiler / Interpreter |
| 4th gen | Declarative / DSL | SQL, MATLAB | Interpreter |
| 5th gen | Logic / AI | Prolog | Inference engine |
Compiler vs. interpreter
- Compiler — translates entire source program into target code once; faster execution (C, C++, Rust).
- Interpreter — translates and executes statement-by-statement; faster development cycle (Python, Ruby).
- Hybrid (JIT) — compile to bytecode, then JIT to machine code (Java, C#).
Programming paradigms
A paradigm is a fundamental style of building programs.
- Imperative / procedural — sequence of statements; state changes (C, Pascal).
- Object-oriented — data and behaviour bundled in objects (Java, C++, Python).
- Functional — pure functions and immutability (Haskell, Lisp, Scala).
- Logic — declarative rules and unification (Prolog).
- Declarative — describe what, not how (SQL).
- Concurrent / parallel — multiple threads or processes (Go, Erlang).
- Event-driven — programs respond to events (JavaScript GUIs).
Control flow
Three classical Bohm-Jacopini constructs are provably sufficient to express any algorithm:
- Sequence — top-down execution.
- Selection —
if,if-else,switch. - Iteration —
while,do-while,for.
Additional flow tools include break, continue, goto (now deprecated) and exception handling (try-catch-finally).
- A boolean expression has only two values: true/false.
- Short-circuit evaluation: in
A && B, B is not evaluated if A is false (in most C-family languages). - Recursion — a function that calls itself; requires a base case to terminate.
- Tail recursion — can be optimised into iteration to save stack space.
Data types
- Primitive: int, float, char, boolean.
- Composite: arrays, strings, records, tuples.
- Reference: pointers (C/C++), references (Java).
- Abstract Data Types (ADTs): stack, queue, list, tree, graph — defined by behaviour not implementation.
Type systems
- Static vs. dynamic — types fixed at compile time (Java) or runtime (Python).
- Strong vs. weak — strict type enforcement vs. implicit conversions.
- Manifest vs. inferred — types declared by the programmer vs. inferred by the compiler.
Functions and modularity
A function (procedure, method, subroutine) is a named unit of code that performs a task. Properties:
- Signature: name, parameters, return type.
- Scope of variables: local, global, block-scoped.
- Parameter passing:
- Pass-by-value — copy of the argument.
- Pass-by-reference — alias to the original.
- Pass-by-pointer — explicit reference (C).
Stack frames
Each function call places a frame on the call stack, containing parameters, local variables, return address. Excessive recursion leads to stack overflow.
Memory model
In C-family languages:
| Region | What it holds | Lifetime |
|---|---|---|
| Code/Text | Compiled instructions | Process |
| Data | Initialised globals | Process |
| BSS | Uninitialised globals | Process |
| Heap | Dynamically allocated (malloc/new) | Until freed |
| Stack | Frames for function calls | Function lifetime |
Common pitfalls: memory leaks (heap not freed), dangling pointers (pointer to freed memory), buffer overflows (security risk).
Object-Oriented Programming
Four pillars:
- Encapsulation — bundle data and methods; expose via interface.
- Inheritance — derive new classes from existing ones (single, multiple, hierarchical).
- Polymorphism — same interface, different behaviour (overloading, overriding).
- Abstraction — hide complexity behind interfaces.
Important OOP concepts
- Class vs. object — blueprint vs. instance.
- Constructor / destructor.
- Access modifiers — public, protected, private.
- Method overriding vs. overloading.
- Virtual functions — runtime polymorphism.
- Abstract class vs. interface.
Error handling
- Compile-time errors — syntax, type mismatch.
- Runtime errors — division by zero, null reference.
- Logical errors — program runs but produces wrong output.
Exception mechanisms (try-catch) separate error-handling from main logic. Languages like Go prefer error returns; Rust uses Result<T, E>.
Software development tools
- IDE — Visual Studio, Eclipse, IntelliJ, VS Code.
- Version control — Git (most widely used), Mercurial, SVN.
- Build systems — Make, Maven, Gradle, npm.
- Testing frameworks — JUnit, PyTest, Mocha.
- Debuggers — gdb, pdb, IDE-integrated.
For CSS conceptual questions, learn to distinguish between declarative (state what you want — SQL, HTML) and imperative (state how to compute — C, Python loops) styles. Modern frameworks like React combine both: imperative under the hood, declarative on top.
Popular modern languages
| Language | Year | Paradigm | Key strength |
|---|---|---|---|
| C | 1972 | Procedural | Systems, embedded |
| C++ | 1985 | OOP + procedural | Performance + abstraction |
| Java | 1995 | OOP, JVM | Portability, enterprise |
| Python | 1991 | Multi-paradigm | Readability, data science |
| JavaScript | 1995 | Event-driven, OOP | Web ubiquity |
| C# | 2000 | OOP, CLR | Microsoft ecosystem |
| Go | 2009 | Concurrent | Cloud, networking |
| Rust | 2010 | Systems, memory-safe | Performance + safety |
| Swift | 2014 | Multi-paradigm | iOS / macOS |
| Kotlin | 2011 | OOP, functional | Android |
Why these fundamentals matter
Pakistan's IT exports — over USD 3 billion in FY 2023-24 — depend on a workforce confident in these basics. The CSS paper increasingly draws on practical literacy: an officer in Pakistan Telecommunication Authority, NADRA or IT-MoITT must read code well enough to evaluate vendor proposals and audit systems. A solid grasp of paradigms, memory and OOP is the entry ticket.