Skip to content

Introduction to Programming in Python final exam review

The topics, worked on a live board with an AI tutor.

Units

  1. Values, types and expressions

    Practice

    What are the value and the type of "4" + "5", and of 4 + 5?Answer

    '45', a str; and 9, an int

    Hint: Quotes make text, and plus joins text

    Values, types and expressions
    With a = 6 and b = 2, then a = a * b and b = a - b, what does print(a, b) show?Answer

    12 10

    Hint: a becomes 12 first, then b is 12 - 2

    Variables and assignment
  2. Booleans and conditionals

    What exams ask

    Every decision a program makes, whether to log a user in, stop a loop or charge a fee, rests on a condition, and a condition with the wrong operator or the wrong grouping gives the wrong answer for just the unusual inputs that tests tend to miss.

    Practice

    For year = 1600, what do year % 4 == 0, year % 100 == 0 and year % 400 == 0 give?Answer

    True, True and True

    Hint: 1600 is 4 times 400

    Booleans and comparisons
    What does the program print for score = 79?Answer

    C

    Hint: 79 >= 80 is False, 79 >= 70 is True

    if, elif and else
  3. Loops

    Practice

    For n = 2 and count = 0, with while n < 50: n = n * 2 and count = count + 1, what does print(count, n) show?Answer

    5 64

    Hint: n goes 4, 8, 16, 32, 64

    while loops
    For k in range(4, 20, 5) with the same block, what is the last value of k?Answer

    19

    Hint: 4, 9, 14, 19, and 24 is past the stop

    for loops and range
  4. Functions

    What exams ask

    Programs beyond a screenful are only manageable as small functions with clear jobs; it is how every project, lab and exam question with "write a program that" is best approached, and testing pieces separately finds bugs where they are, not three calls away.

    Practice

    With def double(n): return n * 2, what does print(double(7) + 1) show?Answer

    15

    Hint: The call becomes 14 first

    Defining and calling functions
    With def sq(x): return x * x and def f(a): return sq(a) + 1, how many frames are there at the deepest point of print(f(3)), and what does it print?Answer

    3 frames, global, f and sq; it prints 10

    Hint: The deepest point is while sq runs

    Scope and the call stack
  5. Strings

    What exams ask

    Text is everywhere in programs, from names and dates to file lines and DNA, and pulling out parts of it by position is the first string skill; the off-by-one at a slice's stop is a classic exam trap, and the same indexing works on lists next unit.

    Many text tasks have no ready-made method, such as checking a password's rules, cleaning names or validating codes, and the loop over characters with an accumulator is the pattern behind them; reversing and palindromes are classic first exam and interview questions.

    Practice

    name = "Grace". What are name[0], name[-1] and name[2]?Answer

    G, e and a

    Hint: G is at 0

    String indexing and slicing
    s = "Python"; s.lower(); print(s); What is printed, and how do you fix it to print python?Answer

    Python; write s = s.lower() before the print

    Hint: Keep the result

    String methods and immutability
  6. Lists and tuples

    What exams ask

    Almost every program keeps a collection of values, test scores, names, readings, and a list is Python's everyday way to hold one; most list bugs are an index one past the end, or a method whose result was used as if it were the list.

    Practice

    For t = [10, 20, 30, 40, 50], what are t[-2] and t[len(t) - 1]?Answer

    40 and 50

    Hint: -1 is the last, so -2 is the one before it

    Lists: indexing and mutation
    What does this print: total = 0, for n in [2, 4, 6], total = total + n * n, print(total)?Answer

    56

    Hint: 4 + 16 + 36

    Traversing lists
  7. Dictionaries and sets

    Practice

    With scores = {"Ana": 8, "Raj": 6}, what is scores["Raj"] + scores.get("Mei", 1)?Answer

    7

    Hint: "Mei" is not a key, so get gives its default

    Dictionaries
    What does len(set([4, 4, 7, 7, 7, 2])) give?Answer

    3

    Hint: The different values are 4, 7 and 2

    Sets
  8. Files and exceptions

    What exams ask

    Real programs meet typing mistakes, missing files and impossible values; catching the right exception keeps them running with a helpful message, and raising one makes a function refuse bad input clearly; reading and writing try and except is a standard exam question.

    CSV is the plain format that spreadsheets, sensors and websites export, and a first real data task is almost always read rows, convert fields, and summarise by group; this read, convert, accumulate pattern is a standard exam and project question.

    Practice

    What are the lines of a file after write("x"), write("y\n") and write("z")?Answer

    Xy, then z

    Hint: Only the second write ends a line

    Reading and writing files
    Write safe_float(text) that returns float(text), or 0.0 when the text is not a number.Answer

    Try: return float(text), except ValueError: return 0.0; safe_float("2.5") gives 2.5 and safe_float("abc") gives 0.0

    Hint: Float raises ValueError too

    Exceptions
  9. Classes and objects

    What exams ask

    Real programs share code between kinds of things, such as a savings account that is a bank account with extras, and the questions that matter are which method Python runs for a given object and what breaks when the parent's setup is skipped; both are standard exam traces.

    Practice

    After p = BankAccount("Dee", 5) and p.balance = p.balance + 10, what does print(p.owner, p.balance) show?Answer

    Dee 15

    Hint: The attribute is read, then written back

    Classes, objects and __init__
    With c = BankAccount("Cy", 10), then c.deposit(5) and c.deposit(5), what does c.balance show?Answer

    20

    Hint: Each call reads what the last one stored

    Methods, self and __str__
  10. Recursion

    What exams ask

    Recursion is how many problems are most naturally written, folders inside folders, trees, divide and conquer searches and sorts, and tracing a recursive call on the stack is a standard exam question in a first programming course.

    Splitting a sequence into its first item and the rest is the pattern behind recursive code on lists, strings, files and trees, and writing reverse, sum or count this way is a standard exam question in a first programming course.

    Practice

    Using factorial, what is factorial(6)?Answer

    720, since 6 × 120

    Hint: 5! is 120

    Recursion: base case and recursive case
    Trace reverse("dog") and give each returned value.Answer

    The empty string, "g", "go", "god"

    Hint: Reverse("og") is "go"

    Recursion on strings and lists
  11. Searching and sorting

    What exams ask

    Searching is one of the most common things programs do, and binary search is the first algorithm where a smart idea turns a million steps into twenty; tracing low, mid and high and counting comparisons are standard exam questions.

    Sorting is everywhere, and these two simple sorts are where students first trace an algorithm pass by pass and count its work; showing the list after each pass and counting comparisons are standard exam questions, and the quadratic count explains why faster sorts exist.

    Practice

    How many comparisons does linear search make for 91, and for 4?Answer

    10 and 1

    Hint: Where are they in nums?

    Linear and binary search
    Give the list after each pass of selection sort on [5, 1, 4, 2].Answer

    1, 5, 4, 2; then 1, 2, 4, 5; then 1, 2, 4, 5 unchanged

    Hint: The second pass finds 2

    Selection sort and insertion sort
  12. Testing and debugging

    What exams ask

    Code that passes a few happy examples often fails at a boundary or on empty input; writing tests for ordinary, edge and error cases is how programmers catch that before users do, and choosing such cases is a common exam and interview question.

    Every programmer spends much of their time debugging, and a calm method, read the error, find the line, trace the values, fix one thing and retest, is what separates guessing from fixing; reading a traceback and tracing a loop are standard exam questions.

    Practice

    Write two ordinary tests for a function double(x) that returns 2 * x.Answer

    For example assert double(3) == 6 and assert double(-4) == -8

    Hint: Include a negative number

    Testing with assert and test cases
    A traceback ends with IndexError, list index out of range, at return xs[3] where xs = [7, 8, 9]. What is wrong?Answer

    Xs has indexes 0, 1 and 2 only, so xs[3] does not exist

    Hint: Count from 0

    Debugging

Questions

What does the Introduction to Programming in Python review cover?

Every unit of the course, 12 in all. Pick one and Helio teaches it on the board, then asks you questions. Values, types and expressions; Booleans and conditionals; Loops; Functions; Strings; Lists and tuples; Dictionaries and sets; Files and exceptions; Classes and objects; Recursion; Searching and sorting; Testing and debugging.

Can it help the week before my final?

Yes. Pick a topic, and Helio explains it, checks you and goes over what you miss.

Is it the same as my class?

It follows the usual topics of Introduction to Programming in Python. For anything else on your syllabus, type the topic and Helio teaches that too.

How much is free?

30 minutes a month for one learner, with no card. Starter is free for 7 days with a card: cancel in time and pay nothing.

A tutor you can talk to

30 minutes every month. No card.