Grade by Grade
A. Programming Concepts and Skills · Data Types, Files and Expressions

Data Types, Files and Expressions: Unit Review

A1.1 A1.2 A1.3 A1.4 A1.5 A3.1 A4.1 A4.3About 40 minutes
Learning goals
  • I can use integer division, remainders, type conversions and comparisons correctly, including in tricky cases.
  • I can explain the limits of integer and floating-point representations and design around them.
  • I can build and process lists of records and objects, including comparable objects and objects in a hierarchy.
  • I can read and write text, CSV, binary, JSON, XML and database files, and debug and document my code professionally.

Words to know

floor division
// rounds the quotient down; % gives a remainder with the sign of the divisor.
narrowing conversion
A conversion that can lose information, such as float to int.
lexicographic order
Character-by-character comparison by code point; the first difference decides.
overflow
A result outside the range of a fixed-width type; usually wraps around.
rounding error
The difference between a real number and the nearest float.
list of records
A one-dimensional list whose elements are compound values with named fields.
serialization
Saving in-memory data to a file or string (CSV, JSON, XML, binary, database).
docstring
Documentation stored with a module, class or function and shown by help().

Pulling the unit together

This unit took the data types you knew from ICS3U and looked at them the way a university computer science course does: exactly how integer division and conversions behave at the edges, why string order and object order must be defined carefully, where integers and floats run out of room, how to group data into records and objects, how to keep it in files of many kinds, and how to debug and document code like a professional.

Work through one practice set per lesson below. Answers are hidden behind each question. Then take the 10-question quiz. Code in questions is shown one line per bullet: type it, do not copy it.

How to use this review

Do each set without notes first, then check. For every miss, write the lesson number next to it. If you miss two or more in one set, reread that lesson's explanation and worked examples, then redo its "Your turn" practice. Finally, run any code you were unsure about in Python; predicting and then checking output is the fastest way to fix a misunderstanding.

Data types andexpressionsLessons 1-4Limits ofrepresentationLessons 5-6Records andobjectsLessons 7-10ExternalfilesLessons 11-13CodemaintenanceLessons 14-15documentation and debugging (A4) run through every lesson
How the unit fits together. Values and expressions (A1.1 to A1.3) have limits (A1.4); they are grouped into records and objects (A1.5); those are saved in and loaded from files (A3.1); and every program is debugged and documented professionally (A4.1, A4.3).
Will people reador edit the file?Many queries ormany users?noyesflat rows:CSV / textnested data:JSON / XMLyesdatabase(SQLite)nobinaryrecords(struct)
A rough guide to choosing a file format (Lessons 11 to 13). Readable formats suit data people look at; binary files and databases suit large data that programs search and update. Real projects weigh other factors too (size, speed, what other programs expect), so treat this as a starting point.

Integer Division and Remainders

1

Give -23 // 4 and -23 % 4.

2

Convert 200 days into weeks and days with one function call.

3

Write an expression for the number of 30-seat buses needed for n students, without floats.

4

What does Java give for -23 % 4, and why does it differ from Python?

Algorithms Built on Remainders: Digits, Bases, GCD and Check Digits

1

Trace the digit-sum loop on 2026. What is returned?

2

Convert 77 to binary by repeated division.

3

Trace gcd(84, 60).

Type Conversion: Strings, Characters and Numbers

1

Give int(-8.5), math.floor(-8.5), round(8.5) and round(9.5).

2

Which raise ValueError: int("12.0"), float(" 12 "), int("1F", 16), int("")?

3

Shift the letter Y three places forward with wrap-around, using ord, chr and %.

Non-Numeric Comparisons: Comparing and Sorting Strings

1

True or False: "Ottawa" < "ottawa", "100" < "99", "ice" < "iceberg".

2

What does sorted(["b", "B", "a"]) give, and how do you sort ignoring case?

3

Why use == and not is to compare strings?

Limits of Finite Representation: Integer Bounds and Overflow

1

Give the range of a 32-bit signed int.

2

What is 250 stored in an 8-bit signed integer?

3

Write −20 in 8-bit two's complement.

4

Why can Python compute 2 ** 100 exactly when Java's long cannot?

Limits of Finite Representation: Floating-Point Precision and Rounding Errors

1

Is 0.1 * 3 == 0.3 True? Is 1.5 + 2.25 == 3.75 True? Explain.

2

