Unit 2: Variables, Types, and Memory
Where a value actually lives when you name it, why types matter, and the two classic ways numbers misbehave.
6 topics · 22 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.
Variables as Names for a Place in Memory
- How it worksHow does a variable name get attached to a specific spot in the computer's memory?
- Worked exampleTrace age = 25 then age = age + 1 and find what value age points to after each line.
- Common mistakeWhy isn't a variable a box that physically holds a number inside it?
Your turn1 of 2
age is set to 56. Then age is set to age + 13. What value does age point to after both lines run?Primitive Types: Integers, Floats, Booleans, and Strings
- How it worksHow does a type tell the computer how many bytes to use and what operations make sense?
- Worked exampleTrace what type(5), type(5.0), type(True), and type(5 + 5.0) each report.
- Common mistakeWhy doesn't adding an integer and a float always keep the result an integer?
- Going furtherWhat happens to a type once a boolean is mixed into arithmetic like True + 1?
Your turn1 of 2
A program mixes a boolean into arithmetic as True + 70, where True behaves as the integer 1. What value does this expression evaluate to?Static Typing Versus Dynamic Typing
- How it worksHow does a statically typed language check every variable's type before the program ever runs?
- How it worksHow does a dynamically typed language let one variable hold an integer and later hold a string?
- Common mistakeWhy isn't dynamic typing the same thing as a language having no types at all?
- Going furtherWhat happens when a dynamically typed program only discovers a type error while running live?
Your turn1 of 2
A statically typed program has 142 variables, and a type checker catches errors in 6 of them before the program ever runs. How many of the n variables pass the type check with no error?Type Conversion and Casting
- How it worksHow does casting a string to an integer with int() convert it before arithmetic ever runs?
- Worked exampleTrace int('5') + int('3') and compare it against '5' + '3' evaluated as strings.
- Going furtherWhat happens when a program tries to cast the text hello into an integer?
Your turn1 of 2
A program computes int('348') + int('324'), casting each string to an integer before adding. What value does this expression evaluate to?Integer Overflow
- How it worksHow does a fixed number of bits wrap a number back around to negative once it climbs too high?
- Worked exampleTrace a signed 8-bit integer counting up from 125 by adding 1 four times in a row.
- Common mistakeWhy doesn't every programming language silently wrap a number the way fixed-width integers do?
- Going furtherWhat changes about overflow once a language switches to an arbitrary-precision integer type?
Your turn1 of 2
A signed 8-bit integer, whose valid range is -128 to 127, starts at 89 (a positive value). How many additions of 1 are needed before it first overflows into a negative value?Why 0.1 Plus 0.2 Does Not Equal 0.3
- How it worksHow does storing a fraction like 0.1 in binary force the computer to round it before it even starts?
- Worked exampleTrace 0.1 + 0.2 and compare the printed result against 0.3 with an equality check.
- Common mistakeWhy isn't a floating-point number stored with the same exactness as a fraction on paper?
- Going furtherWhat changes once two floating-point numbers are compared with a small tolerance instead of ==?
Your turn1 of 2
A program adds 7 tenths and 6 tenths, meaning it computes a/10 plus b/10. What is the exact mathematical value of this sum?