COMP 118 Final Study Guide

1. Python Basics & Core Concepts

Key ideas

  • What a variable is (name, value, type).
  • Common data types: int, float, str, bool, list.
  • Assignment = vs comparison ==.
  • What a Boolean expression is.
  • Immutable vs mutable types.

Things to be able to do

  • Predict the type of an expression.
  • Decide if a given expression is True or False.
  • Explain assignment vs equality comparison.
  • Explain what it means for a type to be immutable.

Sample practice questions

Conceptual

  1. In Python, explain each part of this line: temp = 98.6
    • What is the variable name?
    • What is the value?
    • What is the type?
  2. What does each of these evaluate to (True/False)? 5 == 5 5 == "5" 7 != 3 + 4 "cat" < "dog"
  3. Which of these are immutable, and why?
    • str
    • list
    • tuple

2. Conditionals and Loops (Python)

Key ideas

  • if, elif, else.
  • while vs for loops.
  • Loop conditions and when loops stop.
  • Off-by-one errors.

Things to be able to do

  • Translate English conditions into Python Boolean expressions.
  • Trace a while loop and a for loop.
  • Decide if a loop ever terminates.

Sample practice questions

Translate to code

  1. Write a Python Boolean expression for: “age is between 13 and 19 inclusive.” (Use a single expression.)
  2. Write an if statement that prints "zero", "positive", or "negative" depending on the value of n.

Loop tracing
3. What does this print?

i = 10
while i > 3:
    print(i)
    i -= 3
  1. What does this print? for k in range(1, 6): if k % 2 == 0: print("even") else: print("odd")

3. Functions, Strings, and Lists (Python)

Key ideas

  • Why we use functions (def).
  • Parameters vs arguments; return values.
  • Indexing vs slicing.
  • Iterating over lists and strings.

Things to be able to do

  • Define and call simple functions.
  • Explain what a function returns (including when there is no return).
  • Trace function calls with parameters.
  • Use indexing (s[0]) and slicing (s[1:4]).

Sample practice questions

Conceptual

  1. What is the difference between a parameter and an argument in a Python function?
  2. What does this function return? def f(x): x = x + 5 return x y = 3 z = f(y)
    • What are the final values of y and z?

Strings and lists
3. Suppose:

word = "Python"
nums = [10, 20, 30, 40]

What are the results of each expression?

  • word[0]
  • word[1:4]
  • nums[-1]
  • nums[1:3]
  1. What does this print? data = [1, 2, 3] data.append(4) data[1] = 99 print(data)

4. Python Code Writing Practice

Key ideas

  • Writing small functions that:
    • Loop through lists
    • Accumulate sums or counts
    • Use simple conditionals
  • Simple input–processing–output patterns.

Sample coding questions (practice)

  1. Count positives Write a function count_positives(nums) that returns how many numbers in the list nums are strictly greater than 0. def count_positives(nums): """Return the number of strictly positive values in nums.""" # your code here
  2. Sum of odds Write a function sum_odds(nums) that returns the sum of all odd numbers in the list nums.
  3. Temperature conversion Write a program that:
    • Asks the user for a temperature in Fahrenheit (as a number),
    • Converts it to Celsius,
    • Prints the result.
    Formula: C = (F - 32) * 5 / 9
  4. Word repeater Write a function repeat_word(word, n) that returns a single string containing word repeated n times, separated by spaces. Example: repeat_word("hi", 3) # "hi hi hi"

5. More Python Tracing & Errors

Key ideas

  • How for with range(start, stop, step) works.
  • Difference between syntax errors and runtime errors.
  • Local variables and scope.

Sample practice questions

  1. What does this print? total = 0 for x in range(2, 9, 2): total += x print(total)
  2. For each code snippet, circle whether it’s a syntax error, runtime error, or no error: a. if x > 0 print("positive") b. nums = [1, 2] print(nums[5]) c. value = 10 print(value / 2)
  3. What happens to the local variable x after this function finishes? def demo(): x = 7 print(x)

6. C++ Basics & Arrays

Key ideas

  • Role of #include <iostream> and using namespace std;
  • main() function and return 0;
  • Declaring variables and arrays.
  • Simple loops over arrays.

Things to be able to do

  • Read and explain a very small C++ program.
  • Write a function that:
    • takes an array and its size,
    • loops through the array,
    • computes a sum, min, max, or average.

Sample practice questions

Conceptual

  1. In C++, what does this line mean? int scores[5];
  2. Compare C++ and Python for this idea:
    • In Python, we can print with print(x).
    • In C++, how do we print a variable named x?

Array & function practice
3. What does this program print?

#include <iostream>
using namespace std;

int main() {
    int a[] = {2, 4, 6};
    int sum = 0;
    for (int i = 0; i < 3; i++) {
        sum += a[i];
    }
    cout << sum << endl;
    return 0;
}
  1. Write a C++ function int countZeros(int arr[], int size) that returns how many elements in arr are equal to 0.

7. C++ Code Writing Practice

Key ideas

  • Iterative loops (for, while) in C++.
  • Simple functions that compute or transform.

Sample coding questions (practice)

  1. Count negatives Write a function that counts negative values in an array: int countNegatives(int arr[], int size) { // your code here }
  2. Find minimum Write a function: int minValue(int arr[], int size); that returns the smallest integer in a non-empty array.
  3. Read and average Write a complete C++ program that:
    • Reads 4 integers from the user,
    • Stores them in an array,
    • Computes the average as a double,
    • Prints the average.

8. Mixed Python/C++ Comparison

Key ideas

  • Recognize similar control structures in both languages.
  • Understand where they differ:
    • Python: indentation, no types on variables, print.
    • C++: curly braces {}, type declarations, cout.

Sample practice questions

  1. Write a Python loop and a C++ loop that both print the numbers 1 through 5, one per line.
  2. Compare these two lines: Python: total = 0 C++: int total = 0;
    • How are they the same?
    • How are they different?

9. Classes & Objects (Python & C++)

Key Ideas

  • A class is a blueprint for creating objects.
  • An object is an instance of a class with its own data.
  • Attributes (fields) store data inside an object.
  • Methods are functions that operate on an object’s data.
  • Constructors set up new objects:
    • Python: __init__(self, ...)
    • C++: ClassName(...)
  • Objects use dot notation to access attributes and call methods.

Things to Be Able to Do

  • Recognize attributes and methods in a class definition.
  • Understand what a constructor does and what runs when an object is created.
  • Create objects and call their methods in both Python and C++.
  • Write simple classes with attributes and 1–2 behavior methods.
  • Trace code that creates objects and modifies internal state.

Sample Practice Questions


Conceptual

  1. What is the difference between a class and an object?
  2. What is the difference between a field/attribute and a method?
  3. Why might using a class be better than using separate variables for everything?

Python Examples

Code Reading

class Counter:
    def __init__(self, start):
        self.value = start

    def increment(self):
        self.value += 1

c = Counter(10)
c.increment()
c.increment()
print(c.value)

Questions:

  • Which line creates the object?
  • What attribute does this class store?
  • What does this program print, and why?

Code Writing

Write a Python class Rectangle that stores width and height, has an area() method, and then create one.

Example expectation:

r = Rectangle(3, 4)
print(r.area())   # should output 12

C++ Examples

Code Reading

#include <iostream>
using namespace std;

class Point {
private:
    int x, y;

public:
    Point(int xVal, int yVal) : x(xVal), y(yVal) {}

    void moveRight() { x++; }

    void print() const {
        cout << "(" << x << ", " << y << ")" << endl;
    }
};

int main() {
    Point p(2, 5);
    p.moveRight();
    p.print();     // what prints here?
}

Questions:

  • What are the private data fields?
  • What does the constructor do?
  • After moveRight(), what are the coordinates?

Code Writing Practice

Write a C++ class Student with:

  • fields name and id,
  • a constructor that sets them,
  • a printInfo() method,
  • create a Student in main() and print info.
Scroll to Top