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.
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, thevector<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 functionfirst_repeat_iter(v)
, and write it so that it uses a loop and no recursion. Call the second functionfirst_repeat_recur(v)
, and write it so that it uses recursion and no loops.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:
- a regular C-style while-loop.
- a regular C-style for-loop.
- a ranged for-loop.
- recursion
- using the
count_if
function from the STL.
Hint: First implement a helper function called
is_vowel(c)
that returnstrue
if the characterc
is a vowel, andfalse
otherwise.Write a function called
is_prime(n)
that returnstrue
if theint
n
is a prime number, andfalse
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, ….Write a function called
ends_with_s(v)
that takes avector<string>
as input, and returns a newvector<string>
containing just those strings inv
whose last character is either s or S.For example:
ends_with_s({"cats", "dog", "hats", "mice"}) returns {"cats", "hats"}
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.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.
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
Create a class called
Product
that stores the name and cost (in pennies, as anint
) 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.
Write a function called
is_sorted(v)
that tests if the strings inv
, avector<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).(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:
- black box testing
- white box testing
- exhaustive testing
Make your answers brief, and use clear, grammatical English.
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:
Represent the mathematical vector as an array of \(n\)
double
s. Use a C-style for-loop (with an index variable) in your answer.Represent the mathematical vector as
vector<double>
, and use a for- each loop (with no index variable) in your answer.The same as the previous question, except use a while-loop in your answer.
Represent the mathematical vector as a
vector<double>
, and use recursion (with no loops) in your answer.Instead of passing the vector directly to the function, pass in a pointer of type
double*
representing the first element, and a pointer of typedouble*
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
andreverse
) work.
The following questions are related:
- Write a function called
is_upper(c)
that returnstrue
if thechar
c
is one of the uppercase letters'A'
,'B'
, …,'Z'
, andfalse
otherwise. - Write a function called
to_lower(c)
that returns the lowercase version ofc
ifc
is an uppercase letter, andc
unchanged otherwise. - Using a loop (and no recursion), write a function called
to_lower_loop(s)
that returns a new string that’s the same as strings
, except all uppercase letters have been converted to lowercase. - Using recursion (and no loops), write a function called
to_lower_rec(s)
that returns a new string that’s the same as strings
, except all uppercase letters have been converted to lowercase. You can write helper functions if needed.
- Write a function called
The following questions are related:
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)
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)