← Introduction to Computer Science

Unit 1: What a Program Is and How It Runs

What actually happens between typing code and a computer doing something, and the three kinds of mistakes that stop it.

5 topics · 20 questions

Each topic is a path, not a menu: work down it in order and every question builds on the one above. Click any of them to watch the answer draw itself.

What a Program Actually Is

  1. How it worksHow does a list of instructions turn into a computer moving numbers around in memory?
  2. Worked exampleTrace what happens in memory as x = 5 runs, then print(x + 1) runs right after it.
  3. Common mistakeWhy isn't a program the same thing as the app icon you click to start it?
  4. Going furtherWhat happens inside the computer between double-clicking an icon and a window appearing on screen?
Your turn1 of 2
A program runs x = 14, then y = x + 13, then z = y·8. What value does z hold?

From Source Code to Machine Code

  1. How it worksHow does a compiler translate source code into machine instructions before anything runs?
  2. How it worksHow does an interpreter run source code line by line without ever producing a separate file?
  3. Common mistakeWhy isn't a Python source file the same thing a processor actually executes?
  4. Going furtherWhat changes when the same source file runs through a compiler instead of an interpreter?
Your turn1 of 2
A compiler translates a source file of 351 lines into 7 machine instructions per line on average. How many total machine instructions does the compiled program contain?

The Fetch-Decode-Execute Cycle

  1. How it worksHow does a processor fetch one instruction, decode it, and execute it before moving to the next?
  2. Worked exampleTrace three fetch-decode-execute cycles for the instructions load x, add 1, store x.
  3. Going furtherWhat happens to the fetch-execute cycle when one instruction is a jump to a different address?
Your turn1 of 2
A processor runs a program by repeating the fetch-decode-execute cycle once per instruction. For a program of 697 instructions with no jumps, how many total cycles does the processor perform?

Syntax Errors, Runtime Errors, and Logic Errors

  1. How it worksHow does a syntax error get caught before the program ever starts running?
  2. Worked exampleTrace why total = totl + 1 crashes with a runtime error instead of just giving a wrong number.
  3. Common mistakeWhy doesn't a program that runs and prints an answer guarantee the answer is correct?
  4. Going furtherWhat changes about a bug's symptoms once a logic error moves from one line to a different line?
  5. Ties togetherHow does a logic error slipping past every test connect to why edge cases matter in testing?
Your turn1 of 2
A logic error sits on line 35 of a program in one version, and after a rewrite the same bug ends up on line 115 in the next version. By how many lines did the bug's location shift?

Why Computers Are Relentlessly Literal

  1. How it worksHow does executing x = y then y = x overwrite the original x before y ever gets it back?
  2. Worked exampleTrace x = 5, y = 8, x = y, y = x and find the final values sitting in x and y.
  3. Common mistakeWhy isn't a computer clever enough to guess that you meant to swap two values?
  4. Going furtherWhat changes in the swap once a third temporary variable is introduced to hold one value?
Your turn1 of 2
x starts at 14 and y starts at 13. The program runs x = y, then y = x, attempting to swap them but failing as expected. What is the sum x + y at the end?