I tried my best to list the topics for the exam but I may have forgotten one or two. Your default assumption should be that if I covered it in class before October 3rd, it is fair game for the exam. If you have a specific question about whether a topic could be part of the exam, please come to ask me.
I will add more practice problems as I find new interesting questions, so come back to this page every few days, more practice problems may have appeared.
You should be able to define or explain the following terms:
- scope of a variable
- pointer
- differences between passing a value, a reference, an array and a pointer to a function
- differences between an interpreted language (like Python) and a compiled language (like C++)
- difference between static and dynamic memory allocation
You should be familiar with all aspects of basic C++ programs,
such as those you created for labs 01 to 05. This includes:
- how to compile and execute C++ programs
- the short, int, long, float, double, char, bool, string data types
- variables, assignment with =, and basic arithmetic
- boolean and relational operators in C++
- if / else if / else statements,
while loops, and for loops
- variables, including their declaration, definition, and use
- functions, including their declaration, definition, and use
- arrays (statically and dynamically allocated)
- basic uses of cout and cin
- basic uses of file streams and stringstreams
- purpose of #include and using namespace std;
- void and its use
- pointers, address-of, dereference, nullptr
- dynamic memory management with new, delete and delete[]
- struct, including their declaration how to use variables of that type
- tracing a program using a memory/stack diagram (heap and stack)
Practice problems
- Any problem from one of the labs that you have not completed during lab time would be good practice.
- Write a C++ program that prompts the user for an integer n
and then prints a single output integer, the sum of the integers from 1
to n.
- Write a boolean function isPrimaryColor that takes a string as an
argument and returns true if the string is "red", "yellow", or "blue", and
returns false otherwise. Then write a program that prompts
the user for a color and uses isPrimaryColor to determine if
their color is primary.
- Write a function that asks the user to repeatedly enter double precision floating point numbers until the user enters zero. Then, the function should return the sum of all the numbers entered.
- Consider the following code:
#include <iostream>
using namespace std;
void boo(int* a, int* b){
*b = *a + 5;
cout << "boo: " << *a << " " << *b << endl;
// draw stack diagram here
}
void foo(int a, int b) {
int* bar = &a;
boo(&b, bar);
cout << "foo: " << a << " " << b << endl;
}
int main() {
int x = 52;
int y = 34;
foo(y,x);
cout << "main: " << x << " " << y << endl;
}
Draw the stack diagram corresponding to the state of the program at the location indicated in the code and show all the output of the program that would be displayed on the terminal screen.
- Consider the following code:
#include <iostream>
using namespace std;
void swap(int* a, int* b) {
int tmp;
tmp = *a;
*a = *b;
// draw stack diagram here
*b = tmp;
}
int main() {
int array[4];
array[0] = 1;
array[1] = 2;
array[2] = 3;
array[3] = 4;
swap(&(array[1]), &(array[3]));
cout << "main: " << array[0] << endl;
cout << "main: " << array[1] << endl;
cout << "main: " << array[2] << endl;
cout << "main: " << array[3] << endl;
}
Draw the stack diagram corresponding to the state of the program at the location indicated in the code and show all the output of the program that would be displayed on the terminal screen.