Practice Flashcards
What is a variable, and what does its data type decide?
Track your progress — Sign up free to save your progress and get smart review reminders based on spaced repetition.
All Flashcards in Topic 6.1
Below are all 20 flashcards for this topic. Sign up free to track your progress and get personalized review schedules.
6.1.15 cards
What is a variable, and what does its data type decide?
A named place in memory holding one value. Its data type says what kind of value it is, which decides both what can be stored there and what operations work on it.
Name the five data types and what each holds.
Integer: a whole number. Decimal: a number with a fractional part. Char: a single character. String: a sequence of characters. Boolean: exactly True or False.
Why does + behave differently on numbers and strings?
It adds numbers and joins strings, so 12 + 1 gives 13 but "12" + "1" gives "121". Mixing the two — "12" + 1 — fails, and is one of the commonest bugs.
What is the difference between a local and a global variable?
A local variable is created inside a function, cannot be seen from outside, and is destroyed when the function ends. A global variable is created outside any function and is visible throughout the program.
Why are local variables preferred?
If a local value is wrong, the cause is inside that function. If a global value is wrong, any part of the program could have changed it — so the bug could be anywhere.
6.1.25 cards
Where do string positions start, and what does a slice include?
Positions start at 0, and a slice excludes its end position. So text[2:5] gives the characters at 2, 3 and 4 — three characters, which is simply 5 minus 2.
What do len, find and split each do?
len gives the number of characters. find gives the position where a piece of text starts, or -1 if it is absent. split breaks a string into a list at each occurrence of a separator.
Do string methods change the original string?
No. upper, lower and replace all return a new string and leave the original untouched. To keep the change you must assign it back: text = text.upper().
How do you split an email address at the @?
Find the position of the @, then take everything before it and everything from one position after it: at = email.find("@"); user = email[:at]; domain = email[at + 1:]. The +1 skips the @ itself.
When is split better than using fixed positions?
Whenever the parts can vary in length. code[0:2] works only while the prefix is exactly two characters, whereas splitting at the separator survives a longer or shorter one.
6.1.35 cards
What is an exception?
A problem that appears while the program is running, rather than a mistake in the code's grammar. Without handling the program simply stops; with handling it can report the problem and carry on.
What do try, except and finally each do?
try holds only the lines that might fail. except (catch in Java) handles one specific kind of failure. finally runs whether or not anything failed, and is where files get closed.
What are the three sources of failure to look for?
Unexpected input, usually from people; unavailable resources such as a missing file or unresponsive network; and logic errors, where the code ran on values nobody planned for.
When should you validate rather than catch?
When the problem is predictable and inside your control — an empty list, a zero divisor. Catch the things outside it: files, networks and databases, which can fail between your check and your use.
Why must closing a file go in finally?
If a line inside try fails, control jumps straight to except and any later lines in try never run. finally runs either way, so the file is closed rather than left open holding a system resource.
6.1.45 cards
What are the three kinds of error?
Syntax errors stop the program running at all. Run-time errors stop it partway with a message. Logic errors let it run perfectly and produce the wrong answer — and only the third needs debugging.
Name the four debugging techniques and what each is for.
Trace table: work through by hand, no tools needed — the exam method. Print statements: show a value at a chosen point. Breakpoints: pause and inspect every variable. Step-by-step execution: run one line at a time and watch values change.
What is the method for narrowing down a bug?
Write down what you expected for one specific input; find the first value that is wrong, halving the search each time; look only at the lines that set that value; then change one thing and test again.
Where do logic errors usually hide?
At the boundary: an empty list, a single item, the first and last positions, and values that are exactly equal. If a program is wrong, test the boundary before anything else.
Why change only one thing at a time?
If the change works, you know which change fixed it. If it does not, you have not added a new set of suspects to investigate.
Topic 6.1 study notes
Full notes & explanations for Programming fundamentals
Computer Science exam skills
Paper structures, command terms & tips
Want smart review reminders?
Sign up free to track your progress. Our spaced repetition algorithm will tell you exactly which cards to review and when.
Start Free