Rewrite safely: while t != 1.0: t += 0.1.

3

Name two exact alternatives to floats for money.

One-Dimensional Arrays of Records

1

Write a dataclass Reading with fields station: str and temp_c: float.

2

Write an expression for the coldest reading in a list readings.

3

Why are records safer than parallel lists when sorting?

Arrays of Objects: State, Behaviour and References

1

After b = a[:] (a list of objects) and b[0].stock = 0, what is a[0].stock? Why?

2

What is wrong with items = [Item("?", 0, 0)] * 4?

3

Two separately created objects of an ordinary class have the same fields. What does x == y return, and how do you change that?

Making Objects Comparable

1

Which two methods do you write when using @total_ordering?

2

What should __lt__ return when other has an unknown type?

3

Is "a < b if a has fewer wins or fewer goals" a valid ordering? Explain.

Type Conversion in an Inheritance Hierarchy

1

Which is always safe: upcasting or downcasting?

2

In Java, Vehicle v = new Train(...); Bus b = (Bus) v; What happens?

3

Write the Python check before using a Bus-only field route.

Just for fun

Unit summary in one joke: a float, an int and a string walk into a bar. The int orders 1 drink, the float orders 0.30000000000000004 drinks, and the string orders "1" + "1" = "11" drinks. The bartender asks for their types before serving anyone.

Reading and Writing Text and CSV Files

1

Which mode adds to the end of a file, and which erases it first?

2

Why open files with with and encoding="utf-8"?

3

A CSV field is "Sault Ste. Marie, ON". What should you use to read the file, and why?

Binary Files and JSON

1

What is struct.calcsize("<qd")?

2

Records of 12 bytes: at which byte does record 50 start?

3

What JSON does json.dumps({"ok": False, "x": None}) produce?

XML Files and Databases

1

In <stop id="S1"><name>Union Station</name></stop>, what are the attribute and the text?

2

Write a safe SQLite query for members of the club stored in variable c.

3

What happens if you forget con.commit() after inserting rows?

Code Maintenance: Using Documentation to Fix Errors Independently

1

Which line of a traceback do you read first?

2

What does print(sorted([3, 1, 2]), [3, 1, 2].sort()) show?

3

Put in order: fix and retest, reproduce, check the docs, read the message, isolate.

Code Maintenance: Documenting Code to Industry Standards

1

Fix this docstring: """this function returns the total""".

2

Which comment is useful: i += 1 # increment i or i += 1 # skip the header row?

3

What does a doctest check?

Check your understanding

Quiz

1.

What is -17 % 5 in Python?

2.

What is the check digit c that makes (31 + c) % 10 == 0?

3.

What is int("42.0")?

4.

Which is True in Python?

5.

What is 120 + 10 in an 8-bit signed integer?

6.

Which statement about floats is true?

7.

What does max(roster, key=lambda p: p.goals) return?

8.

A Python class should support < for sorting. Which method do you write?

9.

Which file choice best suits nested settings for a web service?

10.

Which is the best summary line for a docstring?

For parents and teachers

How to use the results: each practice set matches one lesson, so mistakes point straight to the lesson to revisit.

  • Mistakes with negative // and %, divmod or ceiling division: Integer Division and Remainders.
  • Digit loops, base conversion, GCD or check digits: Algorithms Built on Remainders.
  • int() versus round versus floor, ord/chr, or ValueError handling: Type Conversion: Strings, Characters and Numbers.
  • String order, case-insensitive sorting, == versus is: Comparing and Sorting Strings.
  • Ranges, two's complement or overflow: Integer Bounds and Overflow.
  • 0.1 + 0.2, tolerances, money as cents: Floating-Point Precision and Rounding Errors.
  • Dataclasses, key= functions, parallel lists: One-Dimensional Arrays of Records.
  • Methods, aliases, shallow and deep copies: Arrays of Objects.
  • __lt__, @total_ordering, ordering rules: Making Objects Comparable.
  • isinstance, upcasting and downcasting: Type Conversion in an Inheritance Hierarchy.
  • File modes, CSV, binary, JSON, XML or SQL: the three files lessons.
  • Reading tracebacks, using help(), writing docstrings: the two Code Maintenance lessons.

Encouragement: this is university-level material. A student who can explain why -7 % 3 is 2, why 0.1 + 0.2 is not 0.3, and why ? placeholders matter is well prepared for a first-year computer science course.