Back to all Computer Science topics
Topic 6.1Computer Science HL20 flashcards

Programming fundamentals

Practice Flashcards

Flip cards to reveal answers
Card 1 of 206.1.1
6.1.1
Question

What is a variable, and what does its data type decide?

Click to reveal answer

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

Card 1definition
Question

What is a variable, and what does its data type decide?

Answer

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.

Card 2definition
Question

Name the five data types and what each holds.

Answer

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.

Card 3concept
Question

Why does + behave differently on numbers and strings?

Answer

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.

Card 4comparison
Question

What is the difference between a local and a global variable?

Answer

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.

Card 5concept
Question

Why are local variables preferred?

Answer

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

Card 6concept
Question

Where do string positions start, and what does a slice include?

Answer

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.

Card 7definition
Question

What do len, find and split each do?

Answer

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.

Card 8concept
Question

Do string methods change the original string?

Answer

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().

Card 9process
Question

How do you split an email address at the @?

Answer

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.

Card 10concept
Question

When is split better than using fixed positions?

Answer

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

Card 11definition
Question

What is an exception?

Answer

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.

Card 12definition
Question

What do try, except and finally each do?

Answer

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.

Card 13definition
Question

What are the three sources of failure to look for?

Answer

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.

Card 14concept
Question

When should you validate rather than catch?

Answer

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.

Card 15concept
Question

Why must closing a file go in finally?

Answer

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

Card 16definition
Question

What are the three kinds of error?

Answer

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.

Card 17definition
Question

Name the four debugging techniques and what each is for.

Answer

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.

Card 18process
Question

What is the method for narrowing down a bug?

Answer

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.

Card 19concept
Question

Where do logic errors usually hide?

Answer

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.

Card 20concept
Question

Why change only one thing at a time?

Answer

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.

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
IB Computer Science HL Topic 6.1 Flashcards | Programming fundamentals | Aimnova