CSS Prepare

Programming Fundamentals

9 min read

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.

Algorithm

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.

LevelLanguage typeExampleTranslator
1st genMachine code10110000 01100001None
2nd genAssemblyMOV AL, 61hAssembler
3rd genHigh-level proceduralC, Pascal, FortranCompiler / Interpreter
4th genDeclarative / DSLSQL, MATLABInterpreter
5th genLogic / AIPrologInference 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:

  1. Sequence — top-down execution.
  2. Selectionif, if-else, switch.
  3. Iterationwhile, do-while, for.

Additional flow tools include break, continue, goto (now deprecated) and exception handling (try-catch-finally).

Key Points
  • 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:

RegionWhat it holdsLifetime
Code/TextCompiled instructionsProcess
DataInitialised globalsProcess
BSSUninitialised globalsProcess
HeapDynamically allocated (malloc/new)Until freed
StackFrames for function callsFunction lifetime

Common pitfalls: memory leaks (heap not freed), dangling pointers (pointer to freed memory), buffer overflows (security risk).

Object-Oriented Programming

Four pillars:

  1. Encapsulation — bundle data and methods; expose via interface.
  2. Inheritance — derive new classes from existing ones (single, multiple, hierarchical).
  3. Polymorphism — same interface, different behaviour (overloading, overriding).
  4. 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.

LanguageYearParadigmKey strength
C1972ProceduralSystems, embedded
C++1985OOP + proceduralPerformance + abstraction
Java1995OOP, JVMPortability, enterprise
Python1991Multi-paradigmReadability, data science
JavaScript1995Event-driven, OOPWeb ubiquity
C#2000OOP, CLRMicrosoft ecosystem
Go2009ConcurrentCloud, networking
Rust2010Systems, memory-safePerformance + safety
Swift2014Multi-paradigmiOS / macOS
Kotlin2011OOP, functionalAndroid

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.

Programming Fundamentals — Computer Science CSS Notes · CSS Prepare