Practice Questions

Sample solutions can be found here.

Please note that these are generic questions, and may not include all the topics discussed in your particular offering of the course. It is also possible that some questions are about topics not covered in your offering; please ignore any such questions.

  1. In a vector<string>, a repeat is when two identical strings occur next to each other. For example, {"a", "b", "c", "c", "d"} has a repeat that starts at index location 2. However, the vector<string> {"a", "b", "c", "d", "c"} does not have any repeats.

    Write two different functions for finding the first repeat in a vector<string>. Call the first function first_repeat_iter(v), and write it so that it uses a loop and no recursion. Call the second function first_repeat_recur(v), and write it so that it uses recursion and no loops.

  2. In this question your task is to write different versions of a function that counts the number of vowels in a string. The vowels are a, e, i, o, and u, both uppercase and lowercase.

    For example:

    vowel_count("apple") returns 2
    vowel_count("APPLE") returns 2
    vowel_count("") returns 0
    vowel_count("fgh") returns 0
    

    Implement different version of vowel_count using:

    1. a regular C-style while-loop.
    2. a regular C-style for-loop.
    3. a ranged for-loop.
    4. recursion
    5. using the count_if function from the STL.

    Hint: First implement a helper function called is_vowel(c) that returns true if the character c is a vowel, and false otherwise.

  3. Write a function called is_prime(n) that returns true if the int n is a prime number, and false otherwise. An integer n is prime if it is greater than 1, and has exactly two different divisors, 1, and n itself. The first few primes are 2, 3, 5, 7, 11, 13, ….

  4. Write a function called ends_with_s(v) that takes a vector<string> as input, and returns a new vector<string> containing just those strings in v whose last character is either s or S.

    For example:

    ends_with_s({"cats", "dog", "hats", "mice"}) returns {"cats", "hats"}
    
  5. Consider the following fragment of code:

    int a = 3;
    int b = 5;
    
    cout << a << " " << b << "\n"; // 3 5
    swap(&a, &b);
    cout << a << " " << b << "\n"; // 5 3
    

    Implement the swap function.

  6. Write a program that prints all 456,976 four-character strings consisting of lowercase letters a - z:

    aaaa
    aaab
    aaac
    ...
    aaba
    aabb
    aabc
    ...
    zzzx
    zzzy
    zzzz
    

    Make your program as short and simple as possible while still being relatively readable.

  7. Write a recursive program (that doesn’t use any loops or any STL functions) that prints all 676 2-character strings consisting of lowercase letters a - z:

    aa
    ab
    ac
    ...
    ba
    bb
    bc
    ...
    zx
    zy
    zz
    
  8. Create a class called Product that stores the name and cost (in pennies, as an int) and works with code such as this:

    const Product cheese{"Plasma Cheese", 230};
    cheese.println();  // prints: Plasma Cheese, 230
    
    const Product cheese_copy{cheese};
    cheese_copy.println();  // prints: Plasma Cheese, 230
    

    Your Product class must also meet these requirements:

    • It must use an initialization list in its constructors to initialize its variables.

    • An error is thrown (using the error function from the course) by the constructors if an empty name or negative cost is passed to the constructor. For example:

      const Product widget{"Lupper", -450};  // throws an error: -450 is not allowed
      
      const Product shoe{"", 3200};  // throws an error: name can't be empty string
      
    • There should be no (legal!) way to set or modify the name or cost of a Product object after it is constructed.

    • There should be no unnecessary work done or memory used.

  9. Write a function called is_sorted(v) that tests if the strings in v, a vector<string>, are in alphabetical order. Do it in two different functions: the first should use a loop, and the second should use recursion (with no loops).

  10. (These definitions were not covered in the current offering of the course, so you can ignore this question.) Define each of the following terms, and list at least one good thing and one bad thing for each:

    1. black box testing
    2. white box testing
    3. exhaustive testing

    Make your answers brief, and use clear, grammatical English.

  11. In mathematics, the 1-norm of a vector \(\boldsymbol{x} = (x_1, x_2, \ldots, x_n)\) is defined as the sum of the absolute values of the entries, i.e.:

    \[\begin{split}\|\boldsymbol{x}\|_1 &= |x_1| + |x_2| + \ldots + |x_n| \\ &= \sum_{i=1}^{n} |x_i|\end{split}\]

Write five different C++ functions, each of which calculates the 1-norm of a mathematical vector in the following different ways:

  1. Represent the mathematical vector as an array of \(n\) doubles. Use a C-style for-loop (with an index variable) in your answer.

  2. Represent the mathematical vector as vector<double>, and use a for- each loop (with no index variable) in your answer.

  3. The same as the previous question, except use a while-loop in your answer.

  4. Represent the mathematical vector as a vector<double>, and use recursion (with no loops) in your answer.

  5. Instead of passing the vector directly to the function, pass in a pointer of type double* representing the first element, and a pointer of type double* representing one past the last element. You would use it like this:

    double arr[] = {5.5, -1.0, 2.66, 0.4};
    double x = norm(arr, arr + 4);
    

Passing a begin and end pointer as in the last question is similar to how many of C++’s standard algorithms (like sort and reverse) work.

  1. The following questions are related:

    1. Write a function called is_upper(c) that returns true if the char c is one of the uppercase letters 'A', 'B', …, 'Z', and false otherwise.
    2. Write a function called to_lower(c) that returns the lowercase version of c if c is an uppercase letter, and c unchanged otherwise.
    3. Using a loop (and no recursion), write a function called to_lower_loop(s) that returns a new string that’s the same as string s, except all uppercase letters have been converted to lowercase.
    4. Using recursion (and no loops), write a function called to_lower_rec(s) that returns a new string that’s the same as string s, except all uppercase letters have been converted to lowercase. You can write helper functions if needed.
  2. The following questions are related:

    1. Implement the following function:

      // Pre-condition:
      //    none
      // Post-condition:
      //    returns a new string that is the same as s, but all
      //    occurrences of c have been removed
      // Example:
      //     remove_all('a', "alley cat") returns "lley ct"
      string remove_all(char c, const string& s)
      
    2. Implement the following function:

      // Pre-condition:
      //    none
      // Post-condition:
      //    returns a new string that is the same as s, but all
      //    occurrences of every character in bad have been removed
      // Example:
      //     remove_all("l a", "alley cat") returns "eyct"
      string remove_all(const string& bad, const string& s)