Final Exam Review

The final covers the whole course, in both Python and C++. It has three sections:

  • Section 1: Multiple choice (35 questions). Principles of programming, predicting the output of short programs, and reading error messages.
  • Section 2: Short answer (10 questions). Explaining what a piece of code does, finding an error, and fixing an error.
  • Section 3: Write code (5 programs). Three in Python and two in C++.

The practice questions below are not the exam questions, but they cover the same ideas. For any “what does this print?” question, predict first, then run the code to check.


Part 1: Principles of Programming

  • What is the difference between a syntax error, a runtime error, and a logic error? Which one lets the program run to the end but gives the wrong answer?
  • (COMP 191 only): What is pseudocode? Write pseudocode for finding the smallest number in a list.
  • What does a compiler do with a C++ program before it runs? How is that different from how Python runs a program?
  • C++ is statically typed. What happens if you declare int x = 5; and later try to store a string in x?
  • What is functional decomposition? How would you split a “read grades, average them, print a report” program into functions?

Part 2: Python

Expressions and formatting

  • What do these print? print(10 - 6 / 2), print(2 ** 3 * 2), print(17 // 5, 17 % 5)
  • Write an f-string that prints a variable cost with exactly two digits after the decimal point. What happens if you leave off the f?

Input and type conversion

  • What type does input() always return? Which of these cause an error: int("42"), int("4.2"), float("4.2")?
  • Why does "Score: " + 10 cause an error? Give two ways to fix it.

Conditionals

  • In an if / elif / else chain, how many branches can run?
  • What would go wrong in a grading program if you checked score >= 60 before score >= 90?

While loops

  • What are the three parts of a counter-controlled while loop? What happens if you forget the update?
  • How many times does this loop print, and what is the last value printed? x = 100 while x > 1: print(x) x = x // 2
  • A program should add up the numbers the user types until they enter -1 to stop. If the user types 5, 10, then -1, it prints 14 instead of 15. Why? Rearrange the code so the -1 is never added. total = 0 num = 0 while num != -1: num = int(input("Number (-1 to stop): ")) total = total + num print("Total:", total)

For loops and range

  • What values do these produce? range(2, 8), range(0, 20, 5), range(5, 0, -1)
  • Write a for loop that prints the even numbers from 2 to 20.

Strings and lists

  • For s = "notebook", what are s[0:4], s[-3:], and len(s)?
  • What is the final list? items = ["a", "b"] items.insert(1, "z") items.append("c") items.pop(0) print(items)
  • A list has 6 items. What are its valid indexes?

Functions

  • What is the difference between returning a value and printing it? What does a function with no return give back?
  • Can you use a function’s local variable after the function ends?
  • What happens if a return statement is indented inside a loop?

Dictionaries

  • What happens if you look up a key that isn’t in a dictionary? How can you check first?
  • Write a loop that counts how many times each letter appears in a word, using a dictionary.

Classes and objects

  • What does __init__ do? What is self, and why does every method need it?
  • If you create two objects from the same class and change an attribute on one, does the other change?

Text files

  • What do the modes "r", "w", and "a" do? Which one erases the file?
  • Why do you usually call .strip() on each line you read from a file?

Error messages

What usually causes each of these? IndentationError, NameError, TypeError, IndexError, KeyError, ValueError


Part 3: C++

Basics and arithmetic

  • Why does the compiler sometimes report a missing semicolon on the line after the mistake?
  • What do these print? 9 / 4, 9 % 4, 9.0 / 4

Switch

  • What does break do in a switch?
  • What does this print when level is 2? switch (level) { case 1: cout << "X"; break; case 2: cout << "Y"; case 3: cout << "Z"; break; default: cout << "!"; }

Functions, references, and scope

  • What is the difference between void f(int n) and void f(int &n)?
  • If you declare a variable inside the braces of a loop, can you use it after the loop?

Vectors and maps

  • How do you add an item to a vector and find its size? Why should a loop use i < v.size() and not i <=?
  • What happens when you assign to a map key that already exists? In what order does a map keep its keys?

File input

  • What does while (fin >> x) do, and when does the loop stop?

Part 4: Code Writing Practice

These are the kinds of programs you should be able to write from scratch. Practice a couple of variations of each.

Python

  1. Tip calculator. Ask the user for a bill amount and a tip percentage. Print the tip and the total, each with two digits after the decimal point. Example: a bill of 40 and a tip of 20 prints Tip: $8.00, Total: $48.00.
  2. Sum the big numbers. Write a function sum_over(nums, limit) that takes a list of numbers and a limit and returns the sum of only the numbers greater than the limit. Call it and print the result. Example: sum_over([4, 12, 7, 20], 10) returns 32.
  3. Most expensive item. Read item prices from the user until they enter 0. Then print how many items were entered and the highest price, without counting the 0. Example: entering 3.50, 12.00, 7.25, then 0 prints 3 items, most expensive: $12.00.

C++

  1. Average step count. Read 6 daily step counts (integers) with cin into a vector<int> using push_back. Then loop over the vector and print the average, including the decimal part. Example: the input 4000 6500 8000 3000 7000 5500 prints Average: 5666.67. (Watch out for integer division.)
  2. Describe the temperature. Write a function string describeTemp(int degrees) that returns "cold" below 40, "mild" from 40 to 64, "warm" from 65 to 84, and "hot" at 85 and above. In main, read a temperature with cin and print its description. Example: 72 prints warm.

Scroll to Top