Comp 116 Test 1 Study Guide

Test on March 15

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 March 8 (not included), 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:

You should be familiar with all aspects of basic C++ programs, such as those you created for labs 01 to 05. This includes:

Practice problems

  1. Any problem from one of the labs that you have not completed during lab time would be good practice.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.
  7. Suppose a file contains an integer, the number of other words contained in the files, followed by that exact number of words (strings separated by some kind of white space). Write a function that will read the file and print all the words on screen, one word per line.
  8. Same exercise as the previous one, except that this time, the file only contains words and you have to read the file until you read the end of the file. Write a function that reads all the words and prints them on screen one word per line.
  9. Look at the current content of your wallet. Determine the general categories of object you have there, and write a struct Wallet that contains a variable for each of those categories. Then, write code that would initialize each of those variables to a value corresponding to the current content of your wallet